Imports System.IO.Ports Public Class SetingSerialPort Public G_Dictionary As Dictionary(Of String, (Boolean, String, Integer, String, Color)) Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click, Button3.Click, Button2.Click, Button1.Click Dim bt As Button = CType(sender, Button) '‘打开选色面板 Dim ColorDialog1 As New ColorDialog ColorDialog1.ShowDialog() '‘将选中的颜色赋值给文本的字体色 Select Case bt.Tag Case "1" TextBox1.ForeColor = ColorDialog1.Color Case "2" TextBox2.ForeColor = ColorDialog1.Color Case "3" TextBox3.ForeColor = ColorDialog1.Color Case "4" TextBox4.ForeColor = ColorDialog1.Color End Select End Sub Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click G_Dictionary.Clear() Dim boot As Integer '判断勾选框是否选中 '判断串口名称是否为空 port1 If String.IsNullOrEmpty(port1.Text) Then MsgBox("请选择串口1名称") Return End If '判断波特率是否为空 If String.IsNullOrEmpty(boot1.Text) Then MsgBox("请选择串口1波特率") Return End If '判断别名是否为空 If String.IsNullOrEmpty(TextBox1.Text) Then MsgBox("择串口1别名为空") Return End If '添加到列表中 G_Dictionary.Add("1", (CheckBox1.Checked, port1.Text, Convert.ToInt32(boot1.Text), TextBox1.Text, TextBox1.ForeColor)) If String.IsNullOrEmpty(port2.Text) Then MsgBox("请选择串口2名称") Return End If If String.IsNullOrEmpty(boot2.Text) Then MsgBox("请选择串口2波特率") Return End If If String.IsNullOrEmpty(TextBox2.Text) Then MsgBox("择串口2别名为空") Return End If G_Dictionary.Add("2", (CheckBox2.Checked, port2.Text, Convert.ToInt32(boot2.Text), TextBox2.Text, TextBox2.ForeColor)) If String.IsNullOrEmpty(port3.Text) Then MsgBox("请选择串口3名称") Return End If If String.IsNullOrEmpty(boot3.Text) Then MsgBox("请选择串口3波特率") Return End If If String.IsNullOrEmpty(TextBox3.Text) Then MsgBox("择串口3别名为空") Return End If G_Dictionary.Add("3", (CheckBox3.Checked, port3.Text, Convert.ToInt32(boot3.Text), TextBox3.Text, TextBox3.ForeColor)) If String.IsNullOrEmpty(port4.Text) Then MsgBox("请选择串口4名称") Return End If If String.IsNullOrEmpty(boot4.Text) Then MsgBox("请选择串口4波特率") Return End If If String.IsNullOrEmpty(TextBox4.Text) Then MsgBox("择串口4别名为空") Return End If G_Dictionary.Add("4", (CheckBox4.Checked, port4.Text, Convert.ToInt32(boot4.Text), TextBox4.Text, TextBox4.ForeColor)) '关闭窗体并返回ok Me.DialogResult = DialogResult.OK Me.Close() End Sub Private Sub SetingSerialPort_Load(sender As Object, e As EventArgs) Handles MyBase.Load '获取串口列表 Dim portNames As String() = SerialPort.GetPortNames() '将串口列表添加到下拉框中 port1.Items.Clear() port2.Items.Clear() port3.Items.Clear() port4.Items.Clear() port1.Items.AddRange(portNames) port2.Items.AddRange(portNames) port3.Items.AddRange(portNames) port4.Items.AddRange(portNames) If IsNothing(G_Dictionary) OrElse G_Dictionary.Count = 0 Then G_Dictionary = New Dictionary(Of String, (Boolean, String, Integer, String, Color)) port1.SelectedIndex = 0 port2.SelectedIndex = 0 port3.SelectedIndex = 0 port4.SelectedIndex = 0 boot1.SelectedIndex = 0 boot2.SelectedIndex = 0 boot3.SelectedIndex = 0 boot4.SelectedIndex = 0 Else For Each item In G_Dictionary Select Case item.Key Case "1" CheckBox1.Checked = item.Value.Item1 If port1.Items.Contains(item.Value.Item2) Then port1.Text = item.Value.Item2 Else port1.SelectedIndex = 0 End If boot1.Text = item.Value.Item3 TextBox1.Text = item.Value.Item4 TextBox1.ForeColor = item.Value.Item5 Case "2" CheckBox2.Checked = item.Value.Item1 If port2.Items.Contains(item.Value.Item2) Then port2.Text = item.Value.Item2 Else port2.SelectedIndex = 0 End If boot2.Text = item.Value.Item3 TextBox2.Text = item.Value.Item4 TextBox2.ForeColor = item.Value.Item5 Case "3" CheckBox3.Checked = item.Value.Item1 If port3.Items.Contains(item.Value.Item2) Then port3.Text = item.Value.Item2 Else port3.SelectedIndex = 0 End If boot3.Text = item.Value.Item3 TextBox3.Text = item.Value.Item4 TextBox3.ForeColor = item.Value.Item5 Case "4" CheckBox4.Checked = item.Value.Item1 If port4.Items.Contains(item.Value.Item2) Then port4.Text = item.Value.Item2 Else port4.SelectedIndex = 0 End If boot4.Text = item.Value.Item3 TextBox4.Text = item.Value.Item4 TextBox4.ForeColor = item.Value.Item5 End Select Next End If End Sub End Class