为什么combobox.selecteditem没有读取组合框的属性? [英] Why doesn't combobox.selecteditem read the property of a combobox?

查看:122
本文介绍了为什么combobox.selecteditem没有读取组合框的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得很难相信,但是当组合框中的值发生变化时,带有ID LanguageSpoken的文本框(也尝试过标签)不会改变其文本的内容。



这种情况​​发生在Visual Studio 2010上的.NET 4.0xx中。是否会发生在其他人身上?



MSDN



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;
        }
    }
}

推荐答案





不要使用 SelectedText 。使用 SelectedItem

Hi,

Don''t use SelectedText. Use SelectedItem:
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.


你应该这样做的原因因此使用SelectedItem。



ComboBox.SelectedText表示

获取或设置在可编辑部分中选择的文本 System.Windows.Forms.ComboBox。



ComboBox.SelectedItem表示

获取或设置当前所选项目System.Windows.Forms.ComboBox。



/ EDIT

再次我想你忘了阅读SelectedText属性的REMARKS部分n < a href =http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedtext.aspx> MSDN [ ^ ]它就像这样说



当组合框失去焦点时,选择点移动到文本的开头,任何选定的文本变为未选中。在这种情况下,获取SelectedText属性将检索一个空字符串,并设置SelectedText属性将指定的值添加到文本的开头。



所以请使用selectedItem if你的comboBox是DropDown / DropDownList类型显示
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


这篇关于为什么combobox.selecteditem没有读取组合框的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆