2013-05-03 20 views
0

我想做一個簡單的窗口,讓我配置一個基於「默認」串行端口的串行端口。我將在下面顯示代碼,但首先我將總結它的功能。我創建了我的Form來初始化所有內容。我將一個默認的SerialPort設置爲一個值,然後我showDialog。 PortName,DataBits和BaudRate全部更新爲正確的「默認」值。不更新的2是Parity和StopBits。組合框中的選項是正確的,但它顯示的值不是。當我在我的表單上點擊取消時,它表示停止位(可能是奇偶校驗會說同樣的事)表示枚舉值超出了合法範圍。但窗體加載時不顯示錯誤。我被困在這,有人可以幫忙嗎?我多次改變了DefaultSerialPort set方法來嘗試設置值的不同方式,但它並沒有改變我的結果。我試過SelectedValue,SelectedItem。我已經嘗試將屬性轉換爲它們的枚舉值類型,我已經完成了ToString(),但都無濟於事。所以現在代碼。ComboBoxes不更新與Enum作爲數據源的文本

所以這裏是我的測試。這個想法是我應該在我的表單上取消取消。奇怪的非標準值只是表明不同的值正在更新。

[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; 
    } 

回答

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; 
}