【问题标题】:Winform Combobox Changing GUI with vaulesWinform Combobox 使用值更改 GUI
【发布时间】:2014-11-24 14:04:51
【问题描述】:

我正在尝试在 winforms 中创建预订系统。我有一个组合框,您可以在其中选择从一到十的数字。我也有 10 个组框,我只希望显示的组框数量等于组合框中选定的数字。在组框中,我有另一个组合框,我可以在其中选择座位号,这就是为什么并非所有框都同时显示很重要的原因。我有点迷茫,我曾尝试使用 actionlistener,但没有成功。

这里我调用了组合框所在的 GUI:

private void btnMakeBook_Click(object sender, EventArgs e)
    {
        ServiceReferenceBooking.BookingServiceClient bookingService = new ServiceReferenceBooking.BookingServiceClient();
        ServiceReferenceTickets.TicketsServiceClient ticketsService = new ServiceReferenceTickets.TicketsServiceClient();
        splitContainer1.Panel2.Controls.Clear();
        InitializeMakeBookingComponents();

        var allBookings = bookingService.GetAllBookings();
        dataGridView2.DataSource = allBookings;

        cbTickets.ValueMember = "amount";
        cbTickets.DisplayMember = "amount";
        cbTickets.DataSource = ticketsService.GetAllTickets();

    }      

这是我想要制作的事件:

private void cbTickets_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cbTickets.SelectedItem.ToString() == "2")
            {
                gboxSeat2.Visible = true;
            }   
    }

我收到错误:对象引用未设置为对象的实例。 它在 if 语句所在的那一行。

这是我尝试创建的事件,但在选择 2 时 GroupBox 不可见。

希望大家帮忙!谢谢

【问题讨论】:

  • 你在哪里被击中了?有什么问题,如果某些东西不起作用,请发布您的代码。听起来只是在该数字组合框的SelectedIndexChanged 事件中设置GroupBox.Visible 属性。
  • 我没有要发布的代码。是的,这就是我想做的事情。我已经设置了所有 GroupBox.Visible = false 以便默认情况下不显示它们。但我不知道如何使用 SelectedIndexChanged ?
  • 那是simple。试试看,遇到问题就来这里。谢谢。

标签: winforms combobox


【解决方案1】:

SelectedIndexSelectedItem 之间存在差异,第一个是所选项目的基于 0 的索引,第二个是您要查找的实际项目。试试这个

private void cbTickets_SelectedIndexChanged(object sender, EventArgs e)
{
    if (((ComboBox)sender).SelectedItem == "2")        
    {
        gboxSeat2.Visible = true;
    }
}

充实我的建议:

public partial class Form1 : Form
{
    //Declare your basic controls here
    GroupBox gboxSeat1 = new GroupBox() { BackColor = Color.Red, Visible=false};
    GroupBox gboxSeat2 = new GroupBox() { BackColor = Color.Blue, Visible = false };
    GroupBox gboxSeat3 = new GroupBox() { BackColor = Color.Green, Visible = false };
    GroupBox gboxSeat4 = new GroupBox() { BackColor = Color.Orange, Visible = false };
    ComboBox cbTickets = new ComboBox() { Items = { "0", "1", "2", "3", "4" }, Height = 35, Width = 150 };
    public Form1()
    {
        InitializeComponent();
        //Setup handlers and add them to the panel
        cbTickets.SelectedIndexChanged += comboBox2_SelectedIndexChanged;
        panel1.Controls.Add(gboxSeat1);
        panel1.Controls.Add(gboxSeat2);
        panel1.Controls.Add(gboxSeat3);
        panel1.Controls.Add(gboxSeat4);
        panel1.Controls.Add(cbTickets);

    }

    private void InitializeBookingComponents()
    {
        //Arrange your controls the way you want here
    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (((ComboBox)sender).SelectedItem == "2")
            gboxSeat2.Visible = true;
    }

}

这将确保您只有一个控件,您可以在闲暇时将它们删除或添加到您的面板中,只维护您的一个控件

【讨论】:

  • 我已经尝试过了,现在我收到一条错误消息:对象引用未设置为对象的实例。你知道为什么会这样吗?它以某种方式得到一个空值。我不明白为什么?
  • 显示您正在使用的导致该错误的确切代码。这是说你的一个对象是空的
  • 好的,我要编辑上面的代码。请稍等。
  • 在你的 If 语句上放置一个断点并检查你的变量,看看什么是空的。
  • 我的 cbTickets.SelectedItem 为空?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
  • 2015-05-27
相关资源
最近更新 更多