?
Solved

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

Posted on 2011-03-17
10
Medium Priority
?
373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

765 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