C# 如何从不同类获取checkstate和checkbox.text
我再次需要你的帮助。。我这样做的目的是,如果选中Form2.cs中的复选框,那么我将单击一个按钮以显示另一个WindowsForm(另一个Form.cs),该窗体具有一个列表框,该列表框将显示该复选框中的文本。这是我在其他表单上的工作C# 如何从不同类获取checkstate和checkbox.text,c#,text,if-statement,checkbox,listbox,C#,Text,If Statement,Checkbox,Listbox,我再次需要你的帮助。。我这样做的目的是,如果选中Form2.cs中的复选框,那么我将单击一个按钮以显示另一个WindowsForm(另一个Form.cs),该窗体具有一个列表框,该列表框将显示该复选框中的文本。这是我在其他表单上的工作 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.L
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;
namespace WindowsFormsApplication7
{
public partial class computationOfDineIn : Form
{
Form2 form2 = new Form2();
public computationOfDineIn()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (form2.checkBox1.Checked)
{
listBox1.Items.Add(form2.checkBox1.Text.ToString());
}
}
}
}
我将Form2.cs中复选框的修饰符更改为Public,以便在其他表单中使用它。但它不起作用,我是不是错过了什么?请有人告诉我(问:当条件满足时,我如何使它从另一个表单显示在列表框中?
我知道这是一个愚蠢的问题,但提前谢谢!!!:D
更新:显示表格2的代码。
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;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3();
form3.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
似乎您正在
Form2
的computationOfDineIn
内部创建新实例。您应该使用向用户显示的form2的相同实例
并且公开UI元素不是推荐的方法。在我看来,您必须创建一个属性,说明复选框是否已选中
您需要将form2
实例保存到某个地方,然后将该实例传递到computationOfDineIn
表单,然后检查该实例中的属性
一种方法是通过构造函数传递它
public partial class computationOfDineIn : Form
{
Form2 form2 = null;
public computationOfDineIn(Form2 form2)//pass the form2 you created in button click
{
this.form2 = form2;
InitializeComponent();
}
}
如何显示form2?form2.show();我用这个按钮。。。就像另一个表单发布代码一样。您是使用相同的
form2
还是不同的实例?被否决。。。没有理由??