从WindowsFormProject设置UserControl属性 [英] Set UserControl Properties from WindowsFormProject

查看:96
本文介绍了从WindowsFormProject设置UserControl属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在仅包含MenuStrips控件的UserControlProject中,我有两个属性,一个是SourceForm,另一个是DestinationForm:

Hi All,

In my UserControlProject, in which just contains MenuStrips Controls, I have two properties, one is SourceForm, and another one is DestinationForm:

private Form SourceForm = new Form();
public Form _SourceForm
{
    get { return SourceForm; }
    set { SourceForm = value; }
}

private Form DestinationForm = new Form();

public Form _DestinationForm
{
     get { return DestinationForm; }
     set { DestinationForm = value; }
}


现在,一旦我单击了在WindowsFormApplication中拖放的MenuItems之一,就在MenuStrip中,如下代码所示,理想情况下,它应该导航到DestinationForm.


Now once I clicked the one of the MenuItems, which is dragged and dropped in WindowsFormApplication, in the MenuStrip as the code show below, ideally it suppose to navigate to the DestinationForm.

private void tsmiExecuteSql_Click(object sender, EventArgs e)
{
      MenuNavigation.ShowForm(SourceForm, DestinationForm);
}


现在我的问题是我只能在WindowsFormProject的Form_Load事件中设置SourceForm和DestinationForm,如下所示:


Now my issue was i can only set the SourceForm and DestinationForm in WindowsFormProject''s Form_Load Event like this:

private void frm1_Load(object sender, EventArgs e)
{
   ucMenuStrip._SourceForm = this;
   ucMenuStrip._DestinationForm = new frm2();
}

,而MenuItems仅在目标窗体上执行Form_Load事件,而不在源窗体上执行,因此,我无法重新设置DestinationForm属性,更糟糕的是,UserControlProject上的事件无法访问WindowsFormProject中的窗体,因此,我也无法在UserControlProject的MenuStripItem单击事件中指定SourceForm和DestinationForm.

为了缩短线程长度,我真正想知道的是,一旦MenuStripItems单击WindowsFormProject,如何重新设置UserControlProject的SourceForm和DestinationForm?

and the MenuItems only execute the Form_Load event on the destination form, not the source form, hence there is no way for me to re-set the DestinationForm property, and to make things even worse, the event on the UserControlProject does not have the access to the Forms in the WindowsFormProject, therefore, i can not specifiy the SourceForm and DestinationForm in the UserControlProject''s MenuStripItem click events either.

To cut the thread short, really what i would like to know is how to re-set the UserControlProject''s SourceForm and DestinationForm once MenuStripItems clicked on WindowsFormProject?

推荐答案

首先要了解的是:没有什么是非动态的".不仅所有属性都是动态地"分配的,所有对象本身都是被创建并动态地"插入到窗体中的,窗体本身和应用程序也就是在运行时插入的.没有别的办法了.如果您认为设计人员执行此操作是另外一种神秘的方式,请再考虑一下.设计器只是生成某些源代码的一种方法,但是如果您手动"编写该代码,则在运行时将按照正确的方式调用该代码.

就是说,这是一种回答所有此类问题的通用方法.自动生成的代码(在Designer的帮助下)被写入一个单独的文件中.特别是,请查看自动生成的方法InitializeComponent的实现,以了解如何设置所有UI对象及其属性.在您的代码中使用这些知识.

就这样.

—SA
First thing to understand: there is nothing with is "not dynamic". Not only all the properties are assigned "dynamically", all the objects themselves are created and inserted in the Forms "dynamically" as well as the Forms themselves and the Application, that is, during run-time. There is no other way. If you thing the Designer does it is some other mystical way, think again. The Designer is only a way to generate some source code, but that code is called during run-time exactly the say way if you would write it "manually".

That said, this is one universal way to answer all questions like that. Your auto-generated (with the help of the Designer) code is written in a separate file. In particular, look at the implementation of the auto-generated method InitializeComponent to learn how all the UI objects and their properties are set up. Use this knowledge in your code.

That''s it.

—SA


这篇关于从WindowsFormProject设置UserControl属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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