简体   繁体   English

C# 如何从 2nd Form 以 1st Form 创建 object

[英]C# how to create object in 1st Form from 2nd Form

I'm not clear how to create an object (Attribute: panel with label and text box inside) in the 1st Form (Main form) from 2nd Form.我不清楚如何从第 2 形式在第 1 形式(主形式)中创建 object (属性:带有 label 和内部文本框的面板)。

  1. 1st Form has a panel in which Attribute should be created.第一种形式有一个面板,应在其中创建属性
  2. 1st Form also has a Button which opens the 2nd Form.第一种形式也有一个打开第二种形式的按钮
  3. 2nd Form takes roles of creation & configuration of Attribute 2nd Form 负责创建和配置属性
  4. 2nd Form has Button -"Create".第二种形式有按钮- “创建”。 When clicked it should create an attribute in the 1st Form and insert it into Panel of Attributes .单击时,它应该在 1st Form 中创建一个属性并将其插入到Panel of Attributes中。

Could you please provide me with some examples how It would be possible to perform such action?您能否提供一些示例如何执行此类操作?

主表格 第二种形式

Why not let Form1 create the control for itself ?为什么不让Form1自己创建控件呢?

   public partial class Form1 : Form {

     public void CreateMyControl() {
       Panel attrPanel = new Panel() {
         Parent = this,
         Size = new Size(100, 60),   //TODO: Put the right value here
         Location = new Point(0, 0), //TODO: Put the right value here 
       };  

       new Label() {
         Parent   = attrPanel,         
         Text     = "I'm the Label", //TODO: Put the right value here
         Location = new Point(4, 4)  //TODO: Put the right value here
       };

       new TextBox() {
         Parent   = attrPanel, 
         Text     = "I'm the TextBox", //TODO: Put the right value here
         Location = new Point(4, 34)   //TODO: Put the right value here
       }    
    }

    private void btnRun_Click(object sender, EventArgs e) {
      // We create Form2 instance and pass current Form1 instance to it    
      Form2 form2 = new Form2(this);

      form2.ShowDialog(); // Or Show
    }

Having done with Form1 , let's pass Form1 to via constructor完成Form1后,让我们通过构造函数将Form1传递给

public partial class Form2 : Form {
  ...

  public Form1 ParentForm {get;} = null;

  public Form2() {
    InitializeComponent();
  }

  public Form2(Form1 parentForm) : this() {
    ParentForm = parentForm;
  }

  private void btnCreateControl_Click(object sender, EventArgs e) {
    // If we have parent form, create some controls on it
    if (ParentForm != null)
      ParentForm.CreateMyControl(); 
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过单击第一个表单中的按钮,在第二个TabPage中创建DataGridView - How can I create a DataGridView in a TabPage of 2nd from by clicking the button in the 1st Form 将所选项目从第二形式的列表框传递到第一形式的列表框 - Passing selected item from a listbox in 2nd form to listbox in 1st form 如何从第二形式填充列表对象 - How to populate list object from 2nd form C#线程化,第一个任务DoSomething,第二个任务更新WinFormLabel(实时)? - C# Threading, 1st Task DoSomething, 2nd Task update a WinFormLabel (Realtime)? 使用参数启动ac#程序的第二个实例,程序的第一个实例使用该参数 - Start 2nd instance of a c# program with a parameter, which the 1st instance of the program uses 同时发送两个请求 第一个请求使用 C# WebClient 和第二个请求使用 Ajax - Send two request simultaneously 1st request with C# WebClient and 2nd request with Ajax 从在第一个窗口中创建的对象中的事件更新第二个窗口控件 - Updating a 2nd window control from events in an object created in the 1st window WPF-从第一个窗口控制第二个窗口 - WPF - Control 2nd window from 1st window 如何比较第二个结果和第一个结果 - How to compare 2nd result to 1st result 如何将数据从第一张表导入同一数据库的第二张表? - How can I import data from 1st table to a 2nd table on the same database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM