Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WinForms Form with a Textbox and a ComboBox and several other controls. The user can type a search string in the textbox and then the combo box is filtered based on the search string. The Filtering method fires on the Text Changed event of the Textbox.

When the user types text into the textbox, the DroppedDown property of the combo box is set to true so that the combo box opens and the user can see the items as they are being filtered.

This all works great.


It is possible for the user to type in a filter string which results in the combo box being empty. This is where the problem comes in. There has to be at least 1 valid item in the combo box. So, what I would like to do is this:

If the textbox loses focus, I want to use the validating event to check if there is at least one item in the combo box. If not, alert the user and cancel so that the focus remains in the textbox.

The problem is that something weird is happening if I click on the form that contains the textbox and there are no items in that combo box. I get a very non-descriptive error message on the parent form that launched the form that I am working on.

QueryDesignerTool qdTool = new QueryDesignerTool();
            // moved this to QueryDesignerTool_OnLoad function. ens 01/28/2015
            //this.Hide();
            DialogResult dr = qdTool.ShowDialog(this);


I get
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll

Additional information: InvalidArgument=Value of '0' is not valid for 'index'.


While the code is running, if I inspect qdTool and look at the combo box in question, I can see that the SelectedIndex of the combo box is 0 and the count of items is 0, which is illegal.

I am not sure why it is erroring out and falling back to the parent form. But that is not why I am asking for help.

What I am interested in is validating the text in the textbox if the user clicks out of it. I have read that focus cannot be passed to a form, so the Leave Focus event will not fire and hence the Validating event will not fire if focus is on the text box and then the user clicks the form.

So then, is the proper course of action then to catch the click event for the form and handle the validation of that text box?

What I have tried:

I have described what I have tried in the problem description.
Posted
Updated 20-Mar-17 5:55am
Comments
Ralf Meier 17-Mar-17 17:54pm    
The Textbox only looses the Focus if another Control gets it ...
Have you tried to use the MouseLeave-Event ?

Hi,
I think your requirement is Combobox with Autocomplete:
Please check this link:
Autocomplete ComboBox c# vb.net[^]

I hope this will help..
 
Share this answer
 
To solve the form error, before filtering the ComboBox, set the SelectedIndex property to -1. Perform a SuspendLayout, set the SelectedIndex property to -1, Filetre the items, and then reset the SelectedIndex as appropriate. Finally, call ResumeLayout to redraw the combo.

I would perform the validation in the TextBox.Leave event, which is fired whenever the Textbox loses focus.
 
Share this answer
 
I solved the problem. Instead of trying to validate what the user types in after they click outside the filter textbox, I decided to handle it a different way.

private void txtTbl1FromListFilter_TextChanged(object sender, EventArgs e)
   {
      string FilterText = txtTbl1FromListFilter.Text.ToLower().Trim();
      fromComboBox_T1.DroppedDown = true;
      FilterTables(FilterText, fromComboBox_T1, m_FromFilter_T1);

      if (fromComboBox_T1.Items.Count == 0)
      {
          txtTbl1FromListFilter.Text = PreviousFilterText_T1;
      }
      else
      {
          PreviousFilterText_T1 = txtTbl1FromListFilter.Text;
      }
  }


I created a module level string variable called PreviousFilterText_T1. This gets set to empty string when the form loads. So when a user types something in the filter textbox, if the result of the filtering is a count of zero items in the combo box, then whatever the user just typed in to the filtering textbox gets replaced with the previous value of that textbox. This also causes the TextChanged event to fire again which puts items back into the combo box based on the previous filter text. Otherwise, if there are items in the combo box, PreviousFilterText_T1 gets set to the value of the filter textbox.

This is fast and works very well for me.
 
Share this answer
 
Thank you. I do not want auto-complete because I want the filter to work even if the user type something from the middle of the string. The filtering works great. I just need to be able to validate when the user clicks outside the textbox.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900