2
votes


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

4
I think you don't understand the idea behind User Controls to be honest. You can capture the event of the buttons provided you forward the event to the double click event of the control itself. Your reasons for wanting to do this is not exactly clear. I will just say its an odd decision.Security Hound
As I said, I want to do something similar to DataBindingNavigator, where each button can be moved or click.mj82

4 Answers

7
votes

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.

2
votes

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.

1
votes

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.

0
votes

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