locked
usercontrol with 3 buttons RRS feed

  • Question

  • Hi

    I want to create a usercontrol whcih should contain 3 buttons, and I want to use it in all forms.

    Button inside th UC:(Prev, Home, Next)

    so, when any one of 3 buttons is clicked, how to know which button is clicked.

    any example/sample.... please



    Platform: windows mobile, WIndows Forms




    Regards
    Abinash

    Thursday, July 30, 2009 7:31 AM

Answers

  • Hi,

    find the solution:

    Add User Control in your project and add three button and write below code like :

    UserControl1 .cs
    public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
                
            }
    
            public event EventHandler OnUC_Click1 = null;
            public event EventHandler OnUC_Click2 = null;
            public event EventHandler OnUC_Click3 = null;
    
            private void button1_Click(object sender, EventArgs e)
            {
                OnUC_Click1(this, new EventArgs());
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                OnUC_Click2(this, new EventArgs());
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                OnUC_Click3(this, new EventArgs());
            }       
        }


    Now you have to add new form where you want to add this user control. here i have added form2


    Form2.Cs
    public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
    
                UserControl1 UI = new UserControl1();
                UI.Bounds = new Rectangle(0, 100, 150, 150);
                UI.OnUC_Click1 += new EventHandler(UI_OnUC_Click1);
                UI.OnUC_Click2 += new EventHandler(UI_OnUC_Click2);
                UI.OnUC_Click3 += new EventHandler(UI_OnUC_Click3);
                this.Controls.Add(UI);
            }
    
            void UI_OnUC_Click3(object sender, EventArgs e)
            {
                MessageBox.Show("3");
            }
    
            void UI_OnUC_Click2(object sender, EventArgs e)
            {
                MessageBox.Show("2");
            }
    
            void UI_OnUC_Click1(object sender, EventArgs e)
            {
                MessageBox.Show("1");
            }
        }


    Hope this may be help.



    Pavan Pareta | If you find my post is helpful for you then mark as a answer.
    • Proposed as answer by WmDev Thursday, July 30, 2009 8:30 AM
    • Marked as answer by Abinash Tumulu Thursday, July 30, 2009 8:48 AM
    Thursday, July 30, 2009 8:29 AM

All replies

  • Abinash,

    While building user control, you can write individual button events and code. So that when you place the user control on form and click, it goes to the control and pick the event code.

    Please let me know if you have any problem to use the above aproach.

    Regards,
    Malleswar
    Thursday, July 30, 2009 7:55 AM
  • Hi,

    find the solution:

    Add User Control in your project and add three button and write below code like :

    UserControl1 .cs
    public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
                
            }
    
            public event EventHandler OnUC_Click1 = null;
            public event EventHandler OnUC_Click2 = null;
            public event EventHandler OnUC_Click3 = null;
    
            private void button1_Click(object sender, EventArgs e)
            {
                OnUC_Click1(this, new EventArgs());
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                OnUC_Click2(this, new EventArgs());
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                OnUC_Click3(this, new EventArgs());
            }       
        }


    Now you have to add new form where you want to add this user control. here i have added form2


    Form2.Cs
    public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
    
                UserControl1 UI = new UserControl1();
                UI.Bounds = new Rectangle(0, 100, 150, 150);
                UI.OnUC_Click1 += new EventHandler(UI_OnUC_Click1);
                UI.OnUC_Click2 += new EventHandler(UI_OnUC_Click2);
                UI.OnUC_Click3 += new EventHandler(UI_OnUC_Click3);
                this.Controls.Add(UI);
            }
    
            void UI_OnUC_Click3(object sender, EventArgs e)
            {
                MessageBox.Show("3");
            }
    
            void UI_OnUC_Click2(object sender, EventArgs e)
            {
                MessageBox.Show("2");
            }
    
            void UI_OnUC_Click1(object sender, EventArgs e)
            {
                MessageBox.Show("1");
            }
        }


    Hope this may be help.



    Pavan Pareta | If you find my post is helpful for you then mark as a answer.
    • Proposed as answer by WmDev Thursday, July 30, 2009 8:30 AM
    • Marked as answer by Abinash Tumulu Thursday, July 30, 2009 8:48 AM
    Thursday, July 30, 2009 8:29 AM
  • Hi malleswar... thanks for replaying all my questions...
    Thursday, July 30, 2009 8:48 AM
  • Thanks WmDev, it really worked....
    Thursday, July 30, 2009 8:49 AM