[英]C# Pass values from form 1 to form 2
我的Winforms應用程序中有2個不同的Form元素。 現在,我嘗試通過運行以下代碼將一些變量從表格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();
}
但是不知何故,Form 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?)
有人知道我在做什么錯嗎?
將變量的聲明移出。 現在,變量的范圍僅限於構造函數。 在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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.