0

私はいくつかの変数を定義し、ボタン、テキストボックスなどのコンポーネントもいくつか持っている UserControl を持っています:

private List<string> bd = new List<string>();
private List<string> bl = new List<string>();

からこれらの変数にアクセスすることは可能namespace WindowsFormsApplication1ですか? どのように?からこれを実行しようとするとprivate void recuperarOriginalesToolStripMenuItem_Click(object sender, EventArgs e)、エラーが発生しました:

bl = new List<string>();
blYear.Enabled = true;
btnCargarExcel.Enabled = true;
filePath.Text = "";
nrosProcesados.Text = "";
executiontime.Text = "";
listBox1.DataSource = null;

これを行う正しい方法は何ですか?

編集: 明確にする 私が探しているのは、メニュー項目にアクセスするたびに値を消去することです。textBox およびその他のコンポーネントの場合は、ここでの提案のおかげで機能しますが、リストの場合は null に設定する方法がわかりません

4

2 に答える 2

1

you can always access any variable that you define, any control that you place on your UserControl at all those places, where you have placed your UserControl.

Just make sure, you have made your variables public and exposed your Controls through public properties i.e

suppose you have kept a TextBox on your UserControl for Names. In order to use it outside, you must expose it through a public property

public partial class myUserControl:UserControl
{
   public TextBox TxtName{ get{ return txtBox1;}}

   public ListBox CustomListBoxName
   {
      get
      {
         return ListBox1;
      }
      set
      {
         ListBox1 = value;
      }
   }

   public List<object> DataSource {get;set;}

} 

and you can use it on the form you have dragged this usercontrol i.e.

public partial form1: System.Windows.Forms.Form
{
   public form1()
   {
       InitializeComponent();
       MessageBox.Show(myUserControl1.TxtName.Text);

       MessageBox.Show(myUserControl1.CustomListBoxName.Items.Count);
       myUserControl1.DataSource = null;   
   }
}

similary you can expose your variables, through public properties. That way you can also control whether you want some of your variables to be readonly or what!

于 2013-05-06T18:18:32.577 に答える
1

プロパティを公開してから、メイン フォームから usercontrol-instance のそのプロパティにアクセスする必要があります。

ユーザーコントロール

public List<string> BD {get; set;}

メインフォーム

MyUserControl.BD = new List<string>();
于 2013-05-06T18:19:34.367 に答える