Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm trying to read a value of a checkbox created at runtime by looping trhough the controls collection like this.

 

dim oControl as control

for each oControl in forms.controls

if typeof oControl is checkbox then

'*** read the checked property from the control

end if

next

 

But I don't have the checkbox properties when i'm in the for each loop.

Question how do I convert the oControl object to a checkbox control when I have it??

 

Cire

Trust the Universe
  • Leaders
Posted

if you are creating the checkbox at runtime, are you adding it to the Form's control collection? if you are then your method should work, just need to get the TypeOf ... Is CheckBox.

Dim oControl as Control 
For Each oControl in Controls
   If TypeOf oControl Is CheckBox Then
       If oControl.Name = " your checkbox's name " Then
           If oControl.Checked
               MessageBox.Show("It's checked!")
           End If
       End If
   End If
Next

Posted

I add the controls to the form. Does that mean that they are also added to the controls collection of that form.

 

Maybe I should also say that the controls are on a other form and i have set a reference the form with a overloaded constructor like this.

 

Private f as frmadmin

 

Sub New(frm as frmadmin)

f=frm

end sub

 

Creating an instance from a frmInput like :

Dim frm1 as frmInput(me)

 

When i do it like you wrote i don't have the checked property available.

 

Cire

Trust the Universe
  • Leaders
Posted
Creating an instance from a frmInput like :

Dim frm1 as frmInput(me)

i guess you are declaring the above as " New " ? eg:

Dim frm1 As New frmInput(Me)

then what i'd do is cycle through frm1's Controls, eg:

Dim oControl As Control 
For Each oControl In frm1.Controls '/// loop through frm1's Controls.
   If TypeOf oControl Is CheckBox Then
       If oControl.Name = " your checkbox's name " Then
           If oControl.Checked
               MessageBox.Show("It's checked!")
           End If
       End If
   End If
Next

Posted (edited)

I think I have that but it doesn't workfor me

 

Private f As New frmAdmin

Dim oControl As Control
For Each oControl In f.Controls
If TypeOf oControl Is CheckBox Then
   If tn.Text = f.Controls.Item(I).Name Then
      If oControl.checked Then
         strRequired = "1"
      Else
        strRequired = "0"
     End If
  End If
End If
Next

The intellisense gives the error "Checked is not a member of system.windows.forms.control

 

Any suggestions??

Edited by feurich
Trust the Universe
Posted (edited)

I'm not using it anymore because of the Stackoverflow error.

 

I'm runing in to the same problem in an other section of my application.

I want to print values from controls created at runtime and populated from a database. Now there are 3 kinds of controls:

TextBox, DateTimePicker and ComboBox.

 

For the date and the text i see no problem, you can acces the values with

for each oControl in me.controls
strPrint = strprint & Control.text 
Next

But accessing the combobox selected.Text is not possible in this scenenario. I don't have the selected.text propertywhen i acces oControl. Just like the checked property in the post above.

 

Anyone ??

Edited by feurich
Trust the Universe
Posted (edited)

If tn.SelectedImageIndex = g_AppConstants.TextBoxImageIndex Or tn.SelectedImageIndex = g_AppConstants.DTPImageIndex   Then
Dim oForm As New frmAddNewDTTXT(Me)
oform.Text = "  Edit TextBox / DateTimePicker : " & tn.Text
oform.txtDTTXT.Text = tn.Text
         For Each oControl In Me.grpIndexSet.Controls
                   If TypeOf oControl Is CheckBox And oControl.Name = tn.Text Then
                     oform.chkRequired.Checked = oControl.checked
                   End If
          Next
oform.ShowDialog()
End If

 

I still have the same problem with the CheckBox Control.

The checked property of the oControl has the error:

 

F:\Projects\.Net Projects\Print Barcode\Print Barcode\Print Barcode\Forms\frmAdmin.vb(562): 'checked' is not a member of 'System.Windows.Forms.Control'.

Anyone has a suggestion ???

 

Cire

Edited by feurich
Trust the Universe
Posted

For Each oControl In Me.grpIndexSet.Controls
  If TypeOf oControl Is CheckBox And oControl.Name = tn.Text Then
        oform.chkRequired = CType(oControl, CheckBox)
  End If
Next
oform.ShowDialog()

Do you mean something like this? This doesn't get the oControl.checked value in to the oForm.chekrequired.checked.property.

:(

Trust the Universe
Posted

No i meant something like this

 

oform.chkRequired.Checked = Ctype(oControl,Checkbox).Checked

Dream as if you'll live forever, live as if you'll die today
Posted

Check

 

This works fine Mehyar

 

Could you try to explain the = Ctype(oControl,Checkbox).Checked part?

 

I don't seem to understand it :confused:

 

ThkX and GreetZ,

 

Cire

Trust the Universe
Posted

Ok the reason is that, the Control doesnot have a checked property. Ofcourse you know that a Control could be a checkbox, textbox , etc. They all derive from the class control. When you think about it it should work without using what i proposed because you checked for the control to be a checkbox before hand. So by LateBinding it should work. There must be something special in this case that late binding doesnot work so you needed to cast the control explicitly to a check box in order to use the checkbox property.

 

Eventually try always to use explicit casting or turn option strict on

it is faster and safer than late binding.

Dream as if you'll live forever, live as if you'll die today
Posted

I looked at my projects settings in tools/options and the settings and the option stric and option Explicit where sboth et to off.

I put them both to ON.

 

But still I don't have the checked property in the oControl object.

In all the books I read they advice to leave option strict and option explicit on. I don't know why in my case this was switch off.

 

Cire

Trust the Universe
Posted

By default in Visual Basic.NET

 

Option Strict is set to Off

Option Explicit is set to On

 

even if you use them, control wont have the checked property because control is a general type and the checked property is only for check boxes ( a special type of control). The checked property wont appear unless you use DirectCast() or Ctype() .

Dream as if you'll live forever, live as if you'll die today
  • Administrators
Posted

you have declared oControl as Control - not every control has a checked property.

You will need to cast the oControl to a variable of type CheckBox before you can access the checked property.

 

dim chk as CheckBox
For Each oControl In Me.grpIndexSet.Controls
  If TypeOf oControl Is CheckBox And oControl.Name = tn.Text Then
         chk=DirectCast(oControl, CheckBox)
        oform.chkRequired.Checked =  chk.Checked
  End If
Next

 

or similar.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...