我有2種形式:Form1和Form2。我用打開窗體2:如何在Form2中使用Form1中的變量?
Form2 newForm2 = new Form2(this);
現在我要訪問從Form1中某些變量或方法,這些方法在公共設置,如:public int counter;
但是當我嘗試這從Form2的它給了我一個錯誤:
Error 4 'System.Windows.Forms.Form' does not contain a definition for 'StartGame' and no extension method 'StartGame' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?)
編輯: 在形式1:
Form2 newForm2 = new Form2(answer, button3, button4, button5, button6, this, fiftyfifty, web, change);
newForm2.Show();
在表格2:
Form originalParent;
public Form2(int answer, Button button3, Button button4, Button button5, Button button6, Form parentform, int fiftyfifty, int web, int change)
{
InitializeComponent();
originalParent = parentform;
}
,我試圖訪問這個喜歡originalParent."public Method here"
,它給我的錯誤。
你是否從'Form1'打開'Form2'? – mydogisbox
您需要一個'Form1'類型的變量。你有這樣一個變量嗎?請顯示一些代碼。 –