我有 3 个组合框。一个用于 COM 端口,一个用于波特率,一个用于延迟。在某一时刻,它们都工作了,但由于某种奇怪的原因,它不再工作了。没有例外,直到我去加载我的表单然后我得到一个 NullPointerException。发生这种情况是因为我的 Load 事件没有被触发,并且通过一些搜索有人建议他们的 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));
}
}
更新2
我刚试过这个,它按预期工作......
this.delayCombo.Items.AddRange(new object[] { 50, 100, 200, 300, 400, 500 });
this.delayCombo.Dock = DockStyle.Top;
this.delayCombo.SelectedValueChanged += delayCombo_SelectedValueChanged;
有趣的是,波特率也是如此。所以它们都采用相同的参数(一个int数组),并且除了位置和数组之外,它们在各个方面几乎都是相同的。我不明白。我不想使用不同的方式添加数据,但它正在工作..:/