如何通过同一用户控件上的按钮在表单中添加新的用户控件。 [英] how add a new user control in a form by a button on the same user control.

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

问题描述

你好朋友。

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

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



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

<img src =http://upload.tehran98.com/img1/fc0u5y01z9cuplx56zaj_thumb.jpgborder =0alt =fc0u5y01z9cuplx56zaj.jpg/>



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

推荐答案

您的代码难以阅读。你永远不应该使用 UserControl1 button1 button2_Click 等名称 - 他们违反(良好)Microsoft命名约定。我知道,它们是自动生成的。永远不要使用这些名称,它们应该被重命名为语义。



你做其他一些坏事。首先,你不应该公开 button1 ,它打破了良好的封装。不要使用 RemoveByKey ,并且通常不会假设属性的某些值 Name 。此属性适用于设计师,而不适用于您的代码。



首先,正确显示按钮,而不是作为控件,但仅作为事件;你可以单独公开一些属性:



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:

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


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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆