By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,325 Members | 1,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,325 IT Pros & Developers. It's quick & easy.

Determine a control's type using a switch statement

P: n/a
Hi all,

I am a VB programmer who is moving out of the shadows and starting to
code in C#. I would like to know how to determine a control's type
using a swtich statement. In VB i would do something like this:

Private Sub TestControl(ByRef PassedControl as Control)

Select Case True
Case TypeOf PassedControl Is TextBox
'Do something
Case TypeOf PassedControl Is ComboBox
'Do something completely different
Case Else
'Puke
End Select

End Sub

Is there any similar method in C#?

Thanks,

Jimmy V

Sep 13 '06 #1
Share this Question
Share on Google+
4 Replies

P: n/a
Hello Jimmy,

private void TestControl(ref Control PassedControl)
{
if (true == PassedControl is TextBox) {
} else if (true == PassedControl is ComboBox) {
} else {
}
}

JVPrivate Sub TestControl(ByRef PassedControl as Control)
JVSelect Case True
JVCase TypeOf PassedControl Is TextBox
JV'Do something
JVCase TypeOf PassedControl Is ComboBox
JV'Do something completely different
JVCase Else
JV'Puke
JVEnd Select
JVEnd Sub
JV>
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Sep 13 '06 #2

P: n/a

Jimmy V wrote:
Hi all,

I am a VB programmer who is moving out of the shadows and starting to
code in C#. I would like to know how to determine a control's type
using a swtich statement. In VB i would do something like this:

Private Sub TestControl(ByRef PassedControl as Control)

Select Case True
Case TypeOf PassedControl Is TextBox
'Do something
Case TypeOf PassedControl Is ComboBox
'Do something completely different
Case Else
'Puke
End Select

End Sub

Is there any similar method in C#?

Thanks,

Jimmy V

foreach(Control c in this.Controls)
{
switch (c.GetType().Name)
{
case "TextBox":
//do something
break;
default:
Console.WriteLine(c.GetType().Name);
break;
}
}

Remember controls can contain controls, and don't forget the break
statements.
Ian

Sep 13 '06 #3

P: n/a
Michael Nemtsev <ne*****@msn.comwrote:
Hello Jimmy,

private void TestControl(ref Control PassedControl)
{
if (true == PassedControl is TextBox) {
} else if (true == PassedControl is ComboBox) {
} else {
}
}
Note that for the sake of readability, you'd be better off with:

if (PassedControl is TextBox)

There's no need for the comparison with "true" - it just needlessly
complicates things.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 13 '06 #4

P: n/a
Hello Jon Skeet [C# MVP],

to be trully I just used online C#->VB.net convertor to regenerate OP code :)

JMichael Nemtsev <ne*****@msn.comwrote:
J>
>Hello Jimmy,

private void TestControl(ref Control PassedControl) { if (true ==
PassedControl is TextBox) { } else if (true == PassedControl is
ComboBox) { } else { } }
JNote that for the sake of readability, you'd be better off with:
J>
Jif (PassedControl is TextBox)
J>
JThere's no need for the comparison with "true" - it just needlessly
Jcomplicates things.
J>
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Sep 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.