Click here to Skip to main content
15,924,367 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Well I find it hard to believe, but the textbox (also tried a label) with ID LanguageSpoken is not changing the content of its text when the value within the combobox is changing.

This happens in .NET 4.0xx, on Visual Studio 2010.& Does it happen to anyone else?

Combobox documentation at MSDN

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DesktopApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();           
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            LanguageSpoken.Text = cboUserLanguage.SelectedText;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cboUserLanguage.SelectedIndex = 0;
            LanguageSpoken.Text = cboUserLanguage.SelectedText;
        }
    }
}
Posted

Hi,

Don't use SelectedText. Use SelectedItem:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace DesktopApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();           
        }
 
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            LanguageSpoken.Text = (string)cboUserLanguage.SelectedItem;
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            cboUserLanguage.SelectedIndex = 0;
            LanguageSpoken.Text = (string)cboUserLanguage.SelectedItem;
        }
    }
}

Hope this helps.
 
Share this answer
 
v2
The reason why you should use SelectedItem is because of this.

ComboBox.SelectedText means
Gets or sets the text that is selected in the editable portion of a System.Windows.Forms.ComboBox.

ComboBox.SelectedItem means
Gets or sets currently selected item in the System.Windows.Forms.ComboBox.

/EDIT
Again I think you forget to read the REMARKS section of the SelectedText property n MSDN[^] and it says like this

When the combo box loses focus, the selection point moves to the beginning of the text and any selected text becomes unselected. In this case, getting the SelectedText property retrieves an empty string, and setting the SelectedText property adds the specified value to the beginning of the text.

so go with selectedItem if your comboBox is DropDown/DropDownList type display
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900