C# 从以C形式编程生成的元素中获取实时输入
如何通过代码生成的表单细节实时获取文本更改时复选框和文本框的值 下面的代码在按下按钮时生成一个表单,该表单有复选框和richtextbox。理想情况下,我希望任何交互都能产生效果,因此如果我将复选框粘贴到一个由1和0组成的网格中,复选框将更新,并且一旦复选框被单击,文本区域中相应的1或0将更新,这样我就可以将网格矩阵复制到另一个程序中 我知道如何使用VisualStudioGUI maker获取事件,但不知道如何从这样一个编程创建的表单获取事件C# 从以C形式编程生成的元素中获取实时输入,c#,visual-studio,input,real-time,C#,Visual Studio,Input,Real Time,如何通过代码生成的表单细节实时获取文本更改时复选框和文本框的值 下面的代码在按下按钮时生成一个表单,该表单有复选框和richtextbox。理想情况下,我希望任何交互都能产生效果,因此如果我将复选框粘贴到一个由1和0组成的网格中,复选框将更新,并且一旦复选框被单击,文本区域中相应的1或0将更新,这样我就可以将网格矩阵复制到另一个程序中 我知道如何使用VisualStudioGUI maker获取事件,但不知道如何从这样一个编程创建的表单获取事件 private void begin_bu
private void begin_button_Click(object sender, EventArgs e)
{
// Build the child form
Form check_box = new Form();
check_box.FormBorderStyle = FormBorderStyle.FixedSingle;
// Get the values from the textboxes
int height = Convert.ToInt16(this.height_input.Text);
int width = Convert.ToInt16(this.width_input.Text);
// Set the dimensions of the form
check_box.Width = width * 15 + 40;
check_box.Height = ( height * 15 + 40 ) * 3;
// Build checkboxes for the checkbox form
CheckBox[] chk;
chk = new CheckBox[height * width];
int count = 0;
for (int i = 1; i <= height; i++)
{
for (int j = 1; j <= width; j++)
{
chk[count] = new CheckBox();
chk[count].Name = count.ToString();
chk[count].Width = 15; // because the default is 100px for text
chk[count].Height = 15;
chk[count].Location = new Point(j * 15, i * 15);
chk[count].CheckedChanged += new EventHandler(this.CheckedChanged);
check_box.Controls.Add(chk[count]);
count += 1;
//Console.WriteLine(" i: " + i + " j: " + j + " Count: " + count);
}
}
RichTextBox output_area;
output_area = new RichTextBox();
output_area.Location = new Point(chk[0].Location.X, chk[count-1].Location.Y + 30);
check_box.Controls.Add(output_area);
output_area.Text = "hello world\n1,1,1,1,1,1,1,1,1\n0,0,0,0,0,1,0,1\nthese ones and zeros are meant to update in real time!";
output_area.Width = check_box.Width - 40;
output_area.Height = check_box.Height / 2;
// Run the form
check_box.ShowDialog();
}
请查看表单设计器为您生成的.Designer文件 无论如何,在您的循环中,尝试以下操作:
chk[count].CheckedChanged += MyFancyHandler;
处理程序本身看起来就像一个普通的处理程序:
private void MyFancyHandler( object sender, EventArgs e )
{
// ...
}
还请注意,那里的sender参数将包含对事件所指复选框/控件的引用。下面的代码在复选框复选状态更改时更新富文本框中的矩阵文本
RichTextBox output_area;
CheckBox[] chk;
Size MatrixSize;
private void begin_button_Click()
{
// Build the child form
Form check_box = new Form();
check_box.FormBorderStyle = FormBorderStyle.FixedSingle;
check_box.StartPosition = FormStartPosition.CenterScreen;
// Get the values from the textboxes
int height = Convert.ToInt16("10");
int width = Convert.ToInt16("7");
MatrixSize = new Size(width, height);
// Set the dimensions of the form
check_box.Width = width * 15 + 40;
check_box.Height = (height * 15 + 40) * 3;
// Build checkboxes for the checkbox form
chk = new CheckBox[height * width];
int count = 0;
for (int i = 1; i <= height; i++)
{
for (int j = 1; j <= width; j++)
{
chk[count] = new CheckBox();
chk[count].Name = count.ToString();
chk[count].Width = 15; // because the default is 100px for text
chk[count].Height = 15;
chk[count].Location = new Point(j * 15, i * 15);
check_box.Controls.Add(chk[count]);
chk[count].CheckedChanged += new EventHandler(CheckBox1_CheckedChanged);
count += 1;
//Console.WriteLine(" i: " + i + " j: " + j + " Count: " + count);
}
}
output_area = new RichTextBox();
output_area.Location = new Point(chk[0].Location.X, chk[count - 1].Location.Y + 30);
check_box.Controls.Add(output_area);
output_area.Text = "hello world\n1,1,1,1,1,1,1,1,1\n0,0,0,0,0,1,0,1\nthese ones and zeros are meant to update in real time!";
output_area.Width = check_box.Width - 40;
output_area.Height = check_box.Height / 2;
// Run the form
check_box.ShowDialog();
}
private void CheckBox1_CheckedChanged(Object sender, EventArgs e)
{
CheckBox c = (CheckBox)sender;
Debug.WriteLine(c.Name);
StringBuilder sb = new StringBuilder();
int count = 0;
for (int i = 1; i <= MatrixSize.Height; i++)
{
for (int j = 1; j <= MatrixSize.Width; j++)
{
if (chk[count].Checked)
{
sb.Append("1,");
}
else
{
sb.Append("0,");
}
count += 1;
}
sb.Append("\r\n");
}
output_area.Text = sb.ToString();
}
谷歌的其他结果可能有+吨的重复…@BartoszKP,这个链接表明我有一个循环,不断检查每个复选框?我甚至不确定我将把它放在哪里,代码似乎在check_box.ShowDialog处暂停;线路,不,没有。它向您展示了如何以编程方式将事件分配给生成的复选框。好的,我让事件处理程序工作,但是我无法访问生成的表单,我根本无法访问复选框。请不要在继续操作时修改您的帖子。这不是帮助台服务,更重要的是,这与你无关。这是一个收集对每个人都有用的一般问题和答案的地方,而不是为初学者提供详细的手工指导。请使用internet上提供的大量信息—在本例中为.NET教程和MSDN文档。开始提示:阅读对象为您公开的属性:。我不确定是否有复选框窗体的.Designer文件,因为整个内容都是在运行时生成的。我不知道如何访问复选框的处理程序。也许我应该写的是创建一些表单,添加一些复选框并连接一些事件,然后查看设计器文件!看看它是怎么做到的!啊,我明白了,看看它是如何正常工作的,这样我就可以在我的情况下复制它了?谢谢!在事件处理程序中,我添加了char标记=“”;然后sb[sb.Length-1]=令牌;在内部for循环的末尾,这样我就可以划分每一行的末尾,这是我在matlab中使用的一个矩阵。非常感谢你!
RichTextBox output_area;
CheckBox[] chk;
Size MatrixSize;
private void begin_button_Click()
{
// Build the child form
Form check_box = new Form();
check_box.FormBorderStyle = FormBorderStyle.FixedSingle;
check_box.StartPosition = FormStartPosition.CenterScreen;
// Get the values from the textboxes
int height = Convert.ToInt16("10");
int width = Convert.ToInt16("7");
MatrixSize = new Size(width, height);
// Set the dimensions of the form
check_box.Width = width * 15 + 40;
check_box.Height = (height * 15 + 40) * 3;
// Build checkboxes for the checkbox form
chk = new CheckBox[height * width];
int count = 0;
for (int i = 1; i <= height; i++)
{
for (int j = 1; j <= width; j++)
{
chk[count] = new CheckBox();
chk[count].Name = count.ToString();
chk[count].Width = 15; // because the default is 100px for text
chk[count].Height = 15;
chk[count].Location = new Point(j * 15, i * 15);
check_box.Controls.Add(chk[count]);
chk[count].CheckedChanged += new EventHandler(CheckBox1_CheckedChanged);
count += 1;
//Console.WriteLine(" i: " + i + " j: " + j + " Count: " + count);
}
}
output_area = new RichTextBox();
output_area.Location = new Point(chk[0].Location.X, chk[count - 1].Location.Y + 30);
check_box.Controls.Add(output_area);
output_area.Text = "hello world\n1,1,1,1,1,1,1,1,1\n0,0,0,0,0,1,0,1\nthese ones and zeros are meant to update in real time!";
output_area.Width = check_box.Width - 40;
output_area.Height = check_box.Height / 2;
// Run the form
check_box.ShowDialog();
}
private void CheckBox1_CheckedChanged(Object sender, EventArgs e)
{
CheckBox c = (CheckBox)sender;
Debug.WriteLine(c.Name);
StringBuilder sb = new StringBuilder();
int count = 0;
for (int i = 1; i <= MatrixSize.Height; i++)
{
for (int j = 1; j <= MatrixSize.Width; j++)
{
if (chk[count].Checked)
{
sb.Append("1,");
}
else
{
sb.Append("0,");
}
count += 1;
}
sb.Append("\r\n");
}
output_area.Text = sb.ToString();
}