问题描述
我有一个包含15个文本框和3个组合框的表单.
用户可以一个接一个地按Enter键来浏览此控件.光标正在从一个控件移到另一个控件.
现在,我正在做的是在输入事件时更改控件的背景色,以帮助用户识别当前控件.它工作得很好.
但是我想使这个过程自动化.现在,我需要在每18个控件的enter事件中编写代码.
我无法编写单个eventa处理程序并选择所有其他控件,因为某些控件在该事件上编写了一些代码.像comboBox1.DroppedDown == true;在一个例子中.
请帮助我.
I have a form which have 15 text box and 3 combo Box.
User can navigate through this controls by pressing enter key one after another. Cursor is moving from one to another control.
Now what I am doing is changing the backcolor of the control at enter event to help the user to identify the current control. Its working perfectly.
But I want to automate this process. Now I need to write the code in every 18 control''s enter event.
I can''t write a a single eventa handler and choose it all other control, because some control is having some code written on the event. like comboBox1.DroppedDown==true; in an example.
please please help me.
推荐答案
private void OnFocus(object sender, EventArgs ea)
{
Control c = sender as Control;
if (c != null) // this makes sure sender was a control, but it has to be
{
// set the properties of c here, it is the control you just focused on.
}
}
您可以将焦点事件连接到已经设置了焦点的控件上,它们只会被调用.您可以使用焦点丢失事件将在上面的代码中设置的属性改回默认值.
You can hook up the focus event to controls that already have it set, they will just both be called. You can use the focus lost event to change back the properties you set in the above code to the default.
Control control = new Button();
control.Left = //...
control.Top = //... calculate in loop with some step (if using vertical layout, for example)
//alternatively:
AddSpacer(); // add some panel to space between control in the array of controls
control.Dock = DockStyle.Top; // or something
control.Parent = someParentPanel; // or group box, or something -- this is where you insert
//alternatively, same as
someParentPanel.Controls.Add(control);
control.Click += (sender, eventArgs) => { SomeMethodToBeCalledOnClick(); }; // this is how you handle events
//...
也许您不了解如何编写此类代码的足够详细信息.这是我的建议:在Designer中做一部分工作,只是尝试一下.编译并运行此类研究"代码.如果您需要这样做,请查看自动生成的文本以学习如何编写.
关于闪烁光标"的说明:
实际上,这是控件具有输入焦点的状态.您可以使用事件System.Windows.Forms.Control.GotFocus
和LostFocus
:
Perhaps you don''t know enough detail on how to write such code. Here is my advice: do a part of this work in Designer, just to try it. Compile and run such "research" code. If this is what you need, take a look at auto-generated text to learn how to write it.
On the clarification about "blinking cursor":
Actually, this is the state when a control has input focus. You can work with the events System.Windows.Forms.Control.GotFocus
and LostFocus
:
control.GotFocus() += (sender, eventArgs) => {
/* change color or other properties here to show the control as focused ... */
};
control.Focus() += (sender, eventArgs) => {
/* change color or other properties here to show it unfocused ... */
};
到目前为止,您需要的所有阅读资料都在这里:
http://msdn.microsoft.com/en-us/library/system. windows.forms.control.aspx [ ^ ].
All reading you need so far is here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].
这篇关于C#表单设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!