如何在不同的WinForms中使用相同的用户控制器 [英] How to use same user controller in different WinForms

查看:101
本文介绍了如何在不同的WinForms中使用相同的用户控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件,并以两种不同的形式使用它.双击用户控制器时如何完成两种不同的操作.
例如:在Form1中,双击将打开一个表单(假设它为Form3).在Form2中,双击将导致打开Form4.

I have one user control & used it in two different forms. How do I get done two different actions when double click on the user controller.
for ex: In Form1 double click will opens a form(let''s say it Form3). In Form2 double clicking will causes opening of Form4.

推荐答案

create方法可通过提供当前表单名称来获取下一个表单.您可以使用this.Name查找当前表单名称,然后可以从数据库表中获取下一个表单数据.当您收到下一个表格名称时,需要打开;尝试使用以下代码打开下一页.
create method to get next form by giving current form name. You can use this.Name to find the current form name and next form data can be taken from database table. when you receive next form name need to be opened; try with below code to open next page.
public Form TryGetFormByName(string frmname)
{
    var formType = Assembly.GetExecutingAssembly().GetTypes()
        .Where(a => a.BaseType == typeof(Form) && a.Name == frmname)
        .FirstOrDefault();

    if (formType == null) // If there is no form with the given frmname
        return null;

    return (Form)Activator.CreateInstance(formType);
}


如注释中所述,您需要做的就是在用户控件上创建一个属性,然后每种表单都可以设置该属性,然后您的控件将知道任何内容
As mentioned in the comments, all you need to do is create a property on your user control and then each form can set that property and then your control will know whatever it is you need it to know.


您的问题没有任何意义.每次您将用户控件从工具箱中拖放到表单上时,都创建了该用户控件的单独实例,并将其添加到容器的控件集合中(例如表单,或将其放到窗体上的面板上.

您不会重复使用相同/相同的UserControl!

因此,在Form1中,打开拖放到Form1上的UserControl实例的属性/事件",然后双击DoubleClick Event:它将在Form1中为您编写EventHandler存根.在Form2中执行相同的操作.

当然,我们对您的UserControl的功能,其中的其他控件等一无所知.但是,让DoubleClick on UserControl本身做一些重要的事情",例如打开另一个控件,这是一个非常奇怪的设计决定.表格.

我建议您考虑在UserControl上放置一个Button,使其Text清楚地表明其功能,然后使用它来打开另一个Form.
Your question does not make sense. Each time you click-drag-drop a UserControl from the ToolBox onto a Form, you have created a separate instance of that UserControl, and it is added to the Controls Collection of the Container (like a Form, or a Panel on a Form) that you dropped it into.

You are not re-using the same/identical UserControl !

So, in Form1 you open the Properties/Events of the UserControl instance you drag-dropped onto Form1 and double-click on the DoubleClick Event: that writes the EventHandler stub for you in Form1. Do the same thing in Form2.

Of course we don''t know anything about the functions of your UserControl, the other Controls inside it, etc. But, it''s a rather strange design decision to have a DoubleClick on a UserControl itself do something "significant" like opening another Form.

I suggest you consider putting a Button on the UserControl with its Text clearly indicating what it does,and use that to open another Form.


这篇关于如何在不同的WinForms中使用相同的用户控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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