在網上搜索了兩天而沒有找到可以正確理解的解決方案之後,我必須在這裏請求答案。從窗體訪問班級,反之亦然
我有一個windows窗體應用程序,寫在vb.net和工作正常。我已決定重寫本中,我想不會是太大的問題,但是C#...
我在項目兩類:
FormJobs & AppJobs
FormJobs包含的方法和以某種方式修改表單的函數。
AppJobs包含所有其他方法(檢查,掃描等)的方法和函數。
在我的主窗體(FrmStart)上,On_load事件使用AppJobs中的函數來檢查網絡是否啓動(public bool CheckNetConnection),然後檢查以確保存在根保存文件夾(public void CheckRoot)。
如果CheckNetConnection爲false或CheckRoot不存在,則FormJobs類中的某個方法將某些按鈕設置爲禁用,某些標籤顯示錯誤信息以及設置窗體高度。
上述工作在VB.net,但我不斷收到StackOverflowException或NullReferenceException與C#代碼。
我知道異常的原因是因爲兩個類和窗體都在不斷調用對方,所以我知道我需要刪除這段代碼,但我不知道如何讓每個類和窗體訪問對方。這顯然是不好的設計,因爲我剛剛開始學習C#,所以任何幫助,將不勝感激。
但我的主要問題是: - 我如何獲得一個表單來訪問多個類? 允許類訪問對方? 讓這些類對錶單進行更改?
FrmStart代碼
AppJobs Appjobs = new AppJobs();
private void FrmStart_Load(object sender, EventArgs e)
{
KeyPreview = true;
if (Appjobs.CheckNetConnection(this) == true)
{
Appjobs.CheckRoot(this);
}
AppJobs代碼
public class AppJobs
{
FormJobs Formjobs = new FormJobs();
public string AppRoot = Properties.Settings.Default.DefaultFolder;
public string DefaultDevice = Properties.Settings.Default.DefaultScanner;
public bool NoDirectory = false;
DialogResult MsgBoxQuestion;
public bool CheckNetConnection(Form StartForm)
{
IPHostEntry ServerIP = new IPHostEntry();
bool ConnectedToServer = false;
string CurrentRoot = "MyServer";
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
try
{
IPHostEntry DNSTest = Dns.GetHostEntry(CurrentRoot);
if (DNSTest.AddressList.Length > 0)
{
ConnectedToServer = true;
}
else
{
ConnectedToServer = false;
}
}
catch (System.Net.Sockets.SocketException ex)
{
ConnectedToServer = false;
}
}
return ConnectedToServer;
}
public void CheckRoot(Form StartForm)
{
if (string.IsNullOrEmpty(AppRoot))
{
Formjobs.SetHeight(StartForm);
return;
}else if(AppRoot == "0")
{
Formjobs.SetHeight(StartForm);
return;
}
else
{
if ((!Directory.Exists(AppRoot)))
{
NoDirectory = true;
MsgBoxQuestion = MessageBox.Show(AppRoot + " is set, but the directory does not exist." + Environment.NewLine
+ Environment.NewLine + "Would you like to create the folder now?", "Root folder missing", MessageBoxButtons.YesNo);
if (MsgBoxQuestion == DialogResult.Yes)
{
Directory.CreateDirectory(AppRoot);
NoDirectory = false;
}
else
{
MessageBox.Show("You will not be able to use this program until you create a root folder.", "No root folder selected",MessageBoxButtons.OK);
}
}
}
}
}
FormJobs代碼
public class FormJobs
{
AppJobs Appjobs = new AppJobs();
public void SetHeight(Form StartForm)
{
if (Appjobs.AppRoot == null | Appjobs.AppRoot == "0") {
if (Appjobs.DefaultDevice == null | Appjobs.DefaultDevice == "0") {
if (StartForm.Controls["MenuStrip1"].Visible == true) {
StartForm.Height = 167;
StartForm.Controls["LblNoRoot"].Visible = true;
StartForm.Controls["LblNoRoot"].Location = new Point(0, 24);
StartForm.Controls["LblNoRoot"].Text = "There is no root folder selected. Please select a root folder to continue.";
StartForm.Controls["LblNoDevice"].Visible = true;
StartForm.Controls["LblNoDevice"].Location = new Point(0, 48);
StartForm.Controls["LblNoDevice"].Text = "There is no default device selected. Please select a default device to continue.";
StartForm.Controls["BtnOkTickets"].Enabled = false;
StartForm.Controls["BtnQueryTickets"].Enabled = false;
StartForm.Controls["BtnSearch"].Enabled = false;
}else
{
StartForm.Height = 147;
StartForm.Controls["LblNoRoot"].Visible = true;
StartForm.Controls["LblNoRoot"].Location = new Point(0, 9);
StartForm.Controls["LblNoRoot"].Text = "There is no root folder selected. Please select a root folder to continue.";
StartForm.Controls["LblNoDevice"].Visible = true;
StartForm.Controls["LblNoDevice"].Location = new Point(0, 33);
StartForm.Controls["LblNoDevice"].Text = "There is no default device selected. Please select a default device to continue.";
StartForm.Controls["BtnOkTickets"].Enabled = false;
StartForm.Controls["BtnQueryTickets"].Enabled = false;
StartForm.Controls["BtnSearch"].Enabled = false;
}
}
https://stackoverflow.com/help/mcve – mjwills
請與我們分享您的不工作的C#代碼。我很確定這個錯誤是在你的實現中,而不是你在這裏爲我們描述的邏輯。 – Pio
無法看到代碼就無法修復代碼。至於一般的佈局,我不會讓'FormJobs' **改變**形式,而是暴露一個狀態(例如'bool DisableAllButtons'),並且擁有每一種形式(在創建時,或者在接收到一個事件的狀態改變)**觀察**暴露的狀態並相應地進行調整。隨意改變對方**是近乎不可維護的代碼的溫牀。根據他們的環境,自己的班級/表格可以自行更改**,而且更易於管理。 – Flater