C# 从自定义WinForm中的自定义事件处理程序访问控件

C# 从自定义WinForm中的自定义事件处理程序访问控件,c#,winforms,events,event-handling,C#,Winforms,Events,Event Handling,我正在使用VS2010,WinForm 我必须创建一个类来输出windows窗体。我制作它是为了在这种形式下做一些动作,并最终将一些价值传递给它的父母。将数据发送到父部件的工作做得很好。我已经测试并满意了 但是,当我想在我所做的相同形式之间进行一些操作时,这里就产生了一个问题。我想创建事件并告诉这些事件将做什么。我生成了事件处理程序,也可以转到该事件。但是在事件处理程序中,我无法访问我创建的任何控件。我只想从我的事件按钮连接服务器\u单击 这是我的密码: CustomSettingBox.cs

我正在使用VS2010WinForm

我必须创建一个类来输出windows窗体。我制作它是为了在这种形式下做一些动作,并最终将一些价值传递给它的父母。将数据发送到父部件的工作做得很好。我已经测试并满意了

但是,当我想在我所做的相同形式之间进行一些操作时,这里就产生了一个问题。我想创建事件并告诉这些事件将做什么。我生成了事件处理程序,也可以转到该事件。但是在事件处理程序中,我无法访问我创建的任何控件。我只想从我的事件按钮连接服务器\u单击

这是我的密码:

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;
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用系统图;
使用System.IO;
命名空间服务器连接器
{
类CustomSettingBox
{
ConnectionSettings CS=新的ConnectionSettings();
公共列表GetServerName()
{
StreamReader sr=新的StreamReader(“Settings//ServerList.txt”);
字符串行=”;
List lstServerName=新列表();
而((line=sr.ReadLine())!=null)
{
lstServerName.Add(line.Trim());
}
高级关闭();
返回lstServerName;
}
**public void按钮连接服务器\单击(对象发送方,事件参数e)
{MessageBox.Show(“Done!”);`//此消息已成功显示。但我无法访问Show()方法中定义的任何控件`
}**
公共对话框结果显示(参考字符串服务器名、参考字符串身份验证类型)
{
表单=新表单();
Label labelServerName=新标签();//此处为其他标签
TextBox textBoxLogin=new TextBox();//此处有其他TextBox
ComboBox comboboxServerName=新ComboBox();//更多组合
ListBox listboxAllColumn=new ListBox();//更多ListBox
Button Button连接服务器=新建按钮();
按钮按钮OK=新按钮();
Button Button取消=新按钮();
form.Text=“添加设置”;
labelServerName.Text=“选择服务器”;//定义了其他标签
List lstServerName=GetServerName();//从文本中获取服务器名
foreach(lstServerName中的字符串tmp)
{
comboboxServerName.Items.Add(tmp);
}
comboboxAuthenticationType.Items.Add(“Wndows服务器身份验证”);
comboboxAuthenticationType.Items.Add(“SQL Server身份验证”);
buttonOk.DialogResult=DialogResult.OK;
buttonCancel.DialogResult=DialogResult.Cancel;
**buttonConnectServer.Click+=新建System.EventHandler(此.buttonConnectServer\u单击)**
labelServerName.SetBounds(9,20,100,13);//其他人在这里
comboboxServerName.SetBounds(120、20、200、30);//其他人在这里
//comboboxServerName.SetBounds(12,75,372,27);
按钮立根(228450 75 23);
按钮中心立根(309450,75,23);
labelServerName.AutoSize=true;//其他的在这里
comboboxServerName.Anchor=comboboxServerName.Anchor | AnchorStyles.Right;//其他人在这里
buttonConnectServer.Anchor=buttonConnectServer.Anchor | AnchorStyles.Right;
按钮OK.Anchor=主播样式。底部|主播样式。右侧;
buttonCancel.Anchor=主播样式。底部|主播样式。右侧;
form.ClientSize=新尺寸(600500);
form.Controls.AddRange(新控件[]){labelServerName,labelAuthenticationType,labelLogin,labelPassword,labelDbName,labelTableName,labelSelectColumn,labelWhereColumn,comboboxServerName,comboboxAuthenticationType,textBoxLogin,textboxPassword,comboboxDbName,comboboxTableName,listboxAllColumn,listboxSelectColumn,listboxWhereColumn,ButtonConConnectionServer,buttonOk,buttonCancel});
form.FormBorderStyle=FormBorderStyle.FixedDialog;
form.StartPosition=FormStartPosition.CenterScreen;
form.ebox=false;
form.ebox=false;
form.AcceptButton=按钮OK;
form.CancelButton=按钮取消;
DialogResult=form.ShowDialog();
ServerName=textBoxLogin.Text;
AuthenTicationType=comboboxServerName.SelectedItem.ToString();
返回对话框结果;
}
}
}


由于我对设计没有问题,因此我尽量避免使用这里的大多数设计代码,因为代码越来越大。

无法从另一个函数访问局部函数变量。您必须将它们声明为全局变量。

您可以在父窗体中将控制变量声明为字段,也可以指定它们的
名称
>属性并使用
Find
方法访问它们:

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

(只要您将
表单
声明为父表单中的一个字段)。

如何尝试从何处访问它们?我想从buttonConnectServer_Click()访问,并访问我在Show方法中声明的所有控件。