2017-03-22 115 views
0

我正在學習C#與WinForms和控制檯應用程序。我在訪問另一個表單變量時遇到了問題。我沒有聲明新的表單對象,而是意識到要使用另一個表單中的變量,我需要使用包含該變量的同一個表單實例。更改我的表單後,我在Program.cs文件中遇到了一個錯誤,我無法修復它。如果這是一個noob錯誤,我很早就對Forms中的C#提前很抱歉。聲明的類的對象是新的我:X訪問其他窗體變量

這裏是形式背後的代碼,這樣你可以得到一個更好的理解:

frmOptions:

public partial class frmOptions : Form 
    { 
     frmHelp frmHelp1; 

     public frmOptions(frmHelp frmHelp1) 
     { 
      InitializeComponent(); 
     } 

     private void tbnDisplayHelp_Click(object sender, EventArgs e) 
     { 
      //frmHelp frmHelp = new frmHelp();  //Declare a new object for the Help form so it can be accessed 
      frmHelp1.bAccessedFromForm = true; 
      this.Close(); 
      frmHelp1.Show(); 
     } 

frmHelp:

public partial class frmHelp : Form 
{ 
    frmOptions frmOptions1; 
    frmLoginCreate frmLoginCreate1; 

    public bool bAccessedFromForm = false; 

    public frmHelp(frmOptions frmOptions1, frmLoginCreate frmLoginCreate) 
    { 
     InitializeComponent(); 
    } 

    private void btnOK_Click(object sender, EventArgs e) 
    { 
     //frmLoginCreate frmLoginCreate1 = new frmLoginCreate(); 
     this.Hide(); 
     frmLoginCreate1.Show(); 
    } 

    private void frmHelp_Load(object sender, EventArgs e) 
    { 
     if (bAccessedFromForm == true) 
     { 
      btnOK.Hide(); 
      btnReturnToForm.Show(); 
     } 
    } 

    private void btnReturnToForm_Click(object sender, EventArgs e) 
    { 
     this.Hide(); 
     frmOptions1.Show(); 
     btnOK.Show(); 
    } 
} 

這是我的Program.cs文件:

static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new frmHelp()); 
     } 
    } 

這是我在Program.cs中得到了錯誤文件:

沒有給出說法,對應於所需正式 參數的「frmOptions1「frmHelp.frmHelp(frmOptions, frmLoginCreate)」

+1

內'frmHelp'形式預計2個參數和你沒有提供的唯一'constructor',創建一個'constructor'超負荷其預期沒有參數那麼就不會有任何錯誤了。 –

+1

你在其他形式需要什麼樣的變量?你能更新你的代碼嗎? –

+1

字段和參數選擇相同的名稱,將成爲一個爛攤子。我更喜歡用'_'作爲前綴 –

回答

1

您正試圖創建frmHelp的新實例,但不傳遞任何參數。

您frmHelp類只有一個構造函數有兩個參數:

public frmHelp(frmOptions frmOptions1, frmLoginCreate frmLoginCreate) 
{ 
     InitializeComponent(); 
} 

答案很簡單,改變了以前以:與2個參數

public frmHelp() 
{ 
    InitializeComponent(); 
} 

,因爲你沒有做任何事情

0

這不直接回答您的問題,但可能有助於您的解決方案;

我寧願使用modal forms而不是正常形式。

例如:(這只是一個例子來說明如何可以做到)

public class FormMain : Form 
{ 
    // you might 'remember' the previous login name 
    private string _previousUsername; 

    private void ButtonLogin_Click(object sender, EventArgs e) 
    { 
     // create the login form 
     using(var loginForm = new FormLogin()) 
     { 
      // fillin the previous username 
      loginForm.Username = _previousUsername; 

      // this will block here, until the loginForm's DialogResult is set 
      var result = loginForm.ShowDialog(); 

      // if the form was closed (other than pressing ok), cancel login 
      if(result != DialogResult.OK) 
       return; 

      // verify loginForm.Username, loginForm.Password 
     } 
    } 

} 


public class FormLogin : Form 
{ 
    // wrapper properties, it wraps the textbox.Text, 
    // you don't want to expose the textbox directly, you might want to 
    // use some other controls in future as editbox, which will change the contract 
    // for classes that are using this control/form, which causes modifying code 
    // on more than one place, (also affects testing again). This way you can keep it 
    // separated. Classes dependend on the FormLogin, shouldn't be dependend 
    // on how it is presented.(UI) 
    public string Username 
    { 
     get { return textboxUsername.Text; } 
     set { textboxUsername.Text = value; } 
    } 

    public string Password 
    { 
     get { return textboxPassword.Text; } 
     set { textboxPassword.Text = value; } 
    } 

    private void ButtonLogin_Click(object sender, EventArgs e) 
    { 
     DialogResult = DialogResult.OK; 
    } 
} 

如果你想將數據傳遞到另一種形式,你可以使用DataHolder類。

喜歡的東西:

// dataholder class 
public class MyDataHolder 
{ 
    public string UserName { get; set; } 
    public int MyId { get; set; } 
    public List<double> Whatever { get; set; } 
    public DateTime CreatedOn { get; set; } 
} 

// form1 
public class Form1: Form 
{ 
    // a field with a reference to the dataholder 
    private MyDataHolder _myData; 

    public Form1() 
    { 
     // create the data 
     _myData = new MyDataHolder(); 
     _myData.UserName = "Unknown"; 
     _myData.CreatedOn = DateTime.Now; 
    } 

    private void Button1_Click(object sender, EventArgs e) 
    { 
     // create the form2 and pass the data 
     using(Form2 form2 = new Form2(_myData)) 
      form2.ShowDialog(); 
    } 
} 

public class Form2 : Form 
{ 
    // a field with a reference to the dataholder 
    private MyDataHolder _myData; 

    public Form2(MyDataHolder myData) 
    { 
     // don't forget to assign it to the field 
     _myData = myData; 
    } 

    private void Button1_Click(object sender, EventArgs e) 
    { 
     // show the username 
     MessageBox.Show(_myData.UserName); 
    } 
} 

現在,這兩種形式都參照相同的數據。通過這種方式,您可以在Form1上刪除Form2的依賴關係。因此,您也可以以其他形式重複使用Form2,只要您將有效的MyDataHolder傳遞給Form2即可運行。使用Form2和使用Form1爲您提供了有限的可重用性Form2一個Form1,你想重用編寫的代碼,而不是把它改寫;-)


我希望這將幫助你在你的追求....