C#表单设计问题 [英] C# Form Desgning Issue

查看:89
本文介绍了C#表单设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含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.

Can we write any code that will identify the current control and change the backcolor to yellow, and all other to white.


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.

推荐答案

您确实可以让所有控件在获得焦点时都调用同一事件.您可以使用基本Control类设置颜色等.只需将事件的发送方强制转换为Control.

You can indeed make all your controls call the same event when they get the focus. You can set colors and so on using the base Control class. Just cast the sender on the event to type Control.

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.


这是最简单的示例之一,它清楚地展示了使用设计器.

当然,设计器有助于创建基本布局并快速查看其外观.创建一个快速的临时应用程序也足够了.但是,最好是取悦那些虚弱的人,并产生错误的幻想,即创建UI很容易.是的,创建垃圾质量的UI可能很容易,但是即使那样简单,UI也很小.即使是简单的形式,只有一个复杂的控件(太多的控件),设计器也使它变得无聊的重复性工作,其结果对于维护和错误容易产生不好的结果,修复成本很高.

那么,如果不是设计师,那又是什么呢?仍然使用Designer,但仅用于描绘主要的布局元素:添加主菜单,然后添加状态栏,然后添加一个中间带有Dock.Fill的面板.在主面板上放置尽可能多的面板,并确保为每个功能控件组定义不同的父面板.始终在任何地方使用System.Windows.Forms.Control.DockSystem.Windows.Forms.Control.Padding.在开发的这个阶段,绝对不要让自己有任何绝对的位置或绝对的大小.

而已.现在,在代码中添加所有 控件.使用类似的内容:
This is one of the simplest example which brightly demonstrates how bad is to use the Designer.

Of course, the Designer helps to create a basic layout and quickly see how it looks. It also is good enough to create a quick ad-hoc application. But it is even better to please the weak-minded and create a wrong illusion that creation of UI is easy. Yes, creation of trash-quality UI could be easy, but even that is easy of the UI is very small. Even in a simple form with only one complication — too many controls — the Designer makes it a boring repetitive work, with the results very bad for maintenance and error prone, with extremely high cost of a fix.

So, if not a Designer, than what? Still use the Designer, but only to depict main layout elements: add main menu, then status bar, and then one panel with Dock.Fill in between. Put as many panels inside on the main panel, as well as to make sure you define distinct parent panel for each functional group of controls. Always use System.Windows.Forms.Control.Dock and System.Windows.Forms.Control.Padding everywhere. On this stage of development, never allow yourself any absolute positioning or absolute sizes.

That''s it. Now, add all the multitude of controls in code. Use something like:
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.GotFocusLostFocus:



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 [ ^ ].

—SA



All reading you need so far is here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].

—SA


这篇关于C#表单设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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