3

私はプログラミングが初めてで、現在 C# Windows Forms アプリケーションに取り組んでいます。

問題は次のとおりです。タブページ、テキストボックス、タイマーなどのさまざまなオブジェクトとコントロールを含むフォームがあります。メインフォームのタブページの1つにロードするUserControlフォームもあります。

UserControl にコードを書きたいのですが、メイン Form の要素プロパティを操作するにはどうすればよいですか。

例: UserControl フォームのボタンをクリックすると、メイン フォームの timer.Enabled コントロールが true に設定されます。

4

5 に答える 5

4

これを行うことは可能ですが、ユーザー コントロールにアクセスさせてフォームを操作させるのは、最もクリーンな方法ではありません。ユーザー コントロールにイベントを発生させ、ホスト フォームにイベントを処理させる方がよいでしょう。(例えば、ボタンのクリックを処理する際に、フォームはタイマーを有効/無効にすることができます)

そうすれば、必要に応じて、さまざまなフォームに対してさまざまな方法でユーザー コントロールを使用できます。何が起こっているのかがより明確になります。

更新: ユーザー コントロール内で、イベントを宣言できます。ボタンのクリックで、イベントを発生させます。

namespace WindowsFormsApplication1
{
    public partial class UserControl1 : UserControl
    {
        public event EventHandler OnButtonClicked;


        public UserControl1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            EventHandler handler = OnButtonClicked;

            // if something is listening for this event, let let them know it has occurred
            if (handler != null)
            {
                handler(this, new EventArgs());
            }

        }
    }
}

次に、フォーム内にユーザー コントロールを追加します。その後、イベントにフックできます。

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            userControl11.OnButtonClicked += userControl11_OnButtonClicked;
        }

        void userControl11_OnButtonClicked(object sender, EventArgs e)
        {
            MessageBox.Show("got here");
        }


        }
    }
于 2013-04-04T15:23:41.413 に答える
1

何を達成しようとしているのかを再考する必要があるかもしれません。ただし、あなたの質問に答えるために、それは可能です。

これを行う最善の方法は、MainForm と呼ばれる UserControl にプロパティを作成することです。

public Control MainForm {
    get;
    set;
}

次に、MainForm の Load イベントで、プロパティをそれ自体に設定します。

userControl1.MainForm = this;

最後に、ユーザー コントロールで MainForm のタイマーを設定します。

protected button_click(object sender, EventArgs e)
{
    timerName = "timer1";
    EnableTimer(timerName);
}

private void EnableTimer(timerName)
{
    var timer = MainForm.Controls.FirstOrDefault(z => z.Name.ToLower().Equals(timerName.ToLower());
    if (timer != null)
    {
         ((Timer)timer).Enabled = true;
    } else {
         // Timer was not found
    }
}
于 2013-04-04T15:34:27.437 に答える
1

timer1.Modifiersプロパティを「内部」に設定し、次のインスタンスでアクセスできますForm1

form1.timer1.Enabled = true;

Form1クラス自体ではなく、クラスのインスタンスが必要です。例えば:

// INVALID
Form1.timer1.Enabled = true;

// VALID
var form1 = Form1.ActiveForm;
form1.timer1.Enabled = true;

しかし、これはこれを行うためのあまりきれいな方法ではありません.NDJの回答で説明されているようにイベントを使用することをお勧めします.

于 2013-04-04T15:26:28.580 に答える
1

これは非常に簡単です。いわゆるイベントです。ユーザー コントロールでは、フォームがサブスクライブする EventHandler を使用してイベントを公開します。

public partial class MyUserControl : UserControl
{
    /// You can name the event anything you want.
    public event EventHandler ButtonSelected;

    /// This bubbles the button selected event up to the form.
    private void Button1_Clicked(object sender, EventArgs e)
    {
         if (this.ButtonSelected != null)
         {
              // You could pass your own custom arguments back to the form here.
              this.ButtonSelected(this, e)
         }
    }
}

ユーザー コントロール コードができたので、それをフォーム コードに実装します。おそらく、フォームのコンストラクターには、以下のようなコードがあります。

MyUserControl ctrl = new MyUserControl();
ctrl.ButtonSelected += this.ButtonSelected_OnClick;

最後に、フォーム コードには、Timer enabled を true に設定する以下のコードのようなイベントをサブスクライブするメソッドがあります。

private void ButtonSelected_OnClick(object sender, EventArgs e)
{
    this.Timer1.Enabled = true;
}

これが、フォーム上のユーザー コントロールでイベントを許可し、フォーム上のオブジェクトを設定する方法です。

于 2013-04-04T15:49:55.680 に答える