【问题标题】:Why i can't return my method?为什么我不能返回我的方法?
【发布时间】:2015-09-21 10:21:06
【问题描述】:

我有一个枚举,其中包含一些语言,例如英语、法语、西班牙语、德语 ...

我有下面的方法来返回一个以枚举为数据源的组合框实例:

  public ComboBox UpdateComboBoxIdioma()
        {
            comboIdioma.DataSource = Enum.GetValues(typeof(Idioms)); 
            return comboIdioma;//it return 6 languages
        }

我将方法的结果组合框分配给表单中的组合框,以显示我的语言,如下所示:

comboBox2 = classeDefinicoes.UpdateComboBoxIdioma();

但它没有显示表单上的语言。

可能是什么问题?

【问题讨论】:

  • “classeDefinicoes”也是“SettingsClass classeDefinicoes = new SettingsClass();”的变量
  • 你在哪里定义了comboIdioma?
  • 组合框中是空行还是没有行。
  • @Nikita 发生返回指令时我没有任何空行
  • comboIdioma 是我的表单“form.cs”中定义的构造函数的变量

标签: c# forms methods combobox enums


【解决方案1】:

在我看来,从方法返回组合控件不是一个好主意。相反,您应该简单地从 Idioms 枚举中获取值并将其设置为这样的组合的 DataSource -

var comboSource = Enum.GetValues(typeof(Idioms));
this.comboBox2.DataSource = comboSource;

或者如果你喜欢一个衬垫,可以这样:)

this.comboBox2.DataSource = Enum.GetValues(typeof(Idioms));

【讨论】:

    【解决方案2】:

    假设你有一个Form 和SettingsClass:

    我试图重现你想说的话。我在Form 中添加了ComboBox 和Button。在 Button Click 事件中,我像您提供的那样设置了 ComboBox 值。代码如下:

    Form1.cs:

    public partial class Form1 : Form
    {       
        public Form1()
        {
            InitializeComponent();
        }
    
        SettingsClass classeDefinicoes;
    
        private void Form1_Load(object sender, EventArgs e)
        {
             classeDefinicoes = new SettingsClass();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            comboBox2 = classeDefinicoes.UpdateComboBoxIdioma();
        }        
    }
    

    SettingsClass.cs:

    class SettingsClass
    {
        enum Idioms
        {
            English, French, Spanish, German
        }
    
        ComboBox comboIdioma;
    
        public SettingsClass()
        {
            comboIdioma = new ComboBox();
        }
    
        public ComboBox UpdateComboBoxIdioma()
        {
            comboIdioma.DataSource = Enum.GetValues(typeof(Idioms));
            return comboIdioma;//it return 6 languages
        }
    }
    

    使用此代码,即使我在表单中的ComboBox 中也没有得到任何语言。当我如下更改我的Button Click 事件时,我得到了ComboBox 中的项目。

    更改Button点击事件:

    private void button1_Click(object sender, EventArgs e)
    {
        comboBox2.DataSource = classeDefinicoes.UpdateComboBoxIdioma().DataSource;
    }
    

    更新:

    您必须设置DataSource 属性才能获得更改。仅仅将返回的ComboBox 分配给表单中的ComboBox 不足以显示项目。

    Yogi 说的也是对的。如果您想按照他所说的进行操作,那么代码将变成这样:

    SettingsClass.cs::

    class SettingsClass
    {
        enum Idioms
        {
            English, French, Spanish, German
        }
    
        public Array UpdateComboBoxIdioma()
        {
            return Enum.GetValues(typeof(Idioms));
        }
    }
    

    更改Button点击事件:

    private void button1_Click(object sender, EventArgs e)
    {
        comboBox2.DataSource = classeDefinicoes.UpdateComboBoxIdioma();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-02
      • 2018-07-24
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      • 2013-03-10
      相关资源
      最近更新 更多