Reputation: 43
How do I create a public variable for a selected value in my comboBox1 so that it can be used in every button so I don't have to repeat it for every button? For every button I have:
var portNum = comboBox1.SelectedItem.ToString();
using (SerialPort port = new SerialPort( portNum, 9600, Parity.None, 8))
But I want to just have portNum and not have to put the var declaration line in every button.
public partial class planar232 : Form
{
private SerialPort comPort = new SerialPort();
private string[] ports = SerialPort.GetPortNames();
public planar232()
{
InitializeComponent();
foreach (string port in ports)
{
comboBox1.Items.Add(port);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
var portNum = comboBox1.SelectedItem.ToString();
using (SerialPort port = new SerialPort( portNum, 9600, Parity.None, 8))
{
byte[] bytesToSend = new byte[9] { 0x38, 0x30, 0x31, 0x73, 0x21, 0x30, 0x30, 0x31, 0x0D };
port.Open();
port.Write(bytesToSend, 0, 9);
}
}
private void button2_Click(object sender, EventArgs e)
{
var portNum = comboBox1.SelectedItem.ToString();
using (SerialPort port = new SerialPort( portNum, 9600, Parity.None, 8))
{
byte[] bytesToSend = new byte[9] { 0x38, 0x30, 0x31, 0x73, 0x21, 0x30, 0x30, 0x30, 0x0D };
port.Open();
port.Write(bytesToSend, 0, 9);
}
}
Upvotes: 4
Views: 816
Reputation:
If your ComboBox is just displaying "COM1", etc, do this:
using (SerialPort port = new SerialPort(comboBox1.Text, 9600, Parity.None, 8))
Or, if you need to use SelectedItem
,
using (SerialPort port = new SerialPort(comboBox1.SelectedItem.ToString(), 9600, Parity.None, 8))
If you really want a property,
string SelectedPort { get { return comboBox1.SelectedItem.ToString(); } }
Better yet, refactor your code to just specify the data in the button handler:
private void button1_Click(object sender, EventArgs e)
{
byte[] bytesToSend = new byte[9] { 0x38, 0x30, 0x31, 0x73, 0x21, 0x30, 0x30, 0x31, 0x0D };
this.Send(bytesToSend);
}
private void button2_Click(object sender, EventArgs e)
{
byte[] bytesToSend = new byte[9] { 0x38, 0x30, 0x31, 0x73, 0x21, 0x30, 0x30, 0x30, 0x0D };
this.Send(bytesToSend);
}
private void Send(byte[] bytesToSend)
{
var portNum = comboBox1.SelectedItem.ToString();
using (SerialPort port = new SerialPort( portNum, 9600, Parity.None, 8))
{
port.Open();
port.Write(bytesToSend, 0, bytesToSend.Length);
}
}
Upvotes: 4
Reputation: 20320
Disable Buttons 1 and 2 at design time.
add a private member PortNum
Add a SelectedItemChange Event handler to the combobox In that if the selection is not null, save it in PortNum and enable the buttons
Upvotes: 0
Reputation: 39950
Use a property:
// this is a class member
string PortNum
{
get { return comboBox1.SelectedItem.ToString(); }
}
// instead of your original code
using (var port = new SerialPort(PortNum, ...)) {
...
}
Upvotes: 0
Reputation: 44028
Create the property in the form scope:
public Form Form1
{
public string PortNum {get;set;}
public void ComboBox1_SelectionChanged(object sender, EventArgs e)
{
PortNum = ComboBox1.SelectedItem.ToString();
}
... (rest of your code)
}
Upvotes: 2