473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Determine a control's type using a switch statement

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
4 19047
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
6
by: Mark Broadbent | last post by:
How on earth did Anders & co arrive at this awful switch/ case/ select statement? Not only does it lose the abilty to do an intelligent switch a-la VB eg. using >, <, list of values etc .... but...
14
by: Chris | last post by:
Hi, can you specify a range in a switch - statement ? switch (i) { case 100 - 999 : // do something break; case 1000 - 9999:
3
by: John | last post by:
Is there a way in c# to have a switch statement with multiple expressions like in VB.net? Select Case e.Item.ItemType Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
67
by: Rui Maciel | last post by:
I've been delving into finite state machines and at this time it seems that the best way to implement them is through an intense use of the goto statement. Yet, everyone plus their granmother is...
16
by: Silent1Mezzo | last post by:
Is it possible to have multiple control variables in a switch statement? For example: switch(a , b) { case(a>b): case(b<a): case(a==b): }
2
by: Miyagi | last post by:
I have a bunch of conditions in which to determine different types of questions to ask in a survey I am constructing. Just was wondering if anybody has used a two-dimensional switch statement. If...
7
by: =?Utf-8?B?Sm9zZXBo?= | last post by:
I would like to know how to implement several Radiobuttons using the Switch statement instead of If/Else conditional statement. For instance, if I have the following: if (RadioButton1.Checked =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.