feurich Posted November 26, 2003 Posted November 26, 2003 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 Quote Trust the Universe
Leaders dynamic_sysop Posted November 26, 2003 Leaders Posted November 26, 2003 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 Quote
feurich Posted November 26, 2003 Author Posted November 26, 2003 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 Quote Trust the Universe
Leaders dynamic_sysop Posted November 26, 2003 Leaders Posted November 26, 2003 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 Quote
feurich Posted November 26, 2003 Author Posted November 26, 2003 (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 November 27, 2003 by feurich Quote Trust the Universe
feurich Posted November 26, 2003 Author Posted November 26, 2003 Using the as new When I use the as New statment in the assigment. I get a stackoverflow. Cire Quote Trust the Universe
Administrators PlausiblyDamp Posted November 26, 2003 Administrators Posted November 26, 2003 Are you using Private f As New frmAdmin from within frmAdmin? If so it will keep creating a new instance every time it creates a new instance (recursion is a nasty thing at times). Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
feurich Posted November 26, 2003 Author Posted November 26, 2003 (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 November 27, 2003 by feurich Quote Trust the Universe
feurich Posted December 3, 2003 Author Posted December 3, 2003 (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 December 3, 2003 by feurich Quote Trust the Universe
Mehyar Posted December 3, 2003 Posted December 3, 2003 Did you try to Ctype(oControl,Checkbox) ??? Quote Dream as if you'll live forever, live as if you'll die today
feurich Posted December 3, 2003 Author Posted December 3, 2003 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. :( Quote Trust the Universe
Mehyar Posted December 3, 2003 Posted December 3, 2003 No i meant something like this oform.chkRequired.Checked = Ctype(oControl,Checkbox).Checked Quote Dream as if you'll live forever, live as if you'll die today
feurich Posted December 3, 2003 Author Posted December 3, 2003 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 Quote Trust the Universe
Mehyar Posted December 4, 2003 Posted December 4, 2003 Just a question first... Do you have Option Strict On ? Quote Dream as if you'll live forever, live as if you'll die today
feurich Posted December 4, 2003 Author Posted December 4, 2003 Not in the module where this code is runing, only in a class where I handle treeview stuff. Cire Quote Trust the Universe
Mehyar Posted December 4, 2003 Posted December 4, 2003 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. Quote Dream as if you'll live forever, live as if you'll die today
feurich Posted December 4, 2003 Author Posted December 4, 2003 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 Quote Trust the Universe
Mehyar Posted December 4, 2003 Posted December 4, 2003 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() . Quote Dream as if you'll live forever, live as if you'll die today
Administrators PlausiblyDamp Posted December 4, 2003 Administrators Posted December 4, 2003 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
feurich Posted December 4, 2003 Author Posted December 4, 2003 Looked up casting on the internet this is a very clear explanation of it ...I think. http://vbnetadvisor.com/doc/12798 Thanks for the explanation. Cire :) Quote Trust the Universe
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.