Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert Control to RadioButton

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
Hi
I have a form. When I press a button, I am going through all the controls in the form and seeing if it is a radio button

Code:
foreach (Control rb in gbOptions.Controls)
	{
		if (rb is RadioButton)
		{
			Console.WriteLine(rb.Name);
		}
	}

What I need to do is determine if the rb is checked, but since rb is being declared as a control, I can not do that since Control does not have a Checked Property. If I declare rb as a RadioButton first like thisk

Code:
foreach (RadioButton rb in gbOptions.Controls)
	{
		if (rb is RadioButton)
		{
			Console.WriteLine(rb.Name);
		}
	}

Two things happen, one the if statements is invalid because rb will always be a RadioButton and two it will error out when it comes accross a control that is not a radiobutton.

Any idea's?

Thanks,
RT
 
//I don't like foreach loops. regular for loops give you more control.

//basically, loop through each control, check if it's a radiobutton. (Replace rbButton with the name of one of your radiobuttons, or use System.Windows.Forms.RadioButton.

//The radiobutton then has a Checked property (I double checked) so if it is checked, then do your stuff.

//Hope that helps. P.S. this code is directly from my head, not tested.

for (int i = 0; i < gbOptions.Controls.Count; i++)
{
if (gbOptions.Controls.GetType() == rbButton.GetType())
{
if (((RadioButton) gbOptions.Controls).Checked)
{
//do your stuff here
}
}
}
 
A for loop takes 3 pieces of information:

Your starting condition int i = 0;
Your Final condition i < 10;
What happens after each i++;

you don't have to use these in the typical manner. For example:


string yourstring = "";
for (int i = 0; yourstring != "end"; i++)
{
txtMessages.Text += "Try #" + i + "\n";
yourstring = getInput();
}

private string getInput()
{
//capture input string here and return it.
return somestring;
}

This will keep getting input until "end" is returned. But this is only 1 flexibility of the for loop. I can't think of more off hand, but the possibilities are huge.

I had a teacher in college show us some crazy for loops.
 
When I was learning Assembler, I came across C for loops. One particular book gave an example of a for loop, that the author had created to demonstrate how not to use them, that he claimed to have shown to several experienced C programmers, none of which was able to work out what was going on between the brackets. The purpose of the example was to show how not to use for loops. Just because they can be used in a certain way does not mean that it is good to use them in that way.

The foreach construct is especially useful when iterating through collections and when the ordinal position of the object within the collection is not relevant.

Personally I don't like C/++/#. I think your example confirms why. A while construct would do exactly the same as your for and, at least in my opinion, would be intelligable to programmers of many other languages without explanation. Without your explanation I would not have had a clue what your for statement meant.

 
That's why it's my personal preference. The flexibility is there if you need it in some strange case. That's also why C# allows you to write //comments.

You think a for loop is bad, you should see some of the crazy SQL out there.
 
There we are in 100% agreement. [smile]

As an aside I would say that 70-80% of my Delphi code is not commented, although //comments and {comment blocks} are available. I ensure that my code is as "self-documenting" as possible, only using actual comments when that is not possible, and why it is essential that programmers in C and its derivatives do thoroughly comment their code.

Unfortunatley not all do [thumbsdown]

As a further aside - which I'm going to admit through gritted teeth [curse]- on reflection, I quite like your example.
 
Similar to c++

Code:
foreach (Control c in gbOptions.Controls)
{
    if (c is RadioButton)
    {
        RadioButton rb = (RadioButton)c;
        Console.WriteLine(rb.Name);
    }
}
which in c++ is something like (don't do managed C++, only plain):

Code:
for (Controls::iterator b = gbOptions.Controls.begin(); 
     b != gbOptions.Controls.end(); 
     ++b)
{
    RadioButton *rb = dynamic_cast<RadioButton *>(*b);
    if (rb != null) 
    {
        cout << rb.Name;
    }
}
 
JurkMonkey,
when i replace rbButton with System.Windows.Forms.RadioButton, I get this error:

F:\dot-net\c#\AslTextAnalyzerWinApp\frmAslTextViewer.cs(471): 'System.Windows.Forms.RadioButton' denotes a 'class' where a 'variable' was expected

Any Idea?

Thanks
 
maybe try RadioButton tempbtn = new RadioButton;

if (gbOptions.Controls.GetType() == tempbtn.GetType())
{
}


it's a bit of a hack... but it should work
 
The solution of casting the control to a radio button is right on the money.

foreach (Control rb in gbOptions.Controls)
{
if (rb is RadioButton)
{
Console.WriteLine(( Radiobutton ) rb ).Name );
}
}

As for the foreach vs for loop in the .NET framework the foreach is the preffered method in this case. In the framework the comipler will generate the best code for the particular type of colletion you are itterating over. Remember you are dealing with the CLR, MSIL, and a JIT compiler here. :)


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top