Newbie: How to loop through controls given form name?

  • Thread starter Thread starter deko
  • Start date Start date
D

deko

I'm trying to create a class that will return a string based on a switch
statement that matches a control name.

The app in question has several forms, and I want to provide in-form help
based on which checkbox is checked. So, in the different form classes, the
code would look something like this (note this is vb-like pseudo code):

private void chkLogToDatabase_CheckedChanged(object sender, System.EventArgs
e)
{
if ( this.chkLogToDatabase.Checked == true )
{
this.chkLogToFile.Checked = false;
lblHelp = contextHelp(Me.Form.Name); // I want to call contextHelp,
passing to it the name of the form
lblHelp.Visible = True;
}
else
}

In the contextHelp class, I want to define the string that will be returned
to lblHelp:

using System;
namespace dsync.UI
{
/// <summary>
/// returns a string to be displayed in the calling form
/// </summary>
public class contextHelp
{
//loop through controls on form, find those that begin with chk and are
checked:
ctlName = ...
switch (ctlName)
{
case "chkLogToDatabase":
strLogHelp "Log to database message";
case "chkLogToFile":
strLogHelp = "Log to file message";
case "chkResetLog":
strLogHelp = "Reset Log message";
default:
strLogHelp = "Limit log size Message";
}
}
}

Any help is appreciated...

Thanks in advance.
 
Hi deko,

Is this what you mean? This sample uses CheckBoxes and a Label (not used)
and a method to search for a form's checked CheckBoxes

public class Test : Form
{

public Test()
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
Do(f1);
Do(f2);
}

private void Do(Form f)
{
foreach(Control c in f.Controls)
{
if(c is CheckBox)
{
CheckBox ch = (CheckBox)c;
string strLogHelp = "";
switch(ch.Name)
{
case "TestA":
if(ch.Checked)
strLogHelp = "Test A";
break;
case "TestB":
if(ch.Checked)
strLogHelp = "Test B";
break;
default:
strLogHelp = "Default";
break;
}
// do stuff with strLogHelp
}
}
}


[STAThread]
public static void Main()
{
Application.Run(new Test());
}
}

public class Form1 : Form
{
CheckBox chkA;
CheckBox chkB;
Label lbA;
public Form1()
{
lbA = new Label();
this.Controls.Add(lbA);
chkA = new CheckBox();
chkA.Name = "TestA";
this.Controls.Add(chkA);
chkB = new CheckBox();
chkB.Name = "TestB";
chkB.Checked = true;
this.Controls.Add(chkB);
}
}

public class Form2 : Form
{
CheckBox chekA;
CheckBox chekB;
Label labA;
public Form2()
{
chekA = new CheckBox();
chekA.Name = "TestA";
this.Controls.Add(chekA);
chekB = new CheckBox();
chekB.Name = "TestB";
chekA.Checked = true;
this.Controls.Add(chekB);
labA = new Label();
this.Controls.Add(labA);
}
}
 
Is this what you mean? This sample uses CheckBoxes and a Label (not used)
and a method to search for a form's checked CheckBoxes

Hi and thanks for the reply. I am still rather green, so I hope you don't
mind some really simple questions. Coming from a VB background, I'm
wondering how I pass a value into the class. I want to call this from
different Forms, so I need to pass the name of the calling Form to the class
so it knows what set of controls to loop through. Is there a better way to
identify the calling Form?
public class Test : Form
{
okay, so here you are declaring a class - but what does the semicolon do?
Does " : Form" call other code? Is Form an argument?
public Test()
{
I understand that this is the constructor...
Form1 f1 = new Form1();
Form2 f2 = new Form2();
Do(f1);
Do(f2);
}

private void Do(Form f)
{
foreach(Control c in f.Controls)
{
if(c is CheckBox)
{
CheckBox ch = (CheckBox)c;
string strLogHelp = "";
switch(ch.Name)
{
case "TestA":
if(ch.Checked)
strLogHelp = "Test A";
break;
case "TestB":
if(ch.Checked)
strLogHelp = "Test B";
break;
default:
strLogHelp = "Default";
break;
}
// do stuff with strLogHelp
But how? I thought that this routine will not return anything because it's
public "void"...
}
}
}


[STAThread]
public static void Main()
{
Application.Run(new Test());
}
}

public class Form1 : Form
{
CheckBox chkA;
CheckBox chkB;
Label lbA;
public Form1()
{
lbA = new Label();
this.Controls.Add(lbA);
chkA = new CheckBox();
chkA.Name = "TestA";
this.Controls.Add(chkA);
chkB = new CheckBox();
chkB.Name = "TestB";
chkB.Checked = true;
this.Controls.Add(chkB);
}
}

public class Form2 : Form
{
CheckBox chekA;
CheckBox chekB;
Label labA;
public Form2()
{
chekA = new CheckBox();
chekA.Name = "TestA";
this.Controls.Add(chekA);
chekB = new CheckBox();
chekB.Name = "TestB";
chekA.Checked = true;
this.Controls.Add(chekB);
labA = new Label();
this.Controls.Add(labA);
}
}
 
Hi Deko,

Answers inline

Hi and thanks for the reply. I am still rather green, so I hope you
don't
mind some really simple questions. Coming from a VB background, I'm
wondering how I pass a value into the class. I want to call this from
different Forms, so I need to pass the name of the calling Form to the
class
so it knows what set of controls to loop through. Is there a better way
to
identify the calling Form?

Well, you could detect the name of the form once you have a reference to
it.
In the Do method check f.Name.

You can't simply just put a name into the Do method because you need a
reference to the Form before you can detect the checked status of its
CheckBoxes.
okay, so here you are declaring a class - but what does the semicolon do?
Does " : Form" call other code? Is Form an argument?

: means Test inherits the Form class
I understand that this is the constructor...

Correct, this is the constructor of the test class.
It will create two different type of forms, each with a slightly different
set of controls.
It will pass references of these two forms to the 'Do' method.
But how? I thought that this routine will not return anything because
it's
public "void"...

No, this method won't return anything, you have to change it to your needs.
It is merely demonstrating how to detect if a certain CheckBox is checked.
}
}
}


[STAThread]
public static void Main()
{
Application.Run(new Test());
}
}

public class Form1 : Form
{
CheckBox chkA;
CheckBox chkB;
Label lbA;
public Form1()
{
lbA = new Label();
this.Controls.Add(lbA);
chkA = new CheckBox();
chkA.Name = "TestA";
this.Controls.Add(chkA);
chkB = new CheckBox();
chkB.Name = "TestB";
chkB.Checked = true;
this.Controls.Add(chkB);
}
}

public class Form2 : Form
{
CheckBox chekA;
CheckBox chekB;
Label labA;
public Form2()
{
chekA = new CheckBox();
chekA.Name = "TestA";
this.Controls.Add(chekA);
chekB = new CheckBox();
chekB.Name = "TestB";
chekA.Checked = true;
this.Controls.Add(chekB);
labA = new Label();
this.Controls.Add(labA);
}
}
 
Back
Top