使用int数组的Combobox.DataSource无法正常工作

时间:2013-06-03 17:09:56

标签: c# nullpointerexception datasource

我有3个Comboboxes。一个用于COM端口,一个用于波特率,一个用于延迟。有一次,他们都工作了,但由于一些奇怪的原因,它不再有效。在我加载表单之前没有例外,然后我得到一个NullPointerException。之所以会发生这种情况,是因为我的Load事件没有被触发,而且有些人搜索某人建议他们的DataSource应该受到指责。果然我注释掉了我的DataSource,我不再有空指针异常。

但令我困惑的是为什么。

        this.comportCombo.DataSource = System.IO.Ports.SerialPort.GetPortNames();
        this.comportCombo.Dock = DockStyle.Fill;
        this.comportCombo.SelectedValueChanged += comportCombo_SelectedValueChanged;

        this.baudRatCombo.DataSource = new int[] { 4800, 9600, 19200, 115200 };
        this.baudRatCombo.Dock = DockStyle.Fill;
        this.baudRatCombo.SelectedValueChanged += baudRatCombo_SelectedValueChanged;

        this.delayCombo.DataSource = new int[] { 50, 100, 200, 300, 400, 500 }; //Code I comment out to stop the NPE.
        this.delayCombo.Dock = DockStyle.Fill;
        this.delayCombo.SelectedValueChanged += delayCombo_SelectedValueChanged;

BaudRatCombo工作正常。所以我尝试了一个愚蠢的解决方案,只放4个数字,但仍然没有。我对此完全感到困惑。我的调试器对此也没有太多帮助。有什么建议?我已经尝试评论dalyCombo.DataSource(摆脱我的NPE)我已经尝试评论出其他2个组合框。我不知所措。请帮忙。

更新

public class IInputDialog : Form
{
    public IInputDialog()
    {
        InitializeComponent();
        this.Load += SerialPortDialog_Load;
    }
    public IInputDialog(IInput iinput)
        : this()
    {
        this._iinput = iinput;
    }

    void SerialPortDialog_Load(object sender, EventArgs e)
    {
        //Doesn't Fire with DelayCombo.DataSource
        if (this._iinput != null)
        {
            serialPortTablePanel.IInput = this._iinput;
        }
    }

    public static IInput GetIInputDialog(IInput iinput)
    {
        IInput temp = iinput;
        using (IInputDialog icb = new IInputDialog(iinput))
        {
            if (icb.ShowDialog() == DialogResult.OK)
                temp = icb.serialPortTablePanel.IInput;
        }
        return temp;
    }
    private void InitializeComponent()
    {
        this.SuspendLayout();
        this.serialPortTablePanel = new IInputTablePanel();
        this.serialPortTablePanel.Dock = DockStyle.Fill;
        this.Controls.Add(serialPortTablePanel);
        this.MinimumSize = new System.Drawing.Size(501, 562);

        this.ResumeLayout(false);
    }
    private IInputTablePanel serialPortTablePanel;
    private IInput _iinput;
}

SerialPortWedgeTablePanel(部分)

internal class IInputTablePanel : Control
{
    internal IInputTablePanel()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {

        SetupTestGroupBox(ref gb1, ref split1);


        tbl.Controls.Add(gb1, 0, 2);


    }

    private void SetupTestGroupBox(ref GroupBox gb1, ref SplitContainer split1)
    {
        this.comportCombo.DataSource = System.IO.Ports.SerialPort.GetPortNames();
        this.comportCombo.Dock = DockStyle.Top;
        this.comportCombo.SelectedValueChanged += comportCombo_SelectedValueChanged;

        this.baudRatCombo.DataSource = new int[] { 4800, 9600, 19200, 115200 };
        this.baudRatCombo.Dock = DockStyle.Top;
        this.baudRatCombo.SelectedValueChanged += baudRatCombo_SelectedValueChanged;

        this.delayCombo.DataSource = new int[] { 50, 100, 200, 300, 400, 500 };
        this.delayCombo.Dock = DockStyle.Top;
        this.delayCombo.SelectedValueChanged += delayCombo_SelectedValueChanged;

        gb1.Controls.Add(split1);
        gb1.Controls.Add(delayCombo);
        gb1.Controls.Add(baudRatCombo);
        gb1.Controls.Add(comportCombo);
        //gb1.Controls.Add(GetDefaultGroupBox("Input Delay", delayCombo));
        //gb1.Controls.Add(GetDefaultGroupBox("Baud Rate", baudRatCombo));
        //gb1.Controls.Add(GetDefaultGroupBox("Port Name", comportCombo));

        gb1.Dock = DockStyle.Fill;
        gb1.Enabled = false;
        gb1.Font = new Font("Arial", 10F, FontStyle.Regular, GraphicsUnit.Point, 0x00);
        gb1.Margin = new Padding(0);
        gb1.Padding = new Padding(10, 3, 10, 10);
        gb1.Text = "Serial Port Options";
    }

    void delayCombo_SelectedValueChanged(object sender, EventArgs e)
    {
        _iinput.Delay = (int)this.delayCombo.SelectedItem;
    }
    void baudRatCombo_SelectedValueChanged(object sender, EventArgs e)
    {
        _serialPort.BaudRate = (int)baudRatCombo.SelectedItem;
    }
    void comportCombo_SelectedValueChanged(object sender, EventArgs e)
    {
        _serialPort.PortName = comportCombo.SelectedItem.ToString();
    }

    internal IInput IInput
    {
        get {return _iinput;}
        set
        {
            this._iinput = value;
            this.delayCombo.SelectedItem = value.Delay;
            if (value is ComPortInput)
            {
                this.comportCombo.SelectedItem = (value as ComPortInput).serialPort.PortName; //string[]
                this.baudRatCombo.SelectedItem = (value as ComPortInput).serialPort.BaudRate; //object[]
                _serialPort = (value as ComPortInput).serialPort;
            }
        }
    }

    private void SuspendLayouts(params Control[] args)
    {
        this.SuspendLayout();
        foreach(Control c in args)
            c.SuspendLayout();
    }
    private void ResumeLayouts(bool resume, params Control[] args)
    {
        foreach (Control c in args)
            c.ResumeLayout(resume);

        this.ResumeLayout(resume);
    }


    private Button testButt;
    private ClickComboBox comportCombo = new ClickComboBox();
    private ClickComboBox baudRatCombo = new ClickComboBox();
    private ClickComboBox delayCombo = new ClickComboBox();
    private System.IO.Ports.SerialPort _serialPort = new System.IO.Ports.SerialPort();
    private IInput _iinput;
}
internal class ClickComboBox : System.Windows.Forms.ComboBox
{
    internal ClickComboBox()
    {
        this.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        this.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F);
        this.FormattingEnabled = true;
        this.ItemHeight = 37;
        this.Click += ClickComboBox_Click;
    }

    void ClickComboBox_Click(object sender, System.EventArgs e)
    {
        this.BeginInvoke(new System.Action(() => this.DroppedDown = true));
    }
}

UPDATE2

我刚试过这个,它按预期工作......

        this.delayCombo.Items.AddRange(new object[] { 50, 100, 200, 300, 400, 500 });
        this.delayCombo.Dock = DockStyle.Top;
        this.delayCombo.SelectedValueChanged += delayCombo_SelectedValueChanged;
有趣的是,波特率也是如此。所以他们都采用相同的参数(一个int数组),并且它们在各个方面几乎都是相同的,对位置和数组有例外。我不明白。我不想使用不同的方式添加数据,但它正在工作..:/

0 个答案:

没有答案