简体   繁体   中英

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.
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.
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.
Also double clicking button1 do not create button1_click event but UserControl_Load event.
I would like to achieve effect similar to DataBindingNavigator control - where you can click each button independently.

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.

Do you think it is possible to do with standard controls? If so, how to do that?
Best regards,
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. OK, but what about all the other forms that also have that user control? 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. If you want that behavior, just use a tool strip.

Normally, you would put your handlers for control events within your UserControl. If your UserControl needs to notify the page of certain events, then add those events to your UserControl.

If you want separate controls that fire events independently on your page, then those controls should probably not be part of a 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.

If you went so far that you are in need to dynamically manipulate user interface with your code, you should try look into MVC. http://www.asp.net/mvc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM