Solved

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

Posted on 2011-03-17
10
366 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 74

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 74

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 74

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Query datatable in LINQ 6 39
optimize  c# code 7 48
C# code editing and collaboration 3 42
cs.Designer Issue 2 14
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

16 Experts available now in Live!

Get 1:1 Help Now