Solved

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

Posted on 2011-03-17
10
370 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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