2017-01-24 53 views
0

我的Winforms應用程序中有2個不同的表單元素。現在我想從表1通過一些變量,通過運行下面的代碼到窗體2:C#將值從表單1傳遞給表單2

async void bunifuFlatButton2_Click(object sender, EventArgs e) 
{ 

    if (string.IsNullOrWhiteSpace(bunifuMaterialTextbox2.Text)) 
    { 
     MessageBox.Show("Please get the authentification Pin before continuing.", "Twitter Buddy error"); 
    } 
    else 
    { 
     await pinAuth.CompleteAuthorizeAsync(bunifuMaterialTextbox2.Text); 
     SharedState.Authorizer = pinAuth; 

     var credentials = pinAuth.CredentialStore; 

     if (credentials != null) 
     { 
      string oauthToken = credentials.OAuthToken; 
      string oauthTokenSecret = credentials.OAuthTokenSecret; 
      string screenName = credentials.ScreenName; 
      ulong userID = credentials.UserID; 

      new Form1(oauthToken, oauthTokenSecret, screenName, userID).Show(); 
      this.Hide(); 
     } 
     else 
     { 
      MessageBox.Show("Please authorize this application before continuing.", "Twitter Buddy error"); 
     } 
    } 
} 

現在我想接受第二表單中的值是這樣的:

public Form1(string oauthToken, string oauthTokenSecret, string screenName, ulong userID) 
{ 
    string OauthToken = oauthToken; 
    string OauthTokenSecret = oauthTokenSecret; 
    string ScreenName = screenName; 
    ulong UserID = userID; 

    InitializeComponent(); 
} 

但不知怎的,表格2不讓我使用這些值。在測試執行以下操作:

MessageBox.Show(this.OauthToken); 

並收到以下錯誤消息:

Error CS1061 'Form1' does not contain a definition for 'OauthToken' and no extension method 'OauthToken' accepting a first argument of type 'Form1' could be found (are you missing a using directive or an assembly reference?) 

有沒有人有一個想法是什麼,我做錯了什麼?

回答

2

移動變量的聲明。現在變量的範圍僅限於構造函數。詳細瞭解變量here on MSDN的範圍。

string OauthToken; 
string OauthTokenSecret; 
string ScreenName; 
ulong UserID; 

public Form1(string oauthToken, string oauthTokenSecret, string screenName, ulong userID) 
{ 
    this.OauthToken = oauthToken; 
    this.OauthTokenSecret = oauthTokenSecret; 
    this.ScreenName = screenName; 
    this.UserID = userID; 

    InitializeComponent(); 
} 
相關問題