组合框没有更新以Enum作为数据源的文本
本文关键字:数据源 文本 Enum 更新 组合 | 更新日期: 2023-09-27 18:12:15
我试图做一个简单的窗口,让我配置一个基于"默认"串行端口的SerialPort。我将在下面展示代码,但首先我要总结一下它的作用。我创建了初始化一切的Form。我将默认SerialPort设置为一个值,然后执行showDialog。PortName、DataBits和BaudRate全部更新为正确的"默认"值。不更新的两个是奇偶校验和停止位。组合框中的选项是正确的,但它显示的值不是。当我在表单上点击取消时,它说停止位(大概奇偶校验也会说同样的事情)说enum值超出了合法范围。但是表单加载时不会显示错误。我被卡在这个问题上了,有人能帮帮我吗?我已经多次更改DefaultSerialPort设置方法,以尝试设置值的不同方法,但它并没有改变我的结果。我试过SelectedValue, SelectedItem。我试过将属性转换为enum值类型,我已经完成了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;
}
好吧,原来我遇到这么困难的原因与时间有关。好吧,这是一个猜测,但问题消失了,当我改变我的形式有一个加载事件火当它完成,然后设置默认串行端口。
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;
}