Dynamic controls

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

quick question,
Im using VB.NET
Im creating a checkbox dynamically then I stick it on a panel. I give each
one a unique id example: "chkLONDON"
Dim oObjCheckBox As CheckBox = New CheckBox()

oObjCheckBox.ID = "chk" +
CType(dsCustomer.Tables(0).Rows(iCount).Item("LocName"), String)

oObjCheckBox.Text = CType(dsCustomer.Tables(0).Rows(iCount).Item("LocName"),
String)

pnlLocs.Controls.Add(oObjCheckBox)

My problem now is, how do I see if that value is checked or not from my
code. When I push my submit button I want to check if its cheked or not. I
tried the following but it didn't work.

Dim oObjCheckBox As CheckBox = New CheckBox()

oObjCheckBox.ID = "chkLONDON"

If (oObjCheckBox.Checked) Then

'do some stuff

Else
 
Hello Joe,

To use the dynamically created controls you have to get the reference of the
control object.
Here you can do that by iterating through the controls in Panel.

Dim myControl as Control
For each myControl in Panel.Controls
If TypeOf (myControl) Is CheckBox Then
Dim myCheckBox as CheckBox
If myCheckBox.ID="Test" and myCheckBox. myCheckBox.Checked then
'Do your stuff
Exit For
End If
End If
Next

Hopes this will help

Sakharam Phapale
 
Hi Joe,

I missed one statement so corrected here.

Dim myControl as Control
For each myControl in Panel.Controls
If TypeOf (myControl) Is CheckBox Then
Dim myCheckBox as CheckBox

------------ myCheckBox =
Type(myControl,CheckBox) -----------------------------------

If myCheckBox.ID="Test" and myCheckBox. myCheckBox.Checked then
'Do your stuff
Exit For
End If
End If
Next


Sakharam Phapale said:
Hello Joe,

To use the dynamically created controls you have to get the reference of the
control object.
Here you can do that by iterating through the controls in Panel.

Dim myControl as Control
For each myControl in Panel.Controls
If TypeOf (myControl) Is CheckBox Then
Dim myCheckBox as CheckBox


myCheckBox = CType(myControl,CheckBox)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top