2013-07-13 50 views
1

到目前为止,如果用户输入内容,我将存储在标签属性中。我知道这是不对的。我如何根据用户输入更新变量,以便在需要使用它的任何事件中使用?将用户输入保存为c#windows窗体中的变量

这是我尝试过的很多事情之一。我甚至无法找出正确的搜索条件来搜索我需要做的解决方案。

namespace Words 
{ 
    public partial class formWords : Form 
    { 
    int x = 5; 
    int y = 50; 
    int buttonWidth = 120; 
    int buttonHeight = 40; 
    string fileList = ""; 
    string word = ""; 
    string wordFolderPath = @"C:\words\";// this is the variable I want to change with the dialog box below. 

    private void selectWordFolderToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     FolderBrowserDialog folder = new FolderBrowserDialog(); 
     if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      string folderPath = folder.SelectedPath; 
      formWords.wordFolderPath = folderPath; 
     } 
    } 

回答

2

wordFolderPath是你的班级的公开变量(但在其外部是私人的)。这意味着您班级内的任何内容都可以自由读取/写入数值。

至于你的语法,你可以使用变量名或使用this.

private void DoAThing() 
{ 
    wordFolderPath = "asdf"; 
    this.wordFolderPath = "qwerty"; //these are the same 
} 

访问内部变量时,不能使用当前类的名称。 formWords是一种类型,而不是一个实例。

使用this的唯一好处是因为在方法中定义具有相同名称的变量是合法的。使用这个关键字确保你在谈论这个班级的成员。

+0

我认为这比以前复杂得多。谢谢。 –

2

只是改变formWords.wordFolderPath = folderPath;

wordFolderPath = folderPath;

this.wordFolderPath = folderPath;

应该解决您的问题

而且,本来应该在错误列表中说:“一个编译器错误对象引用是非静态字段,方法或属性所必需的...“

如果你没有看到你的错误列表,你应该打开它。

相关问题