从Window UserControl调用窗口表单 [英] Calling a window form from a Window UserControl

查看:80
本文介绍了从Window UserControl调用窗口表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从UserControl中打开一个窗口表单.是否有可能...我尝试过,但未列出任何表单...!

例如:
1)我创建了一个userControl1
2)我有一个windowForm1
3)现在,我必须从userControl1的文本框keyDown事件中打开一个windowForm1.

我该怎么办...?

请帮助...!

I to open a Window Form, from a UserControl.. is it possible any ways... i tried but it is not listing me any of the forms...!

eg:
1) I have created a userControl1
2) I have a windowForm1
3) NOW, I have to open a windowForm1 from userControl1''s textbox keyDown event.

How can i do that...?

Please HELP...!

推荐答案

作为示例,请使用(大致)以下内容:

For your example use (roughly) the following :

//Check what the exact arguments for this method should be but you get the idea
void OnKeyDown(object sender, EventArgs e)
{
    //Create an instance of your form to open
    windowForm1 TheForm = new windowForm1();
    //Now show it
    TheForm.Show(); //(alternatively TheForm.ShowDialog(); depending on your desired result
    //Your form should now be showing
}



这是您从任何事件中打开新表单所需要做的全部工作.不过请务必小心-如果用户只是按下Enter键,此代码将产生大量打开的窗口....我建议使用更好的代码是:



This is all you should have to do to open a new form from any event. Be very careful though - this code will produce a massive number of windows open if a user just holds down the enter key say... I would suggest better code would be:

//Create an instance of your form to open - outside the event so that it can be reused.
windowForm1 TheForm = new windowForm1();
//Store whether the form has already been shown
bool TheForm_Shown = false;
//Check what the exact arguments for this method should be but you get the idea
void OnKeyDown(object sender, EventArgs e)
{
    //Check to see if the form is already open or not
    if(TheForm_Shown)
    {
        TheForm.Close();
    }
    //Set the check to true as the form is now showing - set it before so that even if showing the form fails there isn't suddenly a mass of errors! 
    TheForm_Shown = true;
    //Now show it
    TheForm.Show(); //(alternatively TheForm.ShowDialog(); depending on your desired result
    //Your form should now be showing
}



希望这会有所帮助,
Ed



Hope this helps,
Ed


没有诸如打开"表格这样的动词.您只需使用构造函数创建它,然后使用System.Windows.Forms.Form.Show http:对其进行显示: //msdn.microsoft.com/zh-CN/library/system.windows.forms.form.aspx [ http://msdn.microsoft.com/zh-CN/library/system.windows.forms.control.show.aspx [TextBoxKeyDown"之类的事件,因为将对文本框的引用公开是一种不好的做法.在用户控件的代码内部对文本框事件KeyDown的事件处理程序进行硬编码会更加糟糕.如果您在用户控件之外处理事件,则可以在其他上下文中进行操作,在该上下文中您可能已经有要显示的表单实例或可以在其中创建表单的实例.

为了更好地理解,请阅读有关松散耦合的信息:
http://en.wikipedia.org/wiki/Loose_coupling [ ^ ].

通常,在文本框中单击显示表单看起来像一个不良的UI设计.

—SA
There is no such verb as "open" a form. You just create it with a constructor and then show it with System.Windows.Forms.Form.Show, http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[^], http://msdn.microsoft.com/en-us/library/system.windows.forms.control.show.aspx[^].

To use your user control on some other form, you need to add to your user control and expose some event like "TextBoxKeyDown", because exposing the reference to the text box itself would be a bad practice; and hard-coding the event handler of your text box event KeyDown internally in the code of the user control would be even worse. If you handle the event outside the user control, you do it some other context where you may already have an instance of the form to be shown or where you can create one.

For better understanding, please read about loose coupling: http://en.wikipedia.org/wiki/Loose_coupling[^].

Generally, showing a form on a click in a text box looks like an ill UI design.

—SA


这篇关于从Window UserControl调用窗口表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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