Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-17
10
Medium Priority
?
376 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

610 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