2016-10-11 3 views
0

Mit C#, ich habe ziehen und legte eine Schaltfläche mit Windows Forms und erstellt eine Schaltfläche und Textfeld dynamisch. Ich möchte die Eingabe aus dem Textfeld auf der Schaltfläche anzeigen. Die hartcodierte Schaltfläche funktioniert einwandfrei, aber die dynamische Schaltfläche stürzt das Programm mit einer System.NullReferenceException ab.Dynamisches Textfeld Eingabe mit C#

Bitte helfen Sie mir zu lernen, wie Sie Eingaben aus einer dynamisch erstellten Textbox mit dem Klick auf eine dynamisch erstellte Schaltfläche erhalten.

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace WindowsFormsApplication8 
{ 
public partial class Form1 : Form 
{ 
    Button btnDynamic; 
    TextBox txtBoxYear; 
    public Form1() 
    { 
     InitializeComponent(); 
     tp(); 

    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     Button btn = (Button)sender; 
     btnHardCode.Text = txtBoxYear.Text;  //This hard coded button works 
     // btnDynamic.Text = txtBoxYear.Text; //The dynamic button crashes the program with a 
              //System.NullReferenceException 
    } 
    private void tabPage1_Click(object sender, EventArgs e) 
    { 

    } 

    private void tp() //put textbox and button on tab on tab click 
    { 
     var tab = tabControl1.TabPages[0]; 
     //tab.Controls.Clear(); 
     var btnDynamic = new Button() 
     { 
      Size = new Size(75, 30), 
      Left = 40, 
      Top = 50, 
      Text = "try", 
     }; 
     btnDynamic.Click += new EventHandler(this.Form1_Load); 
     tab.Controls.Add(btnDynamic); 
     txtBoxYear = new TextBox() 
     { 
      Size = new Size(200, 100), 
      Left = 20, 
      Top = 10, 
     }; 
     tab.Controls.Add(txtBoxYear); 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 

    } 
} 

}

Antwort

0

habe ich herausgefunden, wie die Eingabe mit einem Klick auf eine dynamisch erstellte Schaltfläche aus einem dynamisch erstellten Textfeld zu bekommen. Deklarieren Sie zuerst den Namen der Schaltfläche auf Klassenebene. Zweitens: Wenn Sie das Schaltflächenobjekt instanziieren, verwenden Sie nicht wie oben das Schlüsselwort var. Instantiate es wie folgt aus: btnDynamic = new Button()

Hier ist die Funktion Code:

using System; 
using System.Drawing; 
using System.Windows.Forms; 

namespace dynamicButton 
{ 
    public partial class Form1 : Form 
    { 
     TextBox txtBoxYear; 
     Button btnDynamic; 

     public Form1() 
     { 
      InitializeComponent(); 
      tp(); 
     } 


     private void Form1_Load(object sender, EventArgs e) 
     { 
       Button btn = (Button)sender; 
       btnHardCode.Text = txtBoxYear.Text; 
       btnDynamic.Text = txtBoxYear.Text;   
     } 
     private void tabPage1_Click(object sender, EventArgs e) 
     { 

     } 

     private void tp() //put textbox and button on tab on tab click 
     { 
      var tab = tabControl1.TabPages[0]; 
      //tab.Controls.Clear(); 
      btnDynamic = new Button() 
      { 
       Size = new Size(100, 30), 
       Left = 40, 
       Top = 50, 
       Text = "DynamicButton", 
      }; 
      btnDynamic.Click += new EventHandler(this.Form1_Load); 
      tab.Controls.Add(btnDynamic); 
      txtBoxYear = new TextBox() 
      { 
       Size = new Size(200, 100), 
       Left = 20, 
       Top = 10, 
      }; 
      tab.Controls.Add(txtBoxYear); 

     } 

     private void btnHardCode_Click(object sender, EventArgs e) 
     { 

     } 
    } 
}