本文介绍了如何通过同一用户控件上的按钮在表单中添加新的用户控件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友。

这是我的问题。我是c#的新手。

i有一个表格,一个用户控件和一个flowlayoutpanel。



如你所见,我在用户控件上有两个按钮。他们的名字是del并添加。





i希望当我点击用户控件上的删除按钮_同一个用户控件从我的flowlayoutpanel删除等等。



i想要点击添加按钮_一个用户控件(从它自己)添加到我的flowlayoutpanel等等。



这是我的代码删除并添加。

此代码在主窗体上添加按钮时工作。但是当我点击user_control上的添加按钮时,不工作。

如何纠正它。



((如何改变我的代码工作。我的问题是我的代码对于主窗体是正确的。

我的代码添加按钮:只在我的主窗体上工作。

如果我在user_control上使用此代码.this不要添加新的控件。

我应该更改我的代码?我的代码在哪里?在主表单或user_control表单上?))



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 ?))

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 :

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");
           }
       }

推荐答案

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:

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);





请注意,事件处理程序的匿名方法使代码更简单,更好地隔离。不公开用户控件的子控件,只暴露一些最小的属性和事件。



-SA



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


这篇关于如何通过同一用户控件上的按钮在表单中添加新的用户控件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 01:10
查看更多