Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hello friends.
this is my question.i am new in c#.
i have a form and a user control and a flowlayoutpanel.

As you see i have two buttons on user control.their name is del and add.
<img src="http://upload.tehran98.com/img1/fc0u5y01z9cuplx56zaj_thumb.jpg" border="0" alt="fc0u5y01z9cuplx56zaj.jpg" />

i want that when i click on delete button on user control _the same user control remove from my flowlayoutpanel and so on.

i want when i click add button_ one user control(from itself) add to my flowlayoutpanel and so on.

this is my code for delete and add .
this code work when add button is on the main form.but when i click on add button on user_control dont work.
how to correct it.

((how shoud i change my code to work .my problem is that my code is correct for main form.
my code add button:work just on my main form.
if i use this code in button on the user_control .this dont add new control.
what should i change my code?and where shoud i code? on main form or on the user_control form ?))

C#
private void button1_Click(object sender, EventArgs e)
      {
          counter++;
          if (counter == 10) { counter = 0; }
          UserControl1 btnAdd = new UserControl1();
          btnAdd.Name = "usercontrol" + counter.ToString();
          btnAdd.button1.Name = "click";
          btnAdd.button1.Click += new EventHandler(button2_Click);
        //  btnAdd.button1.BackColor = Color.Gray;
        //  btnAdd.button1.Text = "Add";
          flowLayoutPanel1.Controls.Add(btnAdd);
 
      }

delete button :
C#
private void button2_Click(object sender, EventArgs e)
       {
           Button btn = sender as Button;
 
           if (btn != null)
           {
               flowLayoutPanel1.Controls.RemoveByKey(btn.Parent.Name);
               this.Text = (btn.Name + " removeded");
           }
       }
Posted
Updated 26-Apr-13 10:08am
v4
Comments
[no name] 25-Apr-13 11:04am    
You would need to tell us what "dont work" means.
mahmoodof 25-Apr-13 11:12am    
hello
it means when i click on that .new user control does not add to the flow layout panel. my problem is that how should i change my code to work the same button on my user_control.
please explain with a sample.
[no name] 25-Apr-13 11:29am    
You need to debug your code. Is it really not adding your new user control? I think it is. If you want to use the the same code then there is a technique commonly used throughout the computer using industry, it's called copy/paste. But that is just a guess because what you wrote does not make a whole lot of sense.
mahmoodof 25-Apr-13 11:53am    
excuse me.my code has no bug.a friend said me below a min ago.
((You have 2 possibilites. Either handle the add in your control and add a new UserControl1 to the (parent) flowpanel there. Or call the add functionality from the main form.))
i am new in c#.
what does 'handle the add in your control' mean?
pls explain with a sample.
mahmoodof 25-Apr-13 13:09pm    
excuse me.i still have not given my reply.

1 solution

Your code is a pain to read. You should never use names like UserControl1, button1 or button2_Click — they violate (good) Microsoft naming conventions. I know, they are auto-generated. Never use such names, they are supposed to be renamed to something semantic.

You do some other bad things. First of all, you should not expose button1, it breaks good encapsulation. Don't use RemoveByKey, and, generally, never assume certain values of the property Name. This property is for designer, not for your code.

First of all, expose the button properly, not as a control, but only as the event; you can expose some properties, separately:

C#
class MyUserControl {
 
   Button myButton = new Button(); // private; keep it private
   
   internal MyUserControl() {
       //...
       myButton.Click = += (sender, eventArgs) => {
           if (MyButtonClick != null)
               MyButtonClick.Invoke(this, new System.EventArgs());
       }; //myButton.Click
   } //MyUserControl

   internal System.EventHandler MyButtonClick;

   internal string MyButtonName {
       get { return myButton.Name; }
       set { myButton.Name = value; } 
   } //MyButtonName

   // ...
   
} //class MyUserControl


Now, you can properly add the control instances:

C#
Panel parent = new Panel(); // could be flow panel, anything else...

MyUserControl first = new MyUserControl();
// now arrange it, give names to labels, button, etc.
first.MyButtonClick += (sender, EventArgs) => {
    MyUserControl second = new MyUserControl();
    // arrange second here... the location may depends on the location of already added controls
    parent.Controls.Add(second);
}; //first.MyButtonClick

first.Parent = parent; // same as parent.Controls.Add(first);


Note that anonymous methods for event handlers make the code much simpler and better isolated. Child controls of the user control are not exposed, only some minimal set of properties and events is exposed.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900