简体   繁体   中英

How to send command using combobox in C#

I wrote a code to send commands to a serial device using hyperlink click events. Because there are becoming more and more hyperlinks that I keep adding, I would like to simplify the application by making a dropdown list of all the command options so that when a specific one is clicked, an event fires that specific command down the serial port exactly how it does when a link is clicked. I'm not sure how to set this up so if I could see an example, it would be appreciated. Below is an example of my code when the link is clicked, I want to be able to do this but after a selection from the drop down list instead.

 private void linkLabel_HC1_101_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                var content = new List<byte>();
                content.Add(2);
                content.AddRange(Encoding.ASCII.GetBytes("01P00101##"));
                content.Add(3);
                byte[] buffer = content.ToArray();
                serialPort1.Write(buffer, 0, buffer.Length);
            }
        }

        private void HC2_101_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                var content = new List<byte>();
                content.Add(2);
                content.AddRange(Encoding.ASCII.GetBytes("02P00101##"));
                content.Add(3);
                byte[] buffer = content.ToArray();
                serialPort1.Write(buffer, 0, buffer.Length);
            }
        }

If you want to be simple create the dropdown list with static string values. If the string is the one you send it makes it super easy. Let's say your dropdown is called listbox

content.AddRange(Encoding.ASCII.GetBytes(this.listbox.Text));

Otherwise you could do a switch on the dropdown text if you wanted the text to be something different.

string bytesToGet = string.empty;
switch (listbox.Text){
    case "H01":
         bytesToGet = "01P00101##";
    case "H02":
         bytesToGet = "02P00101##";
}
content.AddRange(Encoding.ASCII.GetBytes(bytesToGet));

You can also do dropdowns that have a value paired with the text on the listbox. You can create this pairing statically in the designer if you want and then use the value to hold that string to you want to add as a range like this

content.AddRange(Encoding.ASCII.GetBytes(listbox.SelectedItem.Value));

You could use the SelectedIndexChange event of the ComboBox

eg

private void InitComboBox()
{
  comboBox1.Items.Add("Command1");
  comboBox1.Items.Add("Command2");
  comboBox1.Items.Add("Command3");
  comboBox1.Items.Add("Command4");
  comboBox1.Items.Add("Command5");
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  if (comboBox1.SelectedItem.ToString().Equals("Command1"))
  {
    //Excute Command here
  }
  //...
}

In your ComboBox you probably want to display a user-friendly text, not the bytes you want to send. Make a class with two string properties:

public class Command
{
    public string DisplayText { get; set; }
    public string CommandText { get; set; }

    public Send(SerialPort serialPort)
    {
        if (serialPort.IsOpen) {
            var content = new List<byte>();
            content.Add(2);
            content.AddRange(Encoding.ASCII.GetBytes(CommandText));
            content.Add(3);
            byte[] buffer = content.ToArray();
            serialPort.Write(buffer, 0, buffer.Length);
        }
    }

    public override string ToString()
    {
        return DisplayText;
    }
}

It is important to override ToString . This enables the combo box to display the items correctly.

Now you can add these commands to the combo box:

comboBox1.Add(new Command { DisplayText = "HC1 101", CommandText = "01P00101##" });
comboBox1.Add(new Command { DisplayText = "HC2 101", CommandText = "02P00101##" });

In a combo box event...

void ComboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
    var command = (Command)comboBox1.SelectedItem;
    if (command != null) {
        command.Send(serialPort1);
    )
}

You can also use a "send" button instead. This enables you to send the same command repeatedly, without having to re-select it in the combo box.

If the leading and trailing bytes are different for the different commands, you can include them as properties too.

A nice effect of this approach is, that you have the transmission logic in a separate class, instead of having it in the Form and you don't have to copy/paste the send routine any more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM