Learning how to call form components  
Author Message
xlordt





PostPosted: Visual C# Language, Learning how to call form components Top

I want to read up on how to do things like..
CheckBox chk = panel1.Controls[ i ] as CheckBox;
anyone has any tutorials for this Like were did you learn how to declare codes like this Also is there another way to select certain childs from a certain parents




Visual C#19  
 
 
cverdon





PostPosted: Visual C# Language, Learning how to call form components Top

Hi,

Panels are controls which contains other controls. A control's children is exposed through the Controls collection of child controls.

Usually controls are accessed as you mentionned above when they are created dynamically at tun time. Normally this is not needed as the designer assigns a name to the control for us to use.

In some situation it is preferable to create dynamically an area of the UI. For example let's consider an application which asks a question from a database to the user. If the number of possible answers (checkboxes) is not constant, we need to create the checkboxes dynamically:

//create the UI for the answers (when question shown)

//answerPanel is a panel that was dropped using the VS designer

//remove all previous checkbox of previous question

answersPanel.Controls.Clear();

answersPanel.SuspendLayout();


//create new checkboxes for the new question dynamically

foreach(Answer ans in question.Answers) {

CheckBox chk = new CheckBox();

chk.Text = ans.Text;

answersPanel.Controls.Add( chk );

}

answersPanel.ResumeLayout();

//verify the answers (when button clicked)

int index = 0;

bool success = true;

//loop through all checkboxes to verify answer

foreach (Control ctrl in answersPanel.Controls) {

//ctrl is a generic Control, it needs to be cast to

//CheckBox to read it's value

//the answer is correct if the checkbox is only

//checked when the answer is supposed to be checked

success &= !((ctrl as CheckBox).Checked ^ question.Answers[index].IsCorrect);

index++;

}

if (!success) {

MessageBox.Show( "You failed" );

}

I hope this sample code helps you.
Charles


 
 
xlordt





PostPosted: Visual C# Language, Learning how to call form components Top

So you mean.. that a Control(Panel) that can hold other controls (ie check boxes.) can be declared this way also I might not make sense here but, in your example you checked to see if the checkbox was checked.. what If i just wanted to see if it was a checkbox becaue I see that your using the "as" to add all checkboxes in to ctrl, correct


 
 
cverdon





PostPosted: Visual C# Language, Learning how to call form components Top

Yes a panel can hold any types of controls.

In c# to test the type of the control you can use the is keyword.

if (ctrl is TextBox) {...}
if (ctrl is CheckBox) ...

Generally more complex controls are made of other controls. for example the datagridview is made of dozens of them.

Charles


 
 
xlordt





PostPosted: Visual C# Language, Learning how to call form components Top

Nice.. thanx :)