【问题标题】:How to Change an Index in one form from another form according to user selection in C#如何根据 C# 中的用户选择将一种形式的索引从另一种形式更改为另一种形式
【发布时间】:2013-12-03 15:27:25
【问题描述】:

在 Form1 中,我将 comboBox.SelectedIndex 设置为索引 0 (comboBox.SelectedIndex = 0;)。这是它的代码。

public void FORM1_Load(object sender, EventArgs e) {

        comboBox.Items.Insert(0, "Customer Name");
        comboBox.Items.Insert(1, "Customer ID");
        comboBox.Items.Insert(2, "Phone Number");
        comboBox.Items.Insert(3, "Email");

        comboBox.SelectedIndex = 0;

    }

然后我想根据用户在MDI Form菜单栏中的选择来改变上面的索引。

例如:- 如果用户从 MDI 表单的菜单栏中选择“按 ID 搜索客户”,则上述值应更改为 1。

我使用了以下方法。但是没有成功

    private void byIDToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        frmSearchCustomer frm = new frmSearchCustomer();
        frm.comboBox.SelectedIndex = 1;
        frm.ShowDialog();


    } 

请给我一个正确的编程代码来满足我的要求。 谢谢

【问题讨论】:

  • 组合的名称是什么? cmbSearchBy 还是组合框?
  • 从你提到的内容来看,上面的代码看起来是正确的。这不符合您的期望吗?
  • 组合框的名称是“cmbSearchBy”。
  • @Naresh> 不。它给出了错误消息
  • 这不是你的第一个 sn-p 所说的,它使用“comboBox”。您需要确定真实姓名是什么。将控件的 Modifiers 属性设置为 Public。

标签: c#-4.0


【解决方案1】:

在包含组合框的表单上将代码更改为

public int UserSelectedIndex { get; set;}

public void FORM1_Load(object sender, EventArgs e) {

        comboBox.Items.Insert(0, "Customer Name");
        comboBox.Items.Insert(1, "Customer ID");
        comboBox.Items.Insert(2, "Phone Number");
        comboBox.Items.Insert(3, "Email");

        comboBox.SelectedIndex = UserSelectedIndex;

    }

形成 MDI 表格您现在可以设置选定的索引

private void byIDToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        frmSearchCustomer frm = new frmSearchCustomer();
        frm.UserSelectedIndex = 1;
        frm.ShowDialog();


    } 

【讨论】:

  • 感谢 r.net。它现在正在工作。 :) 谢谢大家对我的支持。
  • 不客气。我希望您确实努力了解为什么您的代码无法正常工作。将值设置为 1 后会调用 FORM_Load 事件,这会将组合框的值重新设置回 0。
  • > 是的。实际上,我一整天都在尝试这样做。但我做不到。无论如何,现在我知道原因了。再次非常感谢。 :)
猜你喜欢
  • 2016-03-07
  • 2010-10-06
  • 1970-01-01
  • 1970-01-01
  • 2015-06-06
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
相关资源
最近更新 更多