[英]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.