【问题标题】:ListBox won't update in different classListBox 不会在不同的类中更新
【发布时间】:2019-11-01 20:10:12
【问题描述】:

我的问题是,我想插入另一个类的字符串。调试器告诉我,该方法运行良好,字符串中充满了我需要的东西,但不知何故,它不会出现在我的 cstmAntraege 的 ListBox 中。该字符串使用GetAnwenderSetAnwender 方法通过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


【解决方案1】:

您需要做的第一件事是引用表单的正确实例,然后调用更新其内容的方法。您可以从 Application.OpenForms 集合中检索表单的当前实例(您已经显示并正在查看的表单)。使用该实例,您可以处理您的数据

private void btnSave_Click(object sender, EventArgs e)
{
    cstmAntraege jobStart = Application.OpenForms.OfType<cstmAntraege>().FirstOrDefault();
    if(jobStart == null)
    {
        jobStart = new cstmAntraege();
        jobStart.Show();
    }
    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
}

【讨论】:

  • 我尝试使用它,但它告诉我(当我将鼠标悬停在 .OpenForms 上时)“不可调用的成员 'Application.OpenForms' 不能像方法一样使用。”它在私人 void btnSave 内......所以我不明白为什么它会给我这个错误。也实现了使用 System.Windows.Forms。
  • 我的错。 OpenForms 是一种属性而不是一种方法。 OpenForms之后不需要()
  • 这几乎是解决方案。它适用于: object o =Application.OpenForms.OfType().FirstOrDefault();然后 cstmAntraege jobStart = (cstmAntraege)o;
【解决方案2】:

试试

listBox.Invalidate();

它告诉控件重绘。

也许这些项目被缓存而不是渲染。

【讨论】:

  • OP 说他已经尝试过了,但没有结果。
  • 哦抱歉,我没看到这个
猜你喜欢
  • 2012-02-29
  • 1970-01-01
  • 2011-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-07
  • 2014-03-08
  • 2014-11-26
相关资源
最近更新 更多