如何引用在运行时创建的TextBox
本文关键字:运行时 创建 TextBox 何引用 引用 | 更新日期: 2023-09-27 18:25:32
我正在创建一个应用程序,需要在其中运行多个计时器实例。我定义了标签、按钮、文本框等,但当我运行代码时,我得到一个NullReferenceException,表示没有对象的实例。我需要在同一类的另一个方法中定义的文本框中获取数据。这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Web.UI;
namespace week2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int i=0;
public int input;
public int temp;
public int hrs;
public int min;
public int sec;
public void button1_Click(object sender, EventArgs e)
{
Label label1 = new Label();
int count = panel1.Controls.OfType<Label>().ToList().Count;
label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label1.Location = new System.Drawing.Point(255, 20+(160*i));
label1.Size = new System.Drawing.Size(103, 13);
label1.Name = "label_" + (count + 1);
label1.Text = "Time (Seconds) :";
panel1.Controls.Add(label1);
Label label2 = new Label();
count = panel1.Controls.OfType<Label>().ToList().Count;
label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label2.Location = new System.Drawing.Point(33, 20+(160* i));
label2.Size = new System.Drawing.Size(150, 20);
label2.Name = "label_" + (count + 1);
label2.Text = "Timer " + (i + 1);
panel1.Controls.Add(label2);
Button button2 = new Button();
count = panel1.Controls.OfType<Button>().ToList().Count;
button2.Location = new System.Drawing.Point(283, 72+(160*i));
button2.Text = "Start";
button2.Name = "Start";
button2.Size= new System.Drawing.Size(75, 23);
button2.UseVisualStyleBackColor = true;
button2.Click += new System.EventHandler(this.button2_Click);
panel1.Controls.Add(button2);
Button button3 = new Button();
count = panel1.Controls.OfType<Button>().ToList().Count;
button3.Location = new System.Drawing.Point(283, 101 + (160 * i));
button3.Text = "Pause";
button3.Size = new System.Drawing.Size(75, 23);
button3.UseVisualStyleBackColor = true;
button3.Click += new System.EventHandler(this.button3_Click);
panel1.Controls.Add(button3);
Button button4 = new Button();
count = panel1.Controls.OfType<Button>().ToList().Count;
button4.Location = new System.Drawing.Point(283, 130 + (160 * i));
button4.Text = "Delete";
button4.Size = new System.Drawing.Size(75, 23);
button4.UseVisualStyleBackColor = true;
button4.Click += new System.EventHandler(this.button4_Click);
panel1.Controls.Add(button4);
TextBox textBox1 = new TextBox();
count = panel1.Controls.OfType<TextBox>().ToList().Count;
textBox1.Location = new System.Drawing.Point(283, 46+(160*i));
textBox1.Text = null;
textBox1.Name = "textBox1";
textBox1.Size = new System.Drawing.Size(75, 20);
textBox1.TextChanged += new System.EventHandler(this.textBox1_Changed);
panel1.Controls.Add(textBox1);
Timer timer1 = new Timer();
timer1.Interval = 1000;
timer1.Tick += new System.EventHandler(this.timer1_Tick);
Label label3 = new Label();
count = panel1.Controls.OfType<Label>().ToList().Count;
label3.Location = new System.Drawing.Point(40, 72 + (160 * i));
label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label3.Name = "hour";
label3.Text = "00";
label3.Size = new System.Drawing.Size(44, 31);
panel1.Controls.Add(label3);
Label label4 = new Label();
count = panel1.Controls.OfType<Label>().ToList().Count;
label4.Location = new System.Drawing.Point(90, 72 + (160 * i));
label4.Text = ":";
label4.Size = new System.Drawing.Size(23, 31);
label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
panel1.Controls.Add(label4);
Label label5 = new Label();
count = panel1.Controls.OfType<Label>().ToList().Count;
label5.Location = new System.Drawing.Point(119, 72 + (160 * i));
label5.Text = "00";
label5.Size = new System.Drawing.Size(46, 31);
label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label5.Name = "minute";
panel1.Controls.Add(label5);
Label label6 = new Label();
count = panel1.Controls.OfType<Label>().ToList().Count;
label6.Location = new System.Drawing.Point(171, 72 + (160 * i));
label6.Text = ":";
label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label6.Size = new System.Drawing.Size(23, 31);
panel1.Controls.Add(label6);
Label label7 = new Label();
count = panel1.Controls.OfType<Label>().ToList().Count;
label7.Location = new System.Drawing.Point(200, 72 + (160 * i));
label7.Text = "00";
label7.Size = new System.Drawing.Size(46, 31);
label7.Name = "second";
label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
panel1.Controls.Add(label7);
Panel panel2 = new Panel();
panel2.SuspendLayout();
panel2.Location = new System.Drawing.Point(10, 15 + (160 * i));
count = panel1.Controls.OfType<Panel>().ToList().Count;
panel2.Name = "panel2";
panel2.BorderStyle = BorderStyle.FixedSingle;
panel2.Size = new System.Drawing.Size(440, 150);
panel1.Controls.Add(panel2);
i = i + 1;
}
private void button3_Click(object sender, EventArgs e)
{
Button Start = (sender as Button);
TextBox textBox1 = (sender as TextBox);
Timer timer1 = (sender as Timer);
Button Pause = (sender as Button);
Label hour = (sender as Label);
Label minute = (sender as Label);
Label second = (sender as Label);
if (Pause.Text == "Pause")
{
Pause.Text = "Resume";
timer1.Enabled = false;
Pause.Enabled = true;
Start.Enabled = true;
}
else if (Pause.Text == "Resume")
{
Pause.Text = "Pause";
timer1.Enabled = true;
Pause.Enabled = true;
Start.Enabled = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Button Start = (sender as Button);
TextBox textBox1 = (sender as TextBox);
Timer timer1 = (sender as Timer);
Button Pause = (sender as Button);
Label hour = (sender as Label);
Label minute = (sender as Label);
Label second = (sender as Label);
if ((hrs == 0) && (min == 0) && (sec == 0))
{
timer1.Enabled = false;
MessageBox.Show("Time Out");
Start.Text = "Start";
Pause.Text = "Pause";
Pause.Enabled = true;
Start.Enabled = true;
textBox1.Clear();
textBox1.Enabled = true;
hour.Text = "00";
minute.Text = "00";
second.Text = "00";
}
else
{
if (sec < 1)
{
sec = 59;
if (min == 0)
{
min = 59;
if (hrs != 0)
{
hrs -= 1;
}
}
else
min -= 1;
}
else
sec -= 1;
hour.Text = hrs.ToString("D2");
minute.Text = min.ToString("D2");
second.Text = sec.ToString("D2");
}
}
public void textBox1_Changed(object sender, EventArgs e)
{
TextBox textBox1 = (sender as TextBox);
}
private void button4_Click(object sender, EventArgs e)
{
Button Delete = (sender as Button);
}
private void button2_Click(object sender, EventArgs e)
{
System.Web.UI.Control myControl1 = new System.Web.UI.Control();
myControl1 = myControl1.FindControl("textBox1");
Button Start = (sender as Button);
TextBox textBox1 = (myControl1 as TextBox);
Timer timer1 = (sender as Timer);
Button Pause = (sender as Button);
Label hour = (sender as Label);
Label minute = (sender as Label);
Label second = (sender as Label);
if (Start.Text == "Start")
{
if (string.IsNullOrWhiteSpace(textBox1.Text))
{
MessageBox.Show("Invalid Input");
}
else
{
input = Convert.ToInt32(textBox1.Text);
if ((input >= 0))
{
Start.Text = "Restart";
timer1.Enabled = true;
textBox1.Enabled = false;
Pause.Enabled = true;
try
{
temp = input / 60;
if (temp < 60)
{
hrs = 0;
min = temp;
sec = input % 60;
}
else if (temp >= 60)
{
hrs = temp / 60;
min = temp % 60;
sec = input % 60;
}
hour.Text = hrs.ToString("D2");
minute.Text = min.ToString("D2");
second.Text = sec.ToString("D2");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("Wrong input");
textBox1.Text = "";
}
}
}
else if (Start.Text == "Restart")
{
Start.Text = "Start";
timer1.Enabled = false;
Pause.Enabled = false;
Pause.Text = "Pause";
textBox1.Clear();
textBox1.Enabled = true;
hour.Text = "00";
minute.Text = "00";
second.Text = "00";
}
}
}
}
是什么导致了这个错误,我该如何解决?
您必须使用FindControl
而不是
private void button2_click (object sender, EventArgs e)
{
// Find control on page.
Control myControl1 = FindControl("textBox1");
if(myControl1 != null)
{
// Get control's parent.
TextBox textBox1 = (myControl1 as TextBox);
if (textBox1 != null && string.IsNullOrWhiteSpace(textBox1.Text))
{
/*code*/;
}
Response.Write("Parent of the text box is : " + myControl1.ID);
}
else
{
Response.Write("Control not found");
}
//...
你想做什么根本不明显。但空引用异常是因为发件人不是文本框
您可能想直接引用文本框,而不是