【发布时间】:2019-11-01 20:10:12
【问题描述】:
我的问题是,我想插入另一个类的字符串。调试器告诉我,该方法运行良好,字符串中充满了我需要的东西,但不知何故,它不会出现在我的 cstmAntraege 的 ListBox 中。该字符串使用GetAnwender 和SetAnwender 方法通过Data.cs 传输,并且运行良好。所以我只需要知道如何在类之间传输ListBox 数据。我应该提一下:我正在使用 Visual Studio,因此我不需要初始化 ListBox,因为它是在设计器中完成的。
我在互联网上搜索了一整天,没有发现任何工作。我尝试使用Listbox.Update()、ListBox.Refresh() 和ListBox.invalidate()(因为有人告诉过,这是有效的)。以我的知识,我没有发现其他任何东西。
// Thats the class where the string and ListBox-data is from
namespace FirewallDB_Client
{
public partial class NeuerAnwender : Form
{
// ... some code ...
// thats where the whole thing starts
private void btnSave_Click(object sender, EventArgs e)
{
cstmAntraege jobStart = new cstmAntraege();
string Anwender = "'" + txtUserID.Text + "', '" + txtVorname.Text + "', '" + txtNachname.Text + "', '" + txtEMail.Text + "', '" + txtFirma.Text + "'";
Data.SetAnwender(Anwender); //here the string is transfered into the data class
jobStart.AnwenderReload(); //and thats where i try to start the job in the other class where the listbox is
}
}
}
//thats the class where the listbox is and where the method is written
namespace FirewallDB_Client
{
public partial class cstmAntraege : Form
{
// ... some code ...
// after starting the method my programm jumps to this point
public void AnwenderReload()
{
string Anwenderzw = ".";
string Anwender = Data.GetAnwender();
if (Anwender != Anwenderzw)
{
lbAnwender.Items.Add(Data.GetAnwender()); //and there is where the string gets into an not existing listbox (i also tried a normal string like "test")
lbAnwender.Update();
}
}
}
}
我在cstmAntraege 表单中输入了Listbox,其中应该出现NeuerAnwender 表单中的字符串。
【问题讨论】:
-
Data是静态的吗?请发布Data课程。我没有看到任何cstmAntraege.Show()你不打算显示其他表单吗? -
此行为表明您正在创建 cstmAntraege 表单的新实例,并且在该实例中正确设置了数据,但您希望数据显示在您已经拥有的 cstmAntraege 的当前实例中显示。您如何创建该变量 jobStart?
-
@MongZhu 数据类仅用于传输字符串,效果很好。
-
@Steve cstmAntraege 应该是一个公式,您可以在其中插入个人数据。为了添加更多人,我创建了 NeuerAnwender 类,您可以在其中插入姓名、电子邮件等个人数据。然后按下保存按钮后,它应该在我当前打开的公式 cstmAntraege 中的 ListBox 中插入信息(信息已打包到字符串“Anwender”中。) JobStart 应该使用 'cstmAntraege JobStart = new cstmAntraege' 创建。
-
cstmAntraege jobStart = new cstmAntraege(); 这一行创建了一个 cstmAntraege 形式的新实例。在您调用 jobStart.Show() 并将项目添加到属于 jobStart 实例的列表框之前,这个新实例是不可见的。如果要将项目添加到 cstmAntraege 的现有实例,则需要使用引用该实例的变量而不是创建新实例
标签: c# visual-studio winforms listbox