Not sure of the subject...

  • Thread starter Thread starter Brian P. Hammer
  • Start date Start date
B

Brian P. Hammer

I have an app that allows users to supply custom values for various
calculations; about 43 of them. I use checkboxes to identify the use or
lack there of, of a custom value. I need to be able to tell when the check
state changes. Instead of coding for each one of the 43 check boxes and text
boxes, I'd like to have one sub that checks. I thought I would just add the
name of the textbox to the checkbox tag and ctype it but I get an invalid
cast exception. Here's what I am trying to accomplish:

Private Sub chkFlightBlock_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles chkFlightBlock.CheckedChanged, ....,
....., chkAnotherCustom

Dim ctl As TextBox
ctl = CType(sender, CheckBox).Tag
ctl.Enabled = CType(sender, CheckBox).Checked
If CType(sender, CheckBox).Checked Then
ctl.BackColor = System.Drawing.SystemColors.Window
Else
ctl.BackColor = System.Drawing.SystemColors.Control
End If
End Sub

In essence, I'd like to store the name of the textbox in the checkbox and
handle my code in one area instead of many different subs. Any help would be
great.

Thanks,
Brian
 
You're declaring the variable ctl as a TextBox but casting the tag, which
you've stored as a string, to that type. This is obviously wrong.

dim ctl as TextBox = CType(sender TextBox)
Dim txt As string
txt = CType(ctl.Tag,string)
ctl.Enabled = CType(sender, CheckBox).Checked 'is this what you really
want??? An un-checked box will become useless.
If CType(sender, CheckBox).Checked Then
ctl.BackColor = System.Drawing.SystemColors.Window
Else
ctl.BackColor = System.Drawing.SystemColors.Control
End If


--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Bob - Thanks for the insight... Last night I went ahead and made a custom
checkbox control and added a textbox property to store the textbox. It
works but that means I have to change all my checkboxes.

Not necessarily disable them, I was just playing around with different ways.
I load the variables into properties on app startup. If the user changes
them, then I just update the property and store the result in app.config so
the textbox really does become useless, just there to show the user what
value is being used.

Thanks,
Brian
 
What exactly have you stored in the CheckBox's Tag property? The
Textbox or the string *name* of the textbox? If it is only the name of
the textbox, then this line:

ctl = CType(sender, CheckBox).Tag

is the offending line. If the tag contains a string, you cannot cast
it to a textbox!
 
Back
Top