简体   繁体   English

如何从标准控件在 C# 中创建用户控件?

[英]How to create User Control in C# from standard controls?


I want to create in C# Visual Studio 2010 my user control from standard controls, which remains independent during design time.我想在 C# Visual Studio 2010 中从标准控件创建我的用户控件,该控件在设计时保持独立
Problem is, when I create standard user control like this:问题是,当我像这样创建标准用户控件时:

public partial class MyUserControl: UserControl 
{
    public MyUserControl()
    {
        InitializeComponent();
    }
}

and I add 2 buttons and 1 textbox on it, they become a part of UserControl.我在上面添加了 2 个按钮和 1 个文本框,它们成为 UserControl 的一部分。
Then, when control is designed and I add it on a form, I can move it, resize etc., but I cannot click or select Button1 or Button2 or TextBox1.然后,当设计控件并将其添加到窗体上时,我可以移动它、调整大小等,但我无法单击或 select Button1 或 Button2 或 TextBox1。
Also double clicking button1 do not create button1_click event but UserControl_Load event.同样双击 button1 不会创建 button1_click 事件,而是创建 UserControl_Load 事件。
I would like to achieve effect similar to DataBindingNavigator control - where you can click each button independently.我想实现类似于 DataBindingNavigator 控件的效果-您可以在其中独立单击每个按钮。

I know I can make a public property setting/returning user control Buttons or Textbox, but they'll appear in designer, there will be no possibility to select them.我知道我可以进行公共属性设置/返回用户控件按钮或文本框,但它们会出现在设计器中,select 将不可能。

Do you think it is possible to do with standard controls?您认为可以使用标准控件吗? If so, how to do that?如果是这样,该怎么做?
Best regards,此致,
mj82 mj82

You cannot do this because it does not make sense to do it.你不能这样做,因为这样做没有意义。

Suppose you could do what you ask - then on the form you would be able to move the user control buttons around etc - essentially designing the user control on the from surface itself.假设您可以按照您的要求进行操作 - 然后在表单上您可以移动用户控件按钮等 - 本质上是在 from 表面本身上设计用户控件。 OK, but what about all the other forms that also have that user control?好的,但是所有其他也具有该用户控制权的 forms 呢? That is why once you drop the user control it is fixed.这就是为什么一旦你放弃用户控制它是固定的。 You do not design a user control per form instance, you design the control and then it is the same for every form.您不会为每个表单实例设计一个用户控件,而是设计控件,然后每个表单都相同。

Same with clicking on the button.与单击按钮相同。 You want to create a click event handler but based on what you are asking for, that would happen in the form.您想创建一个单击事件处理程序,但根据您的要求,这将在表单中发生。 But the button is not on your form, it is on your user control, it's click event has to be handled in the user control, not the parent form.但是按钮不在您的表单上,它在您的用户控件上,它的点击事件必须在用户控件中处理,而不是在父表单中。

The idea behind user controls is to NOT have to do what you describe.用户控件背后的想法是不必按照您的描述进行操作。 They are pre-assembled building blocks.它们是预组装的构建块。

The DataBindinghNavigator is nothing more than a toolstrip. DataBindinghNavigator 只不过是一个工具条。 If you want that behavior, just use a tool strip.如果您想要这种行为,只需使用工具条。

Normally, you would put your handlers for control events within your UserControl.通常,您会将控制事件的处理程序放在 UserControl 中。 If your UserControl needs to notify the page of certain events, then add those events to your UserControl.如果您的 UserControl 需要通知页面某些事件,则将这些事件添加到您的 UserControl。

If you want separate controls that fire events independently on your page, then those controls should probably not be part of a UserControl.如果您想要在页面上独立触发事件的单独控件,那么这些控件可能不应该是 UserControl 的一部分。

What you need to do is create event triggers in your control, and then event handlers in your form to process actions when your 'button_1' is clicked etc... You cannot create the events from the form design view as that is something that should be built into the control.您需要做的是在您的控件中创建事件触发器,然后在您的表单中创建事件处理程序以在单击“button_1”时处理操作等......您无法从表单设计视图创建事件,因为这是应该的内置到控件中。

If you went so far that you are in need to dynamically manipulate user interface with your code, you should try look into MVC.如果您到目前为止需要使用代码动态操作用户界面,您应该尝试查看 MVC。 http://www.asp.net/mvc http://www.asp.net/mvc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户控件大于标准 c# - user control is larger than standard c# 如何在C#中为动态创建的用户控件创建单击事件? - How to create click event for dynamically created user controls in C#? 从现有控件创建用户控件 - Create User Control from existing controls 如何在C#/ Silverlight中创建用户控件数组/为用户控件建立索引? - How to create an array of User Controls / indexing user controls in C# / Silverlight? 如何从c / cpp创建C#自定义控件 - How to create C# custom controls from c/cpp 如何在C#中创建动态服务器控件,以及如何将更多记录从数据库追加到转发器控件,而无需再次加载全部 - How to create dynamic server controls in C# and how to append more records from the database to repeater control without loading all once again 如何通过清单 <string> 从窗体到C#中的用户控件 - How to pass a List<string> from Form to user controls in C# 从动态加载的Web用户控件(asp.net C#)中的控件访问值 - Accessing value from the controls within a dynamically loaded web user control (asp.net c#) 从用户控制页面访问MainWindow的控件。 WPF C# - Accessing MainWindow's controls from a user control page. WPF C# 如何从C#中的面板控件中Dispose()一个特定的User控件? - How to Dispose() a specific User control from panel control in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM