Results 1 to 9 of 9

Thread: Clear Comboboxes ?[Resolved]

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Clear Comboboxes ?[Resolved]

    Right now I have this code . It works fine unless I bind those comboboxes to arrays. any other solutions ?
    VB Code:
    1. Dim ComBox As Control
    2. For Each ComBox In ME.Controls
    3.                         If TypeOf ComBox Is ComboBox Then
    4.                             ComBox.Text = ""
    5.                         End If
    6.                     Next
    Thanks
    Last edited by Pirate; Apr 18th, 2003 at 01:25 PM.

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Is your goal just to clear the .Text property of each combobox, or to actually clear their items? I just wrote a little project that does what it sounds like you're doing, sets ComboBox1.DataSource to an array of strings, and made a button whose event runs the code you posted - and it works fine.

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Not exactly what I need but I may need to set selectedindex property to specific item . It's good suggestion by the way .

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Dim ComBox As Control
    2. For Each ComBox In ME.Controls
    3.       If TypeOf ComBox Is ComboBox Then
    4.          ComBox.SelectedItem=Nothing
    5.          'or
    6.          'ComBox.SelectedIndex=-1
    7.       End If
    8.    Next

  5. #5

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I've tried that already . Gave me this error "SelectedItem' is not a member of 'System.Windows.Forms.Control'"

  6. #6
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Seems to me the only reason you'd get that is if your If condition isn't quite right? All comboboxes expose a .SelectedItem and .SelectedIndex property, whatever their values might be.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Ahh then you have to cast it to a combobox specifically:

    VB Code:
    1. Dim ComBox As Control
    2.         For Each ComBox In Me.Controls
    3.             If TypeOf ComBox Is ComboBox Then
    4.                 Try
    5.                     'if not bound
    6.                     ComBox.Text = ""
    7.                     'if bound
    8.                     DirectCast(ComBox, ComboBox).SelectedIndex = -1
    9.                 Catch
    10.                 End Try
    11.             End If
    12.         Next

  8. #8

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Edneeis
    VB Code:
    1. Dim ComBox As Control
    2. For Each ComBox In ME.Controls
    3.       If TypeOf ComBox Is ComboBox Then
    4.          ComBox.SelectedItem=Nothing
    5.          'or
    6.          'ComBox.SelectedIndex=-1
    7.       End If
    8.    Next
    I'm talking about the above code . Have you tried it and got it to work (you or Edneeis)?

  9. #9

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This works fine Edneeis . Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width