0

我正在使用VS2010WinForm

我必须创建一个将输出 windows 窗体的类。我这样做是为了以这种形式执行一些操作,并最终将一些值传递给它的父级。将数据发送到父部件做得很好。我已经测试过了,很满意。

但是当我想在我制作的相同表格之间进行一些操作时,这里会产生一个问题。我想创建事件并告诉这些事件将做什么。我生成了事件处理程序,也可以去事件。但在事件处理程序中,我无法访问我创建的任何控件。我只想从我的事件按钮ConnectServer_Click中访问我在Show方法中创建的所有控件

这是我的代码:

自定义设置框.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

1

从另一个函数访问局部函数变量是不可能的。您必须将它们声明为全局变量。

于 2012-12-27T13:14:13.870 回答
0

您可以将控制变量声明为父表单中的字段,也可以分配它们的Name属性并使用以下Find方法访问它们:

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

(只要您form在父表单中声明为字段)。

于 2012-12-27T13:17:52.293 回答