Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set all comboboxs listIndex to 0 and clear form controls

Status
Not open for further replies.

Lxmm

Programmer
Joined
May 10, 2005
Messages
32
Location
GB
Hi All,
Can any one help me with the following please. My code below does not seems to work.

1. Disable all controls on form load - I have various controls (textboxes, listboxes, comboboxes, Tabcontrols etc) on a form and I need to disable these controls at form load. The code I used for this does not seems to work as some of the controls are inside TAB/Panel controls. Is there a different way to do this?

Dim ctrl as control
For Each ctrl In me.Controls
If TypeOf ctrl Is TextBox Then ctrl.Enabled = False
If TypeOf ctrl Is ComboBox Then ctrl.Enabled = False
If TypeOf ctrl Is ListBox Then ctrl.Enabled = False
Next

================================================

2. How to set listindex (to -1) of all comboboxes in a form?

Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeOf ctrl Is ComboBox Then ctrl.listindex = -1
Next


Any help resolving above greatly appreciated.
Thanks
 
For the first question, you will need to use a recursive function to get to all the controls that belong to other panels etc. Check out Rick's example in thread796-1056339. This will also apply to your second question.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Thanks for your answer. But It does not seem to work the way I wanted. I have put this coding in my BaseForm, and when I call the function for an inherited form, it does not go through each control. Rather it scan only TAB and Panel controls.
 
That was just an example that Rick provided for that particular thread. I pointed it out to demonstrate "how" to create a recursive function. If you want to use it to loop through the controls in a form, you will have to adapt it to loop through Me.Controls or you could pass it the form and have it loop through the controls in the form that is passed.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Many thanks it works as I wanted to be,
Thanks again for your help.
 
Sorry to bother again but do you know how to set the listindex to -1 if the control is a combobox?

VB.NET does not seem to like the following command

Private Sub recursiveControls(ByVal ctl As Control)
Dim SubCtl As Control
For Each SubCtl In ctl.Controls
recursiveControls(SubCtl)
If TypeOf SubCtl Is ComboBox Then SubCtl.SelectedIndex = -1
Next
End Sub
 
What do you mean by "VB.NET does not seem to like the following command"? Does it produce an error?

From what I can see of your code though, each sub control calls the recursive function before you check it's type and therefore probabl won't be reset.



--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
I have put his code in a Public Sub in my baseform where there are no controls. I have use this form to build all my other forms, so that I can use most of the built in functions/validations written in my baseform.

The erros message is

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


Is this because I do not have a comboboxes on my Baseform?
 
The error message is saying that there isn't a property named SelectedIndex for a control (i.e. a system.Windows.Forms.ComboBox control contains a property named SelectedIndex but a system.Windows.Forms.Control control doesn't)

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
So how can I loop through each control on the form and set its selectedIndex to -1 if the control is a Combobox?

 
If you declare a combobox in your function, you could just set the control to the control. e.g
Code:
            If TypeOf SubCtl Is ComboBox Then
                MyComboBox = CType(SubCtl, ComboBox)
                MyComboBox.SelectedIndex = 1
            End If

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Thanks and your code worked.
But now I have a different problem. In the form load event, I populate these various comboboxes with different lookup tables values and set its selectedindex to -1 so that no text shown at form start. However when I navigate to different tabpages, all my comboboxes shows the first item in the list (all my comboboxes are in different tab pages). Is this normal in vb.net and do you know gow to correct?
Thanks again for your help.
 
I had this same problem recently. It is due to a Microsoft bug (can you believe it??). ::-) Check this link:
The problem is that if your combo box is bound to a datasource, then you have to set the selectedindex to -1 twice before it clears.
Code:
cboProduct.SelectedIndex = -1
cboProduct.SelectedIndex = -1
Just like that. HOWEVER, I found that if you are trying to clear combo boxes on an MDI child form, this still does not work. It works for any function after loading (for example, a "Clear" button), but not on the load event itself. I am not sure why this is, but I think it has something to do with perhaps an MDI child's controls not being considered completely "loaded" until the load event is finished... I have no clue.

Anyway, what I did was to make a public subroutine on my form that the MDI Parent could call after the load. For example, something kind of like this:
Code:
Public Sub ClearComboBoxes ()
    For Each ctl As Control in Me.Controls
        If TypeOf ctl Is ComboBox Then
            ctl.SelectedIndex = -1
            ctl.SelectedIndex = -1
        End If
    Next ctl
End Sub
Then your MDI Parent will open the child form as follows:
Code:
Dim MyForm As New TestForm
MyForm.MdiParent = Me
MyForm.Show()
MyForm.ClearComboBoxes() ' this is the extra step
It's a pain, but it works. I hope this helps.
 
Many thanks for your answer. I'll change my coding accordently.
Thank you both once again for you time & help.
 
Hello,

The error message is saying that there isn't a property named SelectedIndex for a control (i.e. a system.Windows.Forms.ComboBox control contains a property named SelectedIndex but a system.Windows.Forms.Control control doesn't)

The combobox and listbox control inherit from control and then extend the functionality. What you need to do is use CType to cast the control and then it will work. I beat my head against that wall for a couple hours before I figured it out. It's always easy when you find it!
Code:
If TypeOf SubCtl Is ComboBox Then CType(SubCtl, ComboBox).SelectedIndex = -1
Good Luck!


Have a great day!

j2consulting@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top