C#将值从表单1传递到表单2

时间:2017-01-24 12:45:53

标签: c#

我在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?)

有没有人知道我做错了什么?

1 个答案:

答案 0 :(得分:2)

移出变量的声明。现在变量的范围仅限于构造函数。阅读有关变量范围的更多信息here on MSDN

var cps = DateComponents()
cps.year = 2017
cps.month = 1
cps.day = 8
cps.hour = 18
cps.minute = 30

print(cps)

var cal = Calendar(identifier: .gregorian)
let localDate = cal.date(from: cps)!
print(localDate) // 2017-01-08 10:30:00 +0000

let timezone = TimeZone(abbreviation: "UTC")
cal.timeZone = timezone!
let utcDate = cal.date(from: cps)!
print(utcDate) // 2017-01-08 18:30:00 +0000