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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mshmyob
    Recognized Expert Contributor
    • Jan 2008
    • 903

    #1

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

    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.

    [code=vb]
    for intCounter = 1 to 3
    dblPrincipal = Me.Controls("tx tPrincipal" & intCounter).Tex t
    intType = Me.Controls("cb oM" & intCounter).Sel ectedIndex
    next intCounter
    [/code]

    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
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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.

    Code:
    // C#
    combobox Bob = this.controls.find("cbom" + intCount);
    if (bob != null)
    {
       bob.SelectedIndex;
    }

    Comment

    • mshmyob
      Recognized Expert Contributor
      • Jan 2008
      • 903

      #3
      I need this for vb.net.

      The control does exist.

      cheers,

      Comment

      • mshmyob
        Recognized Expert Contributor
        • Jan 2008
        • 903

        #4
        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,

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          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

          Comment

          • mshmyob
            Recognized Expert Contributor
            • Jan 2008
            • 903

            #6
            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:

            [code=vb]

            for intCounter = 1 to 3
            dblPrincipal = Me.Controls("tx tPrincipal" & intCounter).Tex t
            Dim myComboBox As ComboBox
            myComboBox = Me.Controls("cb oM" & intCounter)
            intType = myComboBox.Sele ctedIndex
            next intCounter

            [/code]


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

            Thanks for your guidance.

            cheers,

            Comment

            Working...