我正在使用 VS2010 , WinForm
我必须创建一个输出窗体的类。我做了它以在那种形式做一些动作,最后将一些值传递给它的父。将数据发送到父部分很好。我已经测试并且满意了。
但是当我想在我所制作的相同形式之间采取某些行动时,这会产生一个问题。我想创建事件并告诉这些事件会做什么。我生成了事件处理程序,也可以转到事件。但是在事件处理程序中我无法访问我创建的任何控件。我只想访问我在显示方法中创建的所有控件来自我的活动 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;
}
}
}
因为我对设计没有任何问题,所以我试图在这里避免大部分设计代码,因为它越来越大。
答案 0 :(得分:1)
从其他函数访问本地函数变量是不可能的。您必须将它们声明为全局变量。
答案 1 :(得分:0)
您可以将控制变量声明为父表单中的字段,也可以指定其Name
属性并使用Find
方法访问它们:
form.Controls.Find("control name", true);
(只要您将form
声明为父表单中的字段)。