无法导致文本框验证事件 [英] Cannot cause textbox validating event

查看:103
本文介绍了无法导致文本框验证事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Textbox和ComboBox的WinForms表单以及其他几个控件。用户可以在文本框中键入搜索字符串,然后根据搜索字符串过滤组合框。 Filtering方法触发Textbox的Text Changed事件。



当用户在文本框中键入文本时,组合框的DroppedDown属性设置为true组合框打开,用户可以看到过滤后的项目。



这一切都很有效。





用户可以输入一个过滤字符串,导致组合框为空。这就是问题所在。组合框中必须至少有1个有效项。所以,我想做的是:



如果文本框失去焦点,我想使用验证事件检查是否至少有一个项目组合框。如果没有,请提醒用户并取消,以便焦点保留在文本框中。



如果我点击包含文本框的表单并且该组合框中没有任何项目,问题就是发生了一些奇怪的事情。我在启动我正在处理的表单的父表单上收到一条非描述性的错误消息。



 QueryDesignerTool qdTool = new QueryDesignerTool(); 
//将此移动到QueryDesignerTool_OnLoad函数。 ens 01/28/2015
//this.Hide();
DialogResult dr = qdTool.ShowDialog(this);





我得到

未处理的类型异常System.Windows.Forms.dll中发生'System.ArgumentOutOfRangeException'

附加信息:InvalidArgument ='0'的值对'index'无效。





当代码运行时,如果我检查qdTool并查看有问题的组合框,我可以看到组合框的SelectedIndex是0,项目数是0,这是非法的。



我不确定为什么它出错并退回到父表格。但这不是我寻求帮助的原因。



如果用户点击它,我感兴趣的是验证文本框中的文本。我已经读过焦点无法传递给表单,因此Leave Focus事件不会触发,因此如果焦点位于文本框上然后用户单击表单,则不会触发Validating事件。



那么,正确的做法是抓住表单的click事件并处理该文本框的验证吗?



我尝试了什么:



我已经描述了我拥有的东西在问题描述中试过。

解决方案



我认为你的要求是带有自动完成功能的Combobox:

请查看以下链接:

Autocomplete ComboBox c#vb.net [ ^ ]



我希望这会有所帮助..


要解决表单错误,在过滤ComboBox之前,将SelectedIndex属性设置为-1。执行SuspendLayout,将SelectedIndex属性设置为-1,Filetre the items,然后根据需要重置SelectedIndex。最后,调用ResumeLayout来重绘组合。



我会在TextBox.Leave事件中执行验证,只要Textbox失去焦点就会触发。


我解决了这个问题。我没有尝试验证用户在过滤器文本框外部点击后输入的内容,而是决定以不同的方式处理它。



 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;
}
}





我创建了一个名为PreviousFilterText_T1的模块级字符串变量。表单加载时,这将设置为空字符串。因此,当用户在过滤器文本框中键入内容时,如果过滤的结果是组合框中的零项目计数,则用户刚刚输入到过滤文本框中的任何内容都将替换为该文本框的先前值。这也会导致TextChanged事件再次触发,这会根据先前的过滤器文本将项目放回到组合框中。否则,如果组合框中有项目,则PreviousFilterText_T1将设置为过滤器文本框的值。



这对我来说速度很快,效果很好。


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.

解决方案

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

I hope this will help..


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.


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.


这篇关于无法导致文本框验证事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆