PC Review


Reply
Thread Tools Rate Thread

How do I reference a checkbox in code given a string?

 
 
Mason
Guest
Posts: n/a
 
      16th Aug 2004
I want to do a for loop through 45 check boxes, whose names are
CheckBox1 ... CheckBox 45. I'd like to do something like this:

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNext.Click

Dim i As Integer
Dim strCheckBox As String

For i = 1 To 45
strCheckBox = "CheckBox" & i
test = strCheckBox
If '***strCheckBox.Checked=True***
MsgBox(strCheckBox & "=True")
End If
Next
End Sub

I can't figure out how to reference a checkbox when I have only its
name. It should be a fairly simple piece of code, but I just can't
find it. Any help would be appreciated.

Thanks,

MW
 
Reply With Quote
 
 
 
 
Marina
Guest
Posts: n/a
 
      16th Aug 2004
Try:
Dim curCheckBox as CheckBox

....

curCheckBox = CType(Me.FindControl("CheckBox" & i),CheckBox)

"Mason" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I want to do a for loop through 45 check boxes, whose names are
> CheckBox1 ... CheckBox 45. I'd like to do something like this:
>
> Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles btnNext.Click
>
> Dim i As Integer
> Dim strCheckBox As String
>
> For i = 1 To 45
> strCheckBox = "CheckBox" & i
> test = strCheckBox
> If '***strCheckBox.Checked=True***
> MsgBox(strCheckBox & "=True")
> End If
> Next
> End Sub
>
> I can't figure out how to reference a checkbox when I have only its
> name. It should be a fairly simple piece of code, but I just can't
> find it. Any help would be appreciated.
>
> Thanks,
>
> MW



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      16th Aug 2004
* "Marina" <(E-Mail Removed)> scripsit:
> Try:
> Dim curCheckBox as CheckBox
>
> ...
>
> curCheckBox = CType(Me.FindControl("CheckBox" & i),CheckBox)


For web forms. This won't work in Windows Forms.

\\\
Private Function FindControl( _
ByVal ControlName As String, _
ByVal CurrentControl As Control _
) As Control
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = ControlName Then
Return ctr
Else
ctr = FindControl(ControlName, ctr)
If Not ctr Is Nothing Then
Return ctr
End If
End If
Next ctr
End Function
///

Usage:

\\\
DirectCast(FindControl("Button1", Me), Button).Enabled = False
///

Notice that the procedure listed above is "slow", if you have to access a
lot of controls by name very often, you should store references to them in a
'Hashtable' object. You can use the name of the control as key:

\\\
Private m_Controls As New Hashtable()
///

Adding a control:

\\\
Dim DynamicPictureBox As New PictureBox()
DynamicPictureBox.Name = "PictureBox1"
m_Controls.Add(DynamicPictureBox.Name, DynamicPictureBox)
///

Looking for a control:

\\\
Dim p As PictureBox = DirectCast(m_Controls.Item("PictureBox1"), PictureBox)
///

Removing a control:

\\\
m_Controls.Remove("PictureBox1")
///

Sometimes it's even better to add the control to an array. This will allow
fast and easy index-based access to the control references:

\\\
Dim MyLabels() As Label = {Label1, Label2, ..., Label10}
///

Access by 'MyLabels(0)' to 'MyLabels(9)'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
 
Reply With Quote
 
=?Utf-8?B?QWJ1YmFrYXI=?=
Guest
Posts: n/a
 
      17th Aug 2004
or you can do a:
Dim checkboxes() As CheckBox = New CheckBox() {CheckBox1, CheckBox2}
For Each chk As CheckBox In checkboxes
chk.Checked = True


Next
Put as many check boxes in the array.
Hope that helps.
Abubakar.
http://joehacker.blogspot.com


"Mason" wrote:

> I want to do a for loop through 45 check boxes, whose names are
> CheckBox1 ... CheckBox 45. I'd like to do something like this:
>
> Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles btnNext.Click
>
> Dim i As Integer
> Dim strCheckBox As String
>
> For i = 1 To 45
> strCheckBox = "CheckBox" & i
> test = strCheckBox
> If '***strCheckBox.Checked=True***
> MsgBox(strCheckBox & "=True")
> End If
> Next
> End Sub
>
> I can't figure out how to reference a checkbox when I have only its
> name. It should be a fairly simple piece of code, but I just can't
> find it. Any help would be appreciated.
>
> Thanks,
>
> MW
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert cells reference to string reference?? Robert Crandal Microsoft Excel Programming 2 1st Dec 2009 07:46 AM
checkbox with relative reference? Sven Berg Microsoft Excel Discussion 2 21st Apr 2008 08:21 AM
Color code Yes/No checkbox and text dependent on checkbox =?Utf-8?B?VG90YWxseUNvbmZ1c2Vk?= Microsoft Access Forms 2 16th Feb 2006 07:38 PM
Checkbox not updating on cross reference =?Utf-8?B?TWFyaw==?= Microsoft Word Document Management 1 5th Dec 2005 10:55 PM
Please Help!checkbox relative reference Adresmith Microsoft Excel Programming 0 13th May 2004 05:46 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:23 PM.