重置下拉组合框c#

时间:2015-01-22 10:26:05

标签: c# combobox

我创建了一个允许座位预订的表单,它有两个下拉菜单和一个标签,可以更改显示他们预订的座位。当我按下重置按钮但是下拉菜单没有时,标签会重置,它们会保留在选定的座位上,例如一个菜单说“A”而另一个说“4”我需要这些才能重置为空白,任何人都可以帮忙吗?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace courseworkTask3
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        if (comboBox1.SelectedIndex <= 0)
        {
            button1.Enabled = false;
        }
        else 
        {
            button1.Enabled = true;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

        string temp = comboBox1.Text;
        int dropBox1enter;
        if (int.TryParse(temp, out dropBox1enter))
        {

        }


        int i = 0;
        int seatsFound = 0;
        int maxFoundSeats = 0;
        int maxFoundSeatsStart = 0;
        int maxFoundSeatsEnd = 0;
        bool[] rowA = new bool[10];
        rowA[0] = true;
        rowA[1] = true;
        rowA[2] = false;
        rowA[3] = false;
        rowA[4] = false;
        rowA[5] = true;
        rowA[6] = true;
        rowA[7] = false;
        rowA[8] = false;
        rowA[9] = false;

        bool[] rowB = new bool[10];
        rowB[0] = true;
        rowB[1] = false;
        rowB[2] = false;
        rowB[3] = false;
        rowB[4] = false;
        rowB[5] = true;
        rowB[6] = true;
        rowB[7] = false;
        rowB[8] = false;
        rowB[9] = false;

        bool[] rowC = new bool[10];
        rowC[0] = true;
        rowC[1] = false;
        rowC[2] = false;
        rowC[3] = false;
        rowC[4] = false;
        rowC[5] = true;
        rowC[6] = true;
        rowC[7] = false;
        rowC[8] = false;
        rowC[9] = false;

        bool[] rowD = new bool[10];
        rowD[0] = true;
        rowD[1] = false;
        rowD[2] = false;
        rowD[3] = false;
        rowD[4] = false;
        rowD[5] = true;
        rowD[6] = true;
        rowD[7] = false;
        rowD[8] = false;
        rowD[9] = false;

        bool[] rowE = new bool[10];
        rowE[0] = true;
        rowE[1] = false;
        rowE[2] = false;
        rowE[3] = false;
        rowE[4] = false;
        rowE[5] = true;
        rowE[6] = true;
        rowE[7] = false;
        rowE[8] = false;
        rowE[9] = false;

        if (comboBox2.Text == "A")
        {
            while (i < 10 && maxFoundSeats < dropBox1enter)
            {
                if (rowA[i] == false)
                {
                    seatsFound++;
                    if (seatsFound > maxFoundSeats)
                    {
                        maxFoundSeats = seatsFound;
                        maxFoundSeatsStart = 1 + i - seatsFound;
                        maxFoundSeatsEnd = i;
                    }
                }
                else
                {
                    seatsFound = 0;
                }
                i++;
            } if (maxFoundSeats == dropBox1enter)
            {
                label4.Text = "Booked for " + maxFoundSeats + " seat(s) in row A starting, seat " + maxFoundSeatsStart + " to seat " + maxFoundSeatsEnd + ".";

            }
            else
            {
                label4.Text = "The seats you have selected are unavailable. @The maximum available is " + maxFoundSeats + " in row A From " + maxFoundSeatsStart + " to " + maxFoundSeatsEnd + ".";
                label4.Text = label4.Text.Replace("@", System.Environment.NewLine);
            }
        }
        else
        {
            if (comboBox2.Text == "B")
            {
                while (i < 10 && maxFoundSeats < dropBox1enter)
                {
                    if (rowB[i] == false)
                    {
                        seatsFound++;
                        if (seatsFound > maxFoundSeats)
                        {
                            maxFoundSeats = seatsFound;
                            maxFoundSeatsStart = 1 + i - seatsFound;
                            maxFoundSeatsEnd = i;
                        }
                    }
                    else
                    {
                        seatsFound = 0;
                    }
                    i++;
                }
                if (maxFoundSeats == dropBox1enter)
                {
                    label4.Text = "Booked for " + maxFoundSeats + " seat(s) in row B starting, seat " + maxFoundSeatsStart + " to seat " + maxFoundSeatsEnd + ".";

                }
                else
                {
                    label4.Text = "The seats you have selected are unavailable. @The maximum available is " + maxFoundSeats + " in row B From " + maxFoundSeatsStart + " to " + maxFoundSeatsEnd + ".";
                    label4.Text = label4.Text.Replace("@", System.Environment.NewLine);
                }
            }
            else
            {
                if (comboBox2.Text == "C")
                {
                    while (i < 10 && maxFoundSeats < dropBox1enter)
                    {
                        if (rowC[i] == false)
                        {
                            seatsFound++;
                            if (seatsFound > maxFoundSeats)
                            {
                                maxFoundSeats = seatsFound;
                                maxFoundSeatsStart = 1 + i - seatsFound;
                                maxFoundSeatsEnd = i;
                            }
                        }
                        else
                        {
                            seatsFound = 0;
                        }
                        i++;
                    }
                    if (maxFoundSeats == dropBox1enter)
                    {
                        label4.Text = "Booked for " + maxFoundSeats + " seat(s) in row C starting, seat " + maxFoundSeatsStart + " to seat " + maxFoundSeatsEnd + ".";

                    }
                    else
                    {
                        label4.Text = "The seats you have selected are unavailable. @The maximum available is " + maxFoundSeats + " in row C From " + maxFoundSeatsStart + " to " + maxFoundSeatsEnd + ".";
                        label4.Text = label4.Text.Replace("@", System.Environment.NewLine);
                    }
                }
                else
                {
                    if (comboBox2.Text == "D")
                    {
                        while (i < 10 && maxFoundSeats < dropBox1enter)
                        {
                            if (rowD[i] == false)
                            {
                                seatsFound++;
                                if (seatsFound > maxFoundSeats)
                                {
                                    maxFoundSeats = seatsFound;
                                    maxFoundSeatsStart = 1 + i - seatsFound;
                                    maxFoundSeatsEnd = i;
                                }
                            }
                            else
                            {
                                seatsFound = 0;
                            }
                            i++;
                        }
                        if (maxFoundSeats == dropBox1enter)
                        {
                            label4.Text = "Booked for " + maxFoundSeats + " seat(s) in row D starting, seat " + maxFoundSeatsStart + " to seat " + maxFoundSeatsEnd + ".";

                        }
                        else
                        {
                            label4.Text = "The seats you have selected are unavailable. @The maximum available is " + maxFoundSeats + " in row D From " + maxFoundSeatsStart + " to " + maxFoundSeatsEnd + ".";
                            label4.Text = label4.Text.Replace("@", System.Environment.NewLine);
                        }
                    }
                    else
                    {
                        if (comboBox2.Text == "E")
                        {
                            while (i < 10 && maxFoundSeats < dropBox1enter)
                            {
                                if (rowE[i] == false)
                                {
                                    seatsFound++;
                                    if (seatsFound > maxFoundSeats)
                                    {
                                        maxFoundSeats = seatsFound;
                                        maxFoundSeatsStart = 1 + i - seatsFound;
                                        maxFoundSeatsEnd = i;
                                    }
                                }
                                else
                                {
                                    seatsFound = 0;
                                }
                                i++;
                            }
                            if (maxFoundSeats == dropBox1enter)
                            {
                                label4.Text = "Booked for " + maxFoundSeats + " seat(s) in row E starting, seat " + maxFoundSeatsStart + " to seat " + maxFoundSeatsEnd + ".";

                            }
                            else
                            {
                                label4.Text = "The seats you have selected are unavailable. @The maximum available is " + maxFoundSeats + " in row E From " + maxFoundSeatsStart + " to " + maxFoundSeatsEnd + ".";
                                label4.Text = label4.Text.Replace("@", System.Environment.NewLine);
                            }
                          }
                        } //end E
                    } //end D
                } //end C
            } 
        }

    private void dropBox1_SelectedIndexChanged(object sender, EventArgs e)
    {


    }

    private void dropBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.Text != null && comboBox2.Text != null)

        {
            button1.Enabled = true;
        }

    }

    private void button2_Click(object sender, EventArgs e)
    {

        label4.Text = "";


    } 


    }

}

1 个答案:

答案 0 :(得分:0)

正如@Rohit Prakash在你的问题的评论中指出的那样,将ComboBox的SelectedIndex设置为-1会将任何选择重置为Combobox:

(我假设“button2”是你的重置按钮,你应该更恰当地命名你的控件)

private void button2_Click(object sender, EventArgs e)
{    
    label4.Text = "";
    comboBox1.SelectedIndex = -1; 
    comboBox2.SelectedIndex = -1;      
}