Connect with Expertise | Find Experts, Get Answers, Share Insights

SelectedIndex is not a member of System.Windows.Forms.Control

mshmyob's Avatar
E
C
 
Join Date: Jan 2008
Location: witness protection
Posts: 669
#1: Mar 8 '10
I am trying to read values from textbox controls and combobox controls on a form.

Reading the values from textbox controls is working fine but I cannot read from the 3 combo boxes.

Here is some simplified code of what I am trying to do.

Expand|Select|Wrap|Line Numbers
  1. for intCounter = 1 to 3
  2.    dblPrincipal = Me.Controls("txtPrincipal" & intCounter).Text
  3.    intType = Me.Controls("cboM" & intCounter).SelectedIndex
  4. next intCounter
  5.  
The intType= statement (line 3) is where the problem occurs. Error message as in my Question Title. It appears I cannot reference the selectedindex property in the method.

Any ideas how I can do this for 3 combo boxes on my form.

cheers,

tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#2: Mar 8 '10

re: SelectedIndex is not a member of System.Windows.Forms.Control


Use the .Find method to locate your control. This will return the actual control. If you don't find it, then it doesn't exist. If you do find it, then you have an actual control to work with.

Expand|Select|Wrap|Line Numbers
  1. // C#
  2. combobox Bob = this.controls.find("cbom" + intCount);
  3. if (bob != null)
  4. {
  5.    bob.SelectedIndex;
  6. }
mshmyob's Avatar
E
C
 
Join Date: Jan 2008
Location: witness protection
Posts: 669
#3: Mar 8 '10

re: SelectedIndex is not a member of System.Windows.Forms.Control


I need this for vb.net.

The control does exist.

cheers,
mshmyob's Avatar
E
C
 
Join Date: Jan 2008
Location: witness protection
Posts: 669
#4: Mar 8 '10

re: SelectedIndex is not a member of System.Windows.Forms.Control


I actually got the controls.find to work but it returns a true/false whether the control exists - This doesn't help me - I know the control exists what I need is to be able to reference the name of the combobox control dynamically without the error.

I do not want to hard code for cboM1, cboM2, cboM3 - I would like it to work like my code for the text boxes above.

cheers,
tlhintoq's Avatar
E
C
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 3,476
#5: Mar 8 '10

re: SelectedIndex is not a member of System.Windows.Forms.Control


SelectedIndex is not a member of System.Windows.Forms.Control
Well.. That would be true.
SelectedIndex is a member of System.Windows.Forms.Controls.ComboBox

Try your original technique but cast your control to a ComboBox instead of a generic 'Control'

Also... Double check all your naming matches letter for letter, case for case, including zeros... I find it suspicious that control.find says you don't have it.

cboM03 is not cboM3 for example
cbom3 is not cboM3
mshmyob's Avatar
E
C
 
Join Date: Jan 2008
Location: witness protection
Posts: 669
#6: Mar 8 '10

re: SelectedIndex is not a member of System.Windows.Forms.Control


Try your original technique but cast your control to a ComboBox instead of a generic 'Control'
I am so daft sometimes. I was all around it with declaring controls and comboboxes but it was as simple as the following:

Expand|Select|Wrap|Line Numbers
  1.  
  2. for intCounter = 1 to 3 
  3.    dblPrincipal = Me.Controls("txtPrincipal" & intCounter).Text 
  4.   Dim myComboBox As ComboBox
  5.   myComboBox = Me.Controls("cboM" & intCounter)
  6.   intType = myComboBox.SelectedIndex
  7. next intCounter 
  8.  
  9.  

By the way - control.find always did find it but that was not what I wanted.

Thanks for your guidance.

cheers,
Reply