Jump to content

help with .NET

RTX 3090

Hi, I want to create a basic drop down list but cant find it in my tool box. Basically I am reading for all COM ports, and the user needs to choose the one they want. Help is appreciated, thanks

namespace SerialPortExample
{
    class SerialPortExample
    {
        public static void Main()
        {
            // Get a list of serial port names.
            string[] ports = SerialPort.GetPortNames();

            Console.WriteLine("The following serial ports were found:");

            // Display each port name to the console.
            foreach (string port in ports)
            {
                Console.WriteLine(port);
            }

            Console.ReadLine();
        }
    }
}

And below is my UI

namespace Multimeter_UI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}

image.png.5b90e8fe97a0061d52dfe8ef7499d3c2.png

Please tag me @RTX 3090 so I can see your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Hi! What you're looking for is called "ComboBox"

BJPQrv6M.png
 

string[] ports = SerialPort.GetPortNames();
foreach (string port in ports)
{
	comboBox1.Items.Add(port);
}

or

comboBox1.Items.AddRange(SerialPort.GetPortNames());


Also I recommend to use DropDownList style in Properties!

5POihWNA.png

 

3MCElLh0.png

Link to comment
Share on other sites

Link to post
Share on other sites

24 minutes ago, Murasaki said:

snip

 

 

Hi, greatly appreciate the help. I seem to be getting an error for some reason, this is my current code :

using System.IO.Ports;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Multimeter_UI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                comboBox1.Items.Add(port);
            }
        }
    }
}

and error is 

The name 'SerialPort' does not exist in the current context	Multimeter UI

 

Please tag me @RTX 3090 so I can see your reply

Link to comment
Share on other sites

Link to post
Share on other sites

That's odd, considering you have System.IO.Ports referenced! What .NET version is your project targeting?

Also don't put that code in the SelectedIndexChanged event, it most likely wont do anything since theres no items added for an index to change, put it in your form's Load event instead!

Link to comment
Share on other sites

Link to post
Share on other sites

51 minutes ago, Murasaki said:

That's odd, considering you have System.IO.Ports referenced! What .NET version is your project targeting?

Also don't put that code in the SelectedIndexChanged event, it most likely wont do anything since theres no items added for an index to change, put it in your form's Load event instead!

I went into the visual installer and it downloaded 2017 and 2019 and C and C++ automatically

Please tag me @RTX 3090 so I can see your reply

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Murasaki said:

That's odd, considering you have System.IO.Ports referenced! What .NET version is your project targeting?

Also don't put that code in the SelectedIndexChanged event, it most likely wont do anything since theres no items added for an index to change, put it in your form's Load event instead!

Please can you check I have done everything right. Here is what I see :

image.thumb.png.26f11bedfb3569072784ded04fe81fee.png

image.thumb.png.0198ff4e15dc63d6a972ded85190e608.png

 

I was thinking of using QT designer, but that thing is a real mess, like I don't even know where to start. I tired installing the community edition and it kept leaving out design studio, but I eventually got it to install. Do you think I would be better with qt or net

Please tag me @RTX 3090 so I can see your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Can you hover on 4a3mWNdm.png and see what suggestions it gives you? Should be a lightbulb icon with a red circle and white cross.

4 minutes ago, RTX 3090 said:

Do you think I would be better with qt or net

Oh I've never worked with QT so cant tell you, im a winforms nerd.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Murasaki said:

Try with this, it might need to install it.

Thanks for help, greatly appreciated, it seems to compile fine now, however it duplicates COM ports now. Do you think this could be because the software looped or something. However that is not really a problem for now. I am now going to learn this properly so I can finish the software off, I have a pluralsight subscription would you be able to recommend a good course for me to do over there. 

image.png.8b2fd8b969d3a734d6307bb8ade57e49.png

 

Please tag me @RTX 3090 so I can see your reply

Link to comment
Share on other sites

Link to post
Share on other sites

No problem!

As I said earlier you should execute this code on the form's Load event, not the combobox's SelectedIndexChanged event.

Go to the designer tab where your form is, doubleclick on the top of it and it should generate the Load event in your code. Move the serial port code there and it shouldn't duplicate.

5 minutes ago, RTX 3090 said:

would you be able to recommend a good course for me to do over there. 

I code as a hobby so havent taken any courses myself, can't help you there sadly, sorry!

Link to comment
Share on other sites

Link to post
Share on other sites

 

2 minutes ago, Murasaki said:

No problem!

As I said earlier you should execute this code on the form's Load event, not the combobox's SelectedIndexChanged event.

Go to the designer tab where your form is, doubleclick on the top of it and it should generate the Load event in your code. Move the serial port code there and it shouldn't duplicate.

Hi, all of a sudden it has decided it does not want to show COM ports anymore 🥴

I think it might be the compiler playing up, is it possible to compile it as an .exe, I cant find the bin area and don't know where it is saved

Please tag me @RTX 3090 so I can see your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Weird stuff.

Well it is compiling an exe and executing it for you. You can see where it is by going to View > Solution Explorer and in that window rightclick on the Solution (should look like "Solution 'Multimeter_UI' (1 of 1 project)" and then "Open folder in File Explorer", you should find the bin folder there, by default the exe will be in the Debug folder inside.

 

Or you can get the location the old-fashioned way through Task Manager while its running!

Link to comment
Share on other sites

Link to post
Share on other sites

-> Moved to Programming

 

Please don't dump threads to General Discussion.

^^^^ That's my post ^^^^
<-- This is me --- That's your scrollbar -->
vvvv Who's there? vvvv

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×