form1中有combox1控件,其加载内容为
combox1.Items.Add("C#");
combox1.Items.Add("C++");
form2中有combox2控件,需要加载的内容和combox1的一样
我的做法是和form1中一样,写下面2句进行加载
combox2.Items.Add("C#");
combox2.Items.Add("C++");
如果有很多窗体都用到该类控件的相同内容,这样写太麻烦了
求该问题的简单方法

解决方案 »

  1.   


    我想可以写个方法
            public static void AddElement(ComboBox c)
            {
                c.Items.Add("C++");
                c.Items.Add("C#");
            }
    然后每次都去调用这个方法应该可行
      

  2.   

    /// <summary>
            /// 复制一个ComboBox的所有下拉列表给另一个。
            /// </summary>
            /// <param name="sourceComboBox"></param>
            /// <param name="targetComboBox"></param>
            public void CopyComboBoxToAnother(ComboBox sourceComboBox, ComboBox targetComboBox)
            {
                if (sourceComboBox == null || targetComboBox == null)
                    return;            targetComboBox.Items.Clear();            foreach (object item in sourceComboBox.Items)
                {
                    targetComboBox.Items.Add(item);
                }
            }
      

  3.   

    你public一个  string   str="C#,C++";然后引用上面的   str  对他进行拆分  然后for循环在复制给combox1.Items.Add(str[i]);
    这个是针对str字符串变化(str="C#,C++,js";) 这样就不用全部改了。
      

  4.   

    lz你想写一次每个页面的combox1全部复值吗  貌似不太可能
      

  5.   

    public static void AddElement(ComboBox c)
            {
                string[] str__str = { "C#","C++"};
                for (int i = 0; i < str__str.Count(); i++)
                { c.Items.Add(str__str[i].ToString());}
                //c.Items.Add("C++");
                //c.Items.Add("C#");
            }
    如果值不一样,可以这样写,只要给str__str赋值不同,显示的就不同
      

  6.   

    CopyComboBoxToAnother(comboBox1, comboBox2);我试过可以把一个控件的选项复制给另一个。
      

  7.   

    对于经常用到的方法,就提取一个公共的方法放到一个工具类中。或者像上面提到的,做一个用户控件。我认为还是第一个方法好一些。写一个公共的方法,来绑定你所有窗体的combox。没个窗体只需要都调用这个方法就可以。参数可以是你需要绑定的combox。
      

  8.   

    form1窗体有
    public void CopyComboxToAnother(ComboBox SourceCombox,ComboBox TargetCombox)
            {
                if (SourceCombox == null || TargetCombox == null)
                    return;
                TargetCombox.Items.Clear();
                foreach (object item in SourceCombox.Items)
                {
                    TargetCombox.Items.Add(item);
                }
    SourceCombox在form2窗体中
    TargetCombox在form3中
    form3中
     private void form3_Load(object sender, EventArgs e)
            {
                form1 f1 = new form1();
                form2 f2= new form2();
                f1.CopyComboxToAnother(f2.cbMarry,cbMarried);
               
            }
    错误在哪里?
      

  9.   

    form1窗体有
    public void CopyComboxToAnother(ComboBox SourceCombox,ComboBox TargetCombox)
      {
      if (SourceCombox == null || TargetCombox == null)
      return;
      TargetCombox.Items.Clear();
      foreach (object item in SourceCombox.Items)
      {
      TargetCombox.Items.Add(item);
      }
    SourceCombox在form2窗体中
    TargetCombox在form3中
    form3中
     private void form3_Load(object sender, EventArgs e)
      {
      form1 f1 = new form1();
      form2 f2= new form2();
      f1.CopyComboxToAnother(f2.cbMarry,cbMarried);
        
      }
    错误在哪里?
      

  10.   

    可以通过.net 3.5的扩展方法,给combox控件扩展一个绑定方法。以后再使用这个控件就方便了。
      public static void CodeBinding(this System.Web.UI.WebControls.DropDownList DropDownListControl, string CodeType)
            {
                         DropDownListControl.DataSource = ((List<SysCodeInfo>)HttpContext.Current.Cache["code_" + CodeType]);// codeService.GetCodeListByFlag(CodeType);
                DropDownListControl.DataTextField = "name";
                DropDownListControl.DataValueField = "Code";
                DropDownListControl.DataBind();
                DropDownListControl.Items.Insert(0, new System.Web.UI.WebControls.ListItem("请选择", ""));
            }
      

  11.   

    用wpf啊.binding.一句话:
    双向绑定:
    <TextBox Name="textBox1" Text="{Binding ElementName=TextBox2, Path=Text}"/>textBox1的Text与TextBox2的Text同步绑定改任何一方的值都会变另一方的值.
      

  12.   

    在同一个工程项目中的话可以这样写
    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 WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                comboBox1.Items.Add("C#");
                comboBox1.Items.Add("C++");        }        private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2();
                ComboBox cb = new ComboBox();
                Object[] objs = new Object[comboBox1.Items.Count];
                comboBox1.Items.CopyTo(objs, 0);
                cb.Items.AddRange(objs);
                Label lb = new Label();
                lb.Text = "Form1加给我的";
                cb.Left = 100;
                frm.Controls.Add(lb);
                frm.Controls.Add(cb);
                frm.Show();
            }
        }
    }
    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 WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void Form2_Load(object sender, EventArgs e)
            {
                FormCollection frms = Application.OpenForms;
                foreach (Form frm in frms)
                {
                    if (frm.Name == "Form1")
                    {
                        ComboBox cb=null;
                        cb = frm.Controls.Find("comboBox1", true)[0] as ComboBox;
                        if (cb != null && cb.Items.Count>0)
                        {
                            Object[] objs = new Object[cb.Items.Count];
                            cb.Items.CopyTo(objs, 0);
                            this.comboBox1.Items.AddRange(objs);
                            
                        }
                        break;
                    }
                }
            }
        }
    }
      

  13.   

    写个公共的方法,然后当combox用到的时候直接调用就可以了。
      

  14.   

     FORM1中
    public static  string[] com1 = new string[] { "C#","C++" }; comboBox1.Items.AddRange(com1);
    FORM2中
     comboBox1.Items.AddRange(Form1.com1);