Ctype function

mevets

Member
Joined
Jun 22, 2006
Messages
17
Location
Northern, VA
Programming Experience
Beginner
I have a large case select that I want to simplify. Inside the select I'm creating controls base of which ToolStripButton is checked.
VB.NET:
Select Case strCtrl
                Case "Text"
                    If btnCTRLSText.CheckState = CheckState.Checked Then
                        newCtrl.Name = "lbl" & intText
                        newCtrl.Tag = newCtrl.Name
                        newCtrl.Text = "txt" & intText
                        newCtrl.Location = New Point(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y - 21)
                        Controls.Add(newCtrl)
                        newCtrl.BringToFront()
                        AddHandler newCtrl.Click, AddressOf CtrlHandler_Click
                        intText += 1
                        btnCTRLSText.CheckState = csUn
                    End If
                Case "Textbox"
                [COLOR=Red][B]ETC...[/B][/COLOR]
I have been able to simplify many case selects using
VB.NET:
CType(sender, Control).Height = 200
I dont have to list all the cases of what kind of control it could be (label, textbox, etc.) cause their all controls and all have the Height method. But, using 'Control' won't work when trying the CheckState method so the second param cannot be Control. I tried
VB.NET:
CType(sender, ToolStripButton).CheckState = True
but that was a no go, System.Windows.Forms.ToolStrip cannot be converted to System.Windows.Forms.Control.

Anyone have some helpfl suggestions?
 
Well you can't use Ctype(..,control) because ToolStripButton inherits from component. I'm confused though, the click event on a toolstrip should have a parameter that tells you which button was pressed, no?
Incidently, when casting with reference types it's better and faster to use

VB.NET:
DirectCast(...,...)

instead of Ctype..
 
Well to be honest, I'm not using 'sender', I'm using 'lastSender', which is the saved sender of a sub that handles all the .clicks of the ts buttons. I can then see if the lastSender.CheckState is checked and then add the control to the form.

Thanks though! DirectCast works great!
 
you have one sub, that handles 10 button clicks?

And this sub has a Switch statement with 10 cases in it

So effectively you wrote just as much code, in a very messy way, as if you'd had 10 click event handlers..

Why did you do this? (We can probably tell you how to write it better, more maintainable and more OO style)
 
Back
Top