Solved

How to use  selectedItem in another method (really newbee question)

Posted on 2011-03-17
10
367 Views
Last Modified: 2012-06-21
This is a really newbee question, I know, so bear with me...

I have a combobox on a form where the user can select a command to be sent to a GSM modem.
When the user has selected a command, he can press a "Send command" button that is also on the form. This buttons event handler will then process the command and do various stuff.

How do I retrieve the value of SelectedItem from the Combobox in the "Send Command" button's event handler? I know how to retrieve it inside the combobox' own event handler(s) (see example code)

       private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        // I can easily extract the selected item here and display it in a Text Box ("textBox1") as in the example below:
        Object selectedCommand = smsCommands.SelectedItem;
        textBox1.Text = selectedCommand.ToString();
        }

        // Loads the available commands into the combobox
        private void LoadComboBox1()
        {
            smsCommands.Items.Add("Initialize modem");
            smsCommands.Items.Add("Query PIN status");
            smsCommands.Items.Add("Send PIN"); // AT+CPIN=1234
            smsCommands.Items.Add("Select TXT Mode"); // AT+CMGF=1
        }

        // event handler for the "Send Command" button:
        private void button1_Click(object sender, EventArgs e)
        {
        // How to I get the value of combobox' selectedItem here?
        }

Open in new window


Thanks in advance.
0
Comment
Question by:ThereIsNoSpoon
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35161306
Here's an approach:
// event handler for the "Send Command" button:
private void button1_Click(object sender, EventArgs e)
{
    string value = smsCommands.SelectedText;
}

Open in new window

0
 
LVL 6

Accepted Solution

by:
nandithaa earned 125 total points
ID: 35162664
Try this:
 // event handler for the "Send Command" button:
        private void button1_Click(object sender, EventArgs e)
        {
                 string Item = smsCommands.SelectedItem.ToString();
        }

Open in new window

0
 
LVL 14

Expert Comment

by:systan
ID: 35163217
hi
if you want to get directly from comboBox itself?, use SelectedItem

BUT, if you wan to call it using other method to pass selected text?,
Do like this;      

        string combotext; //Global variable, can be used in other functions or procedure
        private void button1_Click(object sender, EventArgs e)
        {
            combotext = comboBox1.Text;
            MessageBox.Show(combotext);
        }
0
 

Author Comment

by:ThereIsNoSpoon
ID: 35163706

systan, Your solution works fine, but I would prefer not to use a global variable. That can quickly become messy.
I figured it out myself the day after.
The code in the button1_click method should be:

        private void button1_Click(object sender, EventArgs e)
        {
            Object selectedCommand = smsCommands.SelectedItem;
            textBox1.Text = selectedCommand.ToString();
        }

The key to the solution is to declare the local variable selectedCommand as an object type. Then it is possible to refer to the combobox' selectedItem method

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35164239
>>  systan, Your solution works fine, but I would prefer not to use a global variable.

Is that to say the first two didn't?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ThereIsNoSpoon
ID: 35164529
@kaufmed:

I cannot use SelectedText since it only contains a value as long as it is - selected. When the user clicks the button, the text is no longer selected and all I get is an empty string.

nandithaa's solution worked just as well as mine (and is even simpler). Both nandithaa and I reference to the combox' object and therefore no global variable is needed.
0
 

Author Comment

by:ThereIsNoSpoon
ID: 35164535
The points should go to nandithaa as he/she came up with a solution that works and is also the most simple solution.
0
 
LVL 6

Expert Comment

by:nandithaa
ID: 35164563
So good to know that my solution helped :)
Thank you for the points :)
0
 
LVL 14

Expert Comment

by:systan
ID: 35170249
if me to select?, the best approach is from kaufmed;
it only uses selectedtext without converting it to string, converting it to string would differ a mmss.
I am preferring to use a properties that doesn't need a conversion.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35172013
@systan

ThereIsNoSpoon was correct in http:#35164529 that there is no SelectedText once the focus leaves the ComboBox (I didn't realize that at the time). SelectedText is only applicable for the text actually selected in the editable portion of ComboBox and while ComboBox has focus.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Chat Room 1 43
How to measure sizes and angles in scanned images ? 3 51
fomat Json objects 6 28
Problem to error 4 43
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now