Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态更改Winforms组合框中项目的文本_C#_.net_Winforms_Combobox_Tostring - Fatal编程技术网

C# 动态更改Winforms组合框中项目的文本

C# 动态更改Winforms组合框中项目的文本,c#,.net,winforms,combobox,tostring,C#,.net,Winforms,Combobox,Tostring,我有一个Winforms组合框,其中包含自定义类的实例。当项目第一次添加到组合框的items集合时,ToString方法将对每个项目进行调用 但是,当用户更改应用程序运行的语言时,ToString方法的结果会更改 因此,我如何让组合框对所有项目再次调用ToString方法,而不必从组合框中删除所有项目并将其添加回?您应该能够通过调用RefreshItems()方法来实现这一点。是正确的。然而,正如前面提到的,子类是必要的。 RefreshItems是System.Windows.Forms.Co

我有一个Winforms
组合框
,其中包含自定义类的实例。当项目第一次添加到
组合框的items集合时,
ToString
方法将对每个项目进行调用

但是,当用户更改应用程序运行的语言时,
ToString
方法的结果会更改


因此,我如何让组合框
对所有项目再次调用
ToString
方法,而不必从组合框中删除所有项目并将其添加回?

您应该能够通过调用
RefreshItems()
方法来实现这一点。

是正确的。然而,正如前面提到的,子类是必要的。 RefreshItems是System.Windows.Forms.ComboBox的受保护方法

下面的表单应用程序提供了该行为的示例,以及更新组合框的RefreshItems方法:

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

namespace WindowsApplication1
{
    public class Form1 : Form
    {
        private List<HelloWorld> helloWorlds;

        #region Form1.Designer.cs
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose( bool disposing )
        {
            if ( disposing && (components != null) )
            {
                components.Dispose();
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.comboBox1 = new RefreshingComboBox();
            this.comboBox2 = new RefreshingComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // comboBox1
            // 
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point( 76, 12 );
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size( 115, 21 );
            this.comboBox1.TabIndex = 0;
            // 
            // comboBox2
            // 
            this.comboBox2.FormattingEnabled = true;
            this.comboBox2.Location = new System.Drawing.Point( 250, 12 );
            this.comboBox2.Name = "comboBox2";
            this.comboBox2.Size = new System.Drawing.Size( 218, 21 );
            this.comboBox2.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point( 12, 15 );
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size( 58, 13 );
            this.label1.TabIndex = 2;
            this.label1.Text = "Language:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point( 213, 15 );
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size( 31, 13 );
            this.label2.TabIndex = 3;
            this.label2.Text = "Text:";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point( 34, 42 );
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size( 75, 23 );
            this.button1.TabIndex = 4;
            this.button1.Text = "Set All";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler( this.button1_Click );
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point( 116, 42 );
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size( 75, 23 );
            this.button2.TabIndex = 5;
            this.button2.Text = "Set Random";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler( this.button2_Click );
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point( 393, 42 );
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size( 75, 23 );
            this.button3.TabIndex = 6;
            this.button3.Text = "Refresh!";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler( this.button3_Click );
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 13F );
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size( 556, 77 );
            this.Controls.Add( this.button3 );
            this.Controls.Add( this.button2 );
            this.Controls.Add( this.button1 );
            this.Controls.Add( this.label2 );
            this.Controls.Add( this.label1 );
            this.Controls.Add( this.comboBox2 );
            this.Controls.Add( this.comboBox1 );
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout( false );
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.ComboBox comboBox1;
        private System.Windows.Forms.ComboBox comboBox2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;

        #endregion

        public Form1()
        {
            InitializeComponent();

            comboBox1.DataSource = new HelloWorld().GetLanguages();

            helloWorlds = new List<HelloWorld>();
            while ( helloWorlds.Count < 10 )
            {
                helloWorlds.Add( new HelloWorld() );
            }

            comboBox2.DataSource = helloWorlds;
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault( false );
            Application.Run( new Form1() );
        }

        private void changeAllLanguages()
        {
            HelloWorld.LanguageValue newLang = (HelloWorld.LanguageValue) comboBox1.SelectedValue;

            helloWorlds.ForEach(
                delegate( HelloWorld hw )
                {
                    hw.Language = newLang;
                } );

        }

        private void changeRandomLanguage()
        {
            int index = new Random().Next( helloWorlds.Count );
            HelloWorld.LanguageValue newLang = (HelloWorld.LanguageValue) comboBox1.SelectedValue;

            helloWorlds[index].Language = newLang;
        }

        private void button1_Click( object sender, EventArgs e )
        {
            changeAllLanguages();
        }

        private void button2_Click( object sender, EventArgs e )
        {
            changeRandomLanguage();
        }

        private void button3_Click( object sender, EventArgs e )
        {
            (comboBox2 as RefreshingComboBox).RefreshItems();
        }
    }

    public class RefreshingComboBox : System.Windows.Forms.ComboBox
    {
        public new void RefreshItem(int index)
        {
            base.RefreshItem(index);
        }

        public new void RefreshItems()
        {
            base.RefreshItems();
        }
    }

    public class HelloWorld
    {
        public enum LanguageValue
        {
            English,
            日本語,
            Deutsch,
            Français,
            Český
        }

        private LanguageValue language;

        public LanguageValue Language
        {
            get
            {
                return language;
            }
            set
            {
                language = value;
            }
        }

        public Array GetLanguages()
        {
            return Enum.GetValues( typeof( LanguageValue ) );
        }

        Dictionary<LanguageValue, string> helloWorlds;

        public HelloWorld()
        {
            helloWorlds = new Dictionary<LanguageValue, string>();
            helloWorlds[LanguageValue.English] = "Hello, world!";
            helloWorlds[LanguageValue.日本語] = "こんにちは、世界!";
            helloWorlds[LanguageValue.Deutsch] = "Hallo, Welt!";
            helloWorlds[LanguageValue.Français] = "Sallut, monde!";
            helloWorlds[LanguageValue.Český] = "Ahoj svět!";
        }

        public override string ToString()
        {
            return helloWorlds[language];
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用系统文本;
使用System.Windows.Forms;
命名空间Windows应用程序1
{
公开课表格1:表格
{
私有列表helloWorlds;
#区域Form1.Designer.cs
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.comboBox1=新的RefreshingComboBox();
this.combox2=新的刷新组合框();
this.label1=new System.Windows.Forms.Label();
this.label2=new System.Windows.Forms.Label();
this.button1=new System.Windows.Forms.Button();
this.button2=new System.Windows.Forms.Button();
this.button3=new System.Windows.Forms.Button();
这个.SuspendLayout();
// 
//组合框1
// 
this.comboBox1.FormattingEnabled=true;
this.comboBox1.Location=新系统图点(76,12);
this.comboBox1.Name=“comboBox1”;
this.comboBox1.Size=新系统图纸尺寸(115,21);
this.comboBox1.TabIndex=0;
// 
//组合框2
// 
this.comboBox2.FormattingEnabled=true;
this.comboBox2.Location=新系统.图纸.点(250,12);
this.comboBox2.Name=“comboBox2”;
this.comboBox2.Size=新系统图纸尺寸(218,21);
this.comboBox2.TabIndex=1;
// 
//标签1
// 
this.label1.AutoSize=true;
this.label1.Location=新系统图纸点(12,15);
this.label1.Name=“label1”;
this.label1.Size=新系统图纸尺寸(58,13);
this.label1.TabIndex=2;
this.label1.Text=“语言:”;
// 
//标签2
// 
this.label2.AutoSize=true;
this.label2.Location=新系统图纸点(213,15);
this.label2.Name=“label2”;
this.label2.Size=新系统图纸尺寸(31,13);
this.label2.TabIndex=3;
this.label2.Text=“Text:”;
// 
//按钮1
// 
this.button1.Location=新系统图纸点(34,42);
this.button1.Name=“button1”;
this.button1.Size=新系统图纸尺寸(75,23);
this.button1.TabIndex=4;
this.button1.Text=“全部设置”;
this.button1.UseVisualStyleBackColor=true;
this.button1.Click+=新系统.EventHandler(this.button1\u Click);
// 
//按钮2
// 
this.button2.Location=新系统图纸点(116,42);
this.button2.Name=“button2”;
this.button2.Size=新系统图纸尺寸(75,23);
this.button2.TabIndex=5;
this.button2.Text=“设置随机”;
this.button2.UseVisualStyleBackColor=true;
this.button2.Click+=新系统.EventHandler(this.button2\u Click);
// 
//按钮3
// 
this.button3.Location=新系统图纸点(393,42);
this.button3.Name=“button3”;
this.button3.Size=新系统图纸尺寸(75,23);
this.button3.TabIndex=6;
this.button3.Text=“刷新!”;
this.button3.UseVisualStyleBackColor=true;
this.button3.Click+=新系统.EventHandler(this.button3\u Click);
// 
//表格1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(556,77);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboBox2);
this.Controls.Add(this.comboBox1);
this.Name=“Form1”;
this.Text=“Form1”;
此选项为.resume布局(false);
public class RefreshingComboBox : ComboBox
{
    public new void RefreshItem(int index)
    {
        base.RefreshItem(index);
    }

    public new void RefreshItems()
    {
        base.RefreshItems();
    }
}
public class RefreshingToolStripComboBox : ToolStripComboBox
{
    // We do not want "fake" selectedIndex change events etc, subclass that overide the OnIndexChanged etc
    // will have to check InOnCultureChanged them selfs
    private bool inRefresh = false;
    public bool InRefresh { get { return inRefresh; } }

    public void Refresh()
    {
        try
        {
            inRefresh = true;

            // This is harder then it shold be, as I can't get to the Refesh method that
            // is on the embebed combro box.
            //
            // I am trying to get ToString recalled on all the items
            int selectedIndex = SelectedIndex;
            object[] items = new object[Items.Count];
            Items.CopyTo(items, 0);

            Items.Clear();

            Items.AddRange(items);
            SelectedIndex = selectedIndex;
        }
        finally
        {
            inRefresh = false;
        }
    }

    protected override void OnSelectedIndexChanged(EventArgs e)
    {
        if (!inRefresh)
        {
            base.OnSelectedIndexChanged(e);
        }
    }
}
        /// <summary>
        /// Recreates the items
        /// </summary>
        public static void RefreshItems(this ComboBox cb)
        {
            var selectedIndex = cb.SelectedIndex;
            cb.SelectedIndex = -1;            
            MethodInfo dynMethod = cb.GetType().GetMethod("RefreshItems", BindingFlags.NonPublic | BindingFlags.Instance);
            dynMethod.Invoke(cb, null);
            cb.SelectedIndex = selectedIndex;

        }