ComboBox没有使用Enum作为数据源更新Text

时间:2013-05-03 19:08:51

标签: c# binding combobox enums

我正在尝试创建一个简单的窗口,让我可以根据“默认”串行端口配置SerialPort。我将在下面展示代码,但首先我要总结它的作用。我创建了我的表单来初始化所有内容。我将默认的SerialPort设置为一个值,然后我showDialog。 PortName,DataBits和BaudRate都更新为正确的“默认”值。不更新的2是Parity和StopBits。组合框中的选项是正确的,但它显示的值不是。当我在我的表单上点击取消时,它表示停止(并且可能是奇偶校验会说同样的事情)说,枚举值超出了合法范围。但表单加载时不会显示错误。我坚持这个,有人可以帮忙吗?我已多次更改DefaultSerialPort设置方法以尝试设置值的不同方法,但它没有改变我的结果。我试过SelectedValue,SelectedItem。我已经尝试将属性转换为它们的枚举值类型,我已经完成了ToString(),但都无济于事。所以现在代码。

所以这是我的考试。这个想法是我应该在我的表单上点击取消。奇怪的非标准值只是为了表明不同的值正在更新。

    [Test]
    public void TestSerialPortDialog()
    {
        var expected = new SerialPort("COM3", 115200, Parity.Odd, 8, StopBits.OnePointFive);
        var actual = SerialPortDialog.GetSerialPortDialog(expected);
        Assert.AreEqual(expected.PortName, actual.PortName);
        Assert.AreEqual(expected.BaudRate, actual.BaudRate);
        Assert.AreEqual(expected.Parity, actual.Parity);
        Assert.AreEqual(expected.DataBits, actual.DataBits);
        Assert.AreEqual(expected.StopBits, actual.StopBits);

    }

SerialPortDialog

public class SerialPortDialog : Form
{
    public SerialPortDialog()
    {
        InitializeComponent();
    }
    public static SerialPort GetSerialPortDialog(SerialPort sp)
    {
        SerialPort temp = null;
        using (SerialPortDialog icb = new SerialPortDialog())
        {
            icb.serialPortTablePanel.DefaultSerialPort = sp;
            icb.ShowDialog();
            temp = icb.serialPortTablePanel.InputSerialPort;
        }
        return temp;
    }
    private void InitializeComponent()
    {
        this.SuspendLayout();
        this.serialPortTablePanel = new SerialPortTablePanel();
        this.serialPortTablePanel.Dock = DockStyle.Fill;
        this.Controls.Add(serialPortTablePanel);
        this.MinimumSize = new System.Drawing.Size(280, 400);

        this.ResumeLayout(false);
    }
    private SerialPortTablePanel serialPortTablePanel;
}

SerialPortTablePanel

internal class SerialPortTablePanel : Control
{
    internal SerialPortTablePanel()
    {
        InitializeComponent();
    }
    internal SerialPort InputSerialPort
    {
        get
        {
            var com = comportCombo.SelectedItem.ToString();
            var baud = (int)baudRatCombo.SelectedItem;
            var parity = (Parity)parityCombo.SelectedValue;
            var data = (int)databitCombo.SelectedItem;
            var stop = (StopBits)stopbitCombo.SelectedValue;
            return new SerialPort(com, baud, parity, data, stop);
        }
    }

    private void InitializeComponent()
    {
        System.Console.WriteLine("Initialize");
        var tbl = new TableLayoutPanel();
        var split1 = new SplitContainer();
        var split2 = new SplitContainer();

        this.SuspendLayout();
        tbl.SuspendLayout();
        split1.SuspendLayout();
        split1.Panel1.SuspendLayout();
        split1.Panel2.SuspendLayout();
        split2.SuspendLayout();
        split2.Panel1.SuspendLayout();
        split2.Panel2.SuspendLayout();

        split1.Dock = DockStyle.Fill;
        split1.IsSplitterFixed = true;
        split1.SplitterDistance = split1.Width / 2;
        split1.SplitterWidth = 1;

        split2.Dock = DockStyle.Fill;
        split2.SplitterDistance = 218;

        SetupTablePanel(tbl);
        tbl.Controls.Add(split1, 0, 6);
        tbl.Controls.Add(split2, 0, 5);

        comportCombo.Items.AddRange(GetComPorts());
        comportCombo.Text = "Select COM";

        baudRatCombo.Items.AddRange(GetBaudRate());
        baudRatCombo.Text = "Select BaudRate";

        parityCombo.DataSource = (System.Enum.GetValues(typeof(Parity)));
        parityCombo.Text = "Select Parity";

        databitCombo.Items.AddRange(GetDatabit());
        databitCombo.Text = "Select DataBits";

        stopbitCombo.DataSource = (System.Enum.GetValues(typeof(StopBits)));
        stopbitCombo.Text = "Select StopBits";

        okButton = GetDefaultButton("OK", split1.Panel1);
        okButton.DialogResult = DialogResult.OK;
        okButton.Enabled = false;

        cancelButton = GetDefaultButton("Cancel", split1.Panel2);
        cancelButton.DialogResult = DialogResult.Cancel;

        testButton = GetDefaultButton("Test", split2.Panel1);
        testButton.Click += testButton_Click;

        testLbl.Dock = DockStyle.Fill;
        split2.Panel2.Controls.Add(testLbl);


        split1.ResumeLayout(false);
        split1.Panel1.ResumeLayout(false);
        split1.Panel2.ResumeLayout(false);
        split2.ResumeLayout(false);
        split2.Panel1.ResumeLayout(false);
        split2.Panel2.ResumeLayout(false);
        tbl.ResumeLayout(false);
        this.ResumeLayout(false);
    }
    private void SetupTablePanel(TableLayoutPanel tbl)
    {
        tbl.ColumnCount = 1;
        tbl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
        tbl.Controls.Add(this.comportCombo, 0, 0);
        tbl.Controls.Add(this.baudRatCombo, 0, 1);
        tbl.Controls.Add(this.parityCombo, 0, 2);
        tbl.Controls.Add(this.databitCombo, 0, 3);
        tbl.Controls.Add(this.stopbitCombo, 0, 4);
        tbl.Dock = DockStyle.Fill;
        tbl.RowCount = 7;
        tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 14.28571F));
        tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 14.28571F));
        tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 14.28571F));
        tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 14.28571F));
        tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 14.28571F));
        tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 14.28571F));
        tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 14.28571F));

        this.Controls.Add(tbl);
    }
    private Button GetDefaultButton(string text, SplitterPanel sp)
    {
        Button butt = new Button();
        butt.Dock = DockStyle.Fill;
        butt.MinimumSize = new Size(40, 40);
        butt.Text = text;

        sp.Controls.Add(butt);
        return butt;
    }
    void testButton_Click(object sender, System.EventArgs e)
    {
        using (var temp = InputSerialPort)
        {
            try
            {
                temp.Open();
                if (temp.IsOpen)
                {
                    okButton.Enabled = true;
                    testLbl.BackColor = Color.PaleGreen;
                }
                else
                {
                    okButton.Enabled = false;
                    testLbl.BackColor = Color.Pink;
                }
                temp.Close();
            }
            catch
            {
                okButton.Enabled = false;
                testLbl.BackColor = Color.Pink;
            }
        }
    }

    private string[] GetComPorts()
    {
        return SerialPort.GetPortNames();
    }
    private object[] GetBaudRate()
    {
        return new object[] { 4800, 9600, 19200, 115200 };
    }
    private object[] GetDatabit()
    {
        return new object[] { 5, 6, 7, 8 };
    }

    public Button okButton;
    public Button cancelButton;
    public Button testButton;
    public SerialPort DefaultSerialPort
    {
        set
        {
            this.comportCombo.SelectedItem = value.PortName; //string[]
            this.baudRatCombo.SelectedItem = value.BaudRate; //object[]
            this.databitCombo.SelectedItem = value.DataBits; //object[]
            this.parityCombo.SelectedItem = value.Parity; //Parity enum
            this.stopbitCombo.SelectedItem = value.StopBits; //StopBits enum
        }
    }

    private ClickComboBox comportCombo = new ClickComboBox();
    private ClickComboBox baudRatCombo = new ClickComboBox();
    private ClickComboBox parityCombo = new ClickComboBox();
    private ClickComboBox databitCombo = new ClickComboBox();
    private ClickComboBox stopbitCombo = new ClickComboBox();
    private Label testLbl = new Label();
}

感谢您的时间

修改

呃,令人讨厌的一部分就是我将这个代码放在一个单独的项目中,并且按预期工作......

    private void Form1_Load(object sender, System.EventArgs e)
    {
        comboBox1.DataSource = SerialPort.GetPortNames();
        comboBox2.DataSource = new int[] { 4800, 9600, 19200, 115200 };
        comboBox3.DataSource = new int[] { 5, 6, 7, 8 };
        comboBox4.DataSource = Enum.GetValues(typeof(Parity));
        comboBox5.DataSource = Enum.GetValues(typeof(StopBits));

        var sp = new SerialPort("COM2");
        comboBox1.SelectedItem = sp.PortName;
        comboBox2.SelectedItem = sp.BaudRate;
        comboBox3.SelectedItem = sp.DataBits;
        comboBox4.SelectedItem = sp.Parity;
        comboBox5.SelectedItem = sp.StopBits;
    }

1 个答案:

答案 0 :(得分:0)

好吧事实证明我之所以有这么困难的时间与时间有关..这是一个猜测,但是当我改变我的表格以便在完成时发生Load事件时,问题就消失了设置默认串口。

public class SerialPortDialog : Form
{
    public SerialPortDialog()
    {
        InitializeComponent();
        this.Load += SerialPortDialog_Load;
    }

    void SerialPortDialog_Load(object sender, System.EventArgs e)
    {
        if (serial != null)
        {
            serialPortTablePanel.DefaultSerialPort = serial;
        }
    }
    public SerialPortDialog(SerialPort sp) : this()
    {
        this.serial = sp;
    }
    public static SerialPort GetSerialPortDialog(SerialPort sp)
    {
        SerialPort temp = sp;
        using (SerialPortDialog icb = new SerialPortDialog(sp))
        {
            if (icb.ShowDialog() == DialogResult.OK)
                temp = icb.serialPortTablePanel.InputSerialPort;
        }
        return temp;
    }
    private void InitializeComponent()
    {
        this.SuspendLayout();
        this.serialPortTablePanel = new SerialPortTablePanel();
        this.serialPortTablePanel.Dock = DockStyle.Fill;
        this.Controls.Add(serialPortTablePanel);
        this.MinimumSize = new System.Drawing.Size(280, 500);

        this.ResumeLayout(false);
    }
    private SerialPortTablePanel serialPortTablePanel;
    private SerialPort serial;
}