Using List for combo box collection [modified]
-
Hi all, I've been trying to create a List that I want to use to populate a combo box collection. Frankly, I have no idea how to do this and have googled for quite some time trying to get a clue. Not to mention the whole List vs. IList, IEnumerable, etc. that I haven't even considered, but stumbled upon during my search. I have the code below, but I'm doing something wrong because I'm not able to call the list in a form load method.
public List<string> GetSerialDevice()
{// Get a list of serial port names. strPorts = SerialPort.GetPortNames(); List<string> strAmplifiers = new List<string>(); //Console.WriteLine("The following serial ports were found:" ); // // Display each port name to the console. // foreach (string strPort in strPorts) // { // Console.WriteLine(strPort); // } foreach (string strPort in strPorts) { serialPort1 = new SerialPort(strPort, 115200, Parity.None, 8, StopBits.One); serialPort1.Handshake = Handshake.None; serialPort1.Close(); serialPort1.ReadTimeout = 500; serialPort1.WriteTimeout = 500; serialPort1.Open(); serialPort1.WriteLine("!001:USR1?\\r"); string strUSR1Return = serialPort1.ReadTo("\\r"); //Console.WriteLine(USR1Return); //Each unit will need to have 2.0 written to USR1 for this to work properly if (strUSR1Return == "+2.000000") { serialPort1.WriteLine("!001:SERL?\\r"); string strSerialLow = serialPort1.ReadTo("\\r"); serialPort1.WriteLine("!001:SERH?\\r"); string strSerialHigh = serialPort1.ReadTo("\\r"); //SN coding per mfg double dblSerialNumber = double.Parse(strSerialHigh) \* 65536 + double.Parse(strSerialLow); Console.WriteLine("On" + " " + strPort + " " + "there is a valid amplifier with S/N " + dblSerialNumber.ToString()); strAmplifiers.Add(dblSerialNumber.ToString()); }//End of if }//End of foreach Console.WriteLine(strAmpli
-
Hi all, I've been trying to create a List that I want to use to populate a combo box collection. Frankly, I have no idea how to do this and have googled for quite some time trying to get a clue. Not to mention the whole List vs. IList, IEnumerable, etc. that I haven't even considered, but stumbled upon during my search. I have the code below, but I'm doing something wrong because I'm not able to call the list in a form load method.
public List<string> GetSerialDevice()
{// Get a list of serial port names. strPorts = SerialPort.GetPortNames(); List<string> strAmplifiers = new List<string>(); //Console.WriteLine("The following serial ports were found:" ); // // Display each port name to the console. // foreach (string strPort in strPorts) // { // Console.WriteLine(strPort); // } foreach (string strPort in strPorts) { serialPort1 = new SerialPort(strPort, 115200, Parity.None, 8, StopBits.One); serialPort1.Handshake = Handshake.None; serialPort1.Close(); serialPort1.ReadTimeout = 500; serialPort1.WriteTimeout = 500; serialPort1.Open(); serialPort1.WriteLine("!001:USR1?\\r"); string strUSR1Return = serialPort1.ReadTo("\\r"); //Console.WriteLine(USR1Return); //Each unit will need to have 2.0 written to USR1 for this to work properly if (strUSR1Return == "+2.000000") { serialPort1.WriteLine("!001:SERL?\\r"); string strSerialLow = serialPort1.ReadTo("\\r"); serialPort1.WriteLine("!001:SERH?\\r"); string strSerialHigh = serialPort1.ReadTo("\\r"); //SN coding per mfg double dblSerialNumber = double.Parse(strSerialHigh) \* 65536 + double.Parse(strSerialLow); Console.WriteLine("On" + " " + strPort + " " + "there is a valid amplifier with S/N " + dblSerialNumber.ToString()); strAmplifiers.Add(dblSerialNumber.ToString()); }//End of if }//End of foreach Console.WriteLine(strAmpli
Hi, your message isn't clear at all. The code shown is unrelated to the combobox you mention in the subject line. and you provide no symptoms: what should it do? what does it? how is that different? a few suggestions: 1. don't leave code in comments, in confuses all readers, including yourself. 2. don't do
serialPort1.WriteLine("!001:SERL?\r");
i.e. don't mix an explicit "\r" with some implicit newline character; if you need to make sure, either set the correct value toserialPort1.NewLine
or make everything explicit, e.g.serialPort1.Write("!001:SERL?\r\n");
3. what is the peripheral sending? if it includes "\r\n", what is going to happen to the "\n"? Won't it confuse the next read? 4. you should not usedouble.Parse
as it may throw an exception on bad input; never trust an external device to always offer correct data! 5. you should provide an overall try-catch, and log the Exception.ToString() you may get. At least till you get something to work, then maybe revisit proper error handling. 6. I think you should open and close serial ports within your loop, first open, then close. Not what you did! 7. A serial open may fail (e.g. when your system has an old dial-up modem built in, you probably won't be able to open it; when it throws an exception, your foreach loop would terminate immediately! :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
Hi, your message isn't clear at all. The code shown is unrelated to the combobox you mention in the subject line. and you provide no symptoms: what should it do? what does it? how is that different? a few suggestions: 1. don't leave code in comments, in confuses all readers, including yourself. 2. don't do
serialPort1.WriteLine("!001:SERL?\r");
i.e. don't mix an explicit "\r" with some implicit newline character; if you need to make sure, either set the correct value toserialPort1.NewLine
or make everything explicit, e.g.serialPort1.Write("!001:SERL?\r\n");
3. what is the peripheral sending? if it includes "\r\n", what is going to happen to the "\n"? Won't it confuse the next read? 4. you should not usedouble.Parse
as it may throw an exception on bad input; never trust an external device to always offer correct data! 5. you should provide an overall try-catch, and log the Exception.ToString() you may get. At least till you get something to work, then maybe revisit proper error handling. 6. I think you should open and close serial ports within your loop, first open, then close. Not what you did! 7. A serial open may fail (e.g. when your system has an old dial-up modem built in, you probably won't be able to open it; when it throws an exception, your foreach loop would terminate immediately! :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
Hi Luc, Depending on your memory, you may remember that you've addressed many of these issues on previous posts of mine. As you can see, I haven't addressed these yet. I know that it is best to do as much of that up front as possible, but as a very novice C# developer, you can understand that I am happy to successfully ping all available com ports and have the S/N of the device(s) plugged in returned. I think I will have to hire someone eventually to look at what the application is doing and build a more robust one.....However, I will implement exception handling soon (as well as most of your other suggestions) Initially, I had a question about getting a list into a combo box and the list I wanted was generated (at least attempted to) by the attached code. Specifically the instantiation of List
List<string> strAmplifiers = new List<string>();
and then
strAmplifiers.Add(dblSerialNumber.ToString());
in the if loop, followed by the return statement
return strAmplifiers;
The problem I'm having is that I cannot call strAmplifiers in another method. I want to use
cboAmplifier.Items.AddRange(strAmplifiers);
but it is as if strAmplifiers wasn't returned. Apparently I have some error be it syntax or something much larger.
-
Hi Luc, Depending on your memory, you may remember that you've addressed many of these issues on previous posts of mine. As you can see, I haven't addressed these yet. I know that it is best to do as much of that up front as possible, but as a very novice C# developer, you can understand that I am happy to successfully ping all available com ports and have the S/N of the device(s) plugged in returned. I think I will have to hire someone eventually to look at what the application is doing and build a more robust one.....However, I will implement exception handling soon (as well as most of your other suggestions) Initially, I had a question about getting a list into a combo box and the list I wanted was generated (at least attempted to) by the attached code. Specifically the instantiation of List
List<string> strAmplifiers = new List<string>();
and then
strAmplifiers.Add(dblSerialNumber.ToString());
in the if loop, followed by the return statement
return strAmplifiers;
The problem I'm having is that I cannot call strAmplifiers in another method. I want to use
cboAmplifier.Items.AddRange(strAmplifiers);
but it is as if strAmplifiers wasn't returned. Apparently I have some error be it syntax or something much larger.
Call the method not the variable.
cboAmplifier.Items.AddRange(GetSerialDevice().ToArray());
I added ToArray because the method is returning a List but the AddRange method expects an array[] of objects. Your strAmplifier variable/List exists only within the socpe of the Method it was defined. Can't use the variable outside the method where it was decalred.
Just an irritated, ranting son of ... an IT guy. At your trolling services