在其他类中使用文本框之类的对象 [英] Use objects like textboxes in other classes

查看:68
本文介绍了在其他类中使用文本框之类的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的表单应用程序中,我使用多个文本框和按钮.我想重用它们,但是应用程序一遍又一遍地失败了.我以为是这样的:

窗口TestWindow = new window();

因此,如何在其他课程中重用我的文本框? (我只是从C#开始,所以希望您能为我提供帮助:-))

Hi,

In my form application I use multiple textboxes and buttons. I want to reuse them but the application failed over and over. I thought it was something like:

window TestWindow = new window();

So, how can I reuse my textboxes in my other classes? (I am just starting with C#, so hopefully you can help me :-))

推荐答案

WPF中有一个窗口"类,但是由于您正在谈论关于WinForms,我假设您不想使用它!

尚不清楚您要做什么,但我认为:
1)您创建了带有多个文本框和按钮的表单.
2)您想要显示此表单的多个副本,以便用户可以分别输入或查看不同的信息.

如果是这样,那就非常简单:每次您想要显示表单的新副本时,只需执行以下操作:
There is a "window" class in WPF, but since you are talking about WinForms, I assume that you don''t want to use that!

It''s not that clear what you are trying to do, but I think that:
1) You have created a form with multiple textboxes and buttons.
2) You want to show several copies of this form, so that the user can enter or view different information with each.

If so, then it is fairly simple: each time you want to display a new copy of your form, just do:
MyNewForm form = new MyNewForm();
// Set any properties the form needs.
form.Show();

我还建议包括一个Closed事件处理程序是一个好主意,以便您可以在需要时检索任何新信息.

I would also suggest that it would be a good idea to include a Closed event handler so that you can retrieve any new information if you need to.

MyNewForm form = new MyNewForm();
form.FormClosed += new FormClosedEventHandler(MyNewForm_FormClosed);
// Set any properties the form needs.
form.Show();





void MyNewForm_FormClosed(object sender, FormClosedEventArgs e)
   {
   MyNewForm form = sender as MyNewForm;
   if (form != null)
      {
      // Get any information the user has entered.
      }
   }


如果您具有以多种格式以相同格式/排列使用的控件(如文本框和单选按钮),则可以创建一个包含这些控件的UserControl,然后以需要这些控件的任何形式包含用户控件.

如果要动态创建表单中的控件,也可以执行此操作,但是这样做会有点困难,因为您需要管理代码中的所有创建和初始化操作,而不是依赖设计器来完成.在这种情况下,我可能会创建一个类库来为我处理所有内容,从而使程序员可以传递位置,初始设置,事件处理之类的参数.
由于陈述的方式很难回答您的问题.
If you have controls (like textboxes and radio buttons) that are used in the same format/arrangment in multiple forms, you can create a UserControl that contains those controls, and then include the user control in any form that needs those controls.

If you want to dynamically crerate controls in a form, you can do that too, but it gets a little tougher because you need to manage ALL of the creation and initializeation in code instead of relying on the designer to do it for you. In that case, I''d probably create a class library that handles all that stuff for me allowing the programmer to pass in parameters for things like position, initial settings, event handling, and stuff like that.

It''s hard to answer your question because of the way it was stated.


这篇关于在其他类中使用文本框之类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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