[英]Accessing Control from Custom Event Handler in Custom WinForm
我正在使用VS2010 , WinForm
我必须创建一个将输出Windows窗体的类。 我这样做是为了以这种形式执行一些操作,并最终将一些值传递给它的父级。 将数据发送到父部件的操作很好。 我已经测试并满意。
但是,当我想在我做过的相同形式之间进行某些操作时,就会出现问题。 我想创建事件并告诉这些事件将要做什么。 我生成了事件处理程序,也可以去事件。 但是在事件处理程序中,我无法访问我创建的任何控件 。 我只想访问我在Show方法中创建的所有控件,请单击我的事件按钮。
这是我的代码:
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;
}
}
}
因为我对设计没有问题,所以我尝试避免这里的大多数设计代码,因为它越来越大。
从另一个函数访问局部函数变量是不可能的。 您必须将它们声明为全局变量。
您可以将控制变量声明为父表单中的字段,或者分配其Name
属性并使用Find
方法访问它们:
form.Controls.Find("control name", true);
(只要您将form
声明为父表单中的字段)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.