0

私はVS2010WinFormを使用しています

Windowsフォームを出力するクラスを作成する必要がありました。そのフォームで何らかのアクションを実行するために作成し、最終的にその親に何らかの値を渡します。親パーツへのデータ送信はうまくいきます。私はテストして満足しています。

しかし、私が作ったのと同じフォームの間に何らかのアクションを持たせたい場合、ここで問題が発生します。イベントを作成し、これらのイベントが何をするかを伝えたいです。イベントハンドラーを生成し、イベントにも移動できます。しかし、イベントハンドラーでは、作成したコントロールにアクセスできませんShowメソッドで作成したすべてのコントロールにアクセスしたいだけですイベントからbuttonConnectServer_Click

ここに私のコードがあります:

CustomSettingBox.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace ServerConnector
{
    class CustomSettingBox
    {
        ConnectionSettings CS = new ConnectionSettings();

    public List<string> GetServerName()
    {
        StreamReader sr = new StreamReader("Settings//ServerList.txt");
        string line = "";
        List<string> lstServerName = new List<string>();
        while ((line = sr.ReadLine()) != null)
        {
             lstServerName.Add(line.Trim());
        }
        sr.Close();
        return lstServerName;
    }





**public void buttonConnectServer_Click(object sender, EventArgs e)
        { MessageBox.Show("Done!");   `//this message is succesfully shown. but i cannot get access any of my controls defined in Show() method.`

        }**

    public DialogResult Show(ref string ServerName, ref string AuthenTicationType)
    {
        Form form = new Form();

        Label labelServerName = new Label(); //otherlabels here

        TextBox textBoxLogin = new TextBox(); //other textboxs here

        ComboBox comboboxServerName = new ComboBox(); //more combo's

        ListBox listboxAllColumn = new ListBox(); // more listbox's

        Button buttonConnectServer = new Button();
        Button buttonOk = new Button();
        Button buttonCancel = new Button();

        form.Text = "Add Setting";
        labelServerName.Text = "Select a Server";//other labels are defined


        List<string> lstServerName = GetServerName();//gets servername from a text
        foreach (string tmp in lstServerName)
        {
            comboboxServerName.Items.Add(tmp);
        }

        comboboxAuthenticationType.Items.Add("Wndows Server Authentication");
        comboboxAuthenticationType.Items.Add("SQL Server Authentication");


        buttonOk.DialogResult = DialogResult.OK;
        buttonCancel.DialogResult = DialogResult.Cancel;


        **buttonConnectServer.Click +=new System.EventHandler(this.buttonConnectServer_Click);**

        labelServerName.SetBounds(9, 20, 100, 13);//others are here

        comboboxServerName.SetBounds(120, 20, 200, 30);//others are here

        //comboboxServerName.SetBounds(12, 75, 372, 27);
        buttonOk.SetBounds(228, 450, 75, 23);
        buttonCancel.SetBounds(309, 450, 75, 23);

        labelServerName.AutoSize = true;//otheres are here

        comboboxServerName.Anchor = comboboxServerName.Anchor | AnchorStyles.Right; // others are here

        buttonConnectServer.Anchor = buttonConnectServer.Anchor | AnchorStyles.Right;
        buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

        form.ClientSize = new Size(600, 500);
        form.Controls.AddRange(new Control[] { labelServerName, labelAuthenticationType, labelLogin, labelPassword, labelDbName, labelTableName, labelSelectColumn, labelWhereColumn, comboboxServerName, comboboxAuthenticationType, textBoxLogin, textboxPassword, comboboxDbName, comboboxTableName, listboxAllColumn, listboxSelectColumn, listboxWhereColumn, buttonConnectServer, buttonOk, buttonCancel });

        form.FormBorderStyle = FormBorderStyle.FixedDialog;
        form.StartPosition = FormStartPosition.CenterScreen;
        form.MinimizeBox = false;
        form.MaximizeBox = false;
        form.AcceptButton = buttonOk;
        form.CancelButton = buttonCancel;

        DialogResult dialogResult = form.ShowDialog();

        ServerName = textBoxLogin.Text;
        AuthenTicationType = comboboxServerName.SelectedItem.ToString();
        return dialogResult;
    }
}

}

私はデザインに問題がないので、ここではデザイン コードのほとんどを避けようとしました。

4

2 に答える 2

0

コントロール変数を親フォームのフィールドとして宣言するか、Nameプロパティを割り当ててFindメソッドを使用してアクセスできます。

form.Controls.Find("control name", true);

form(親フォームでフィールドとして宣言する限り)。

于 2012-12-27T13:17:52.293 に答える