2013-08-05 178 views
1
public class Simple : Form 
{ 
    public Simple() 
    { 
     Text = "Server Command Line"; 
     Size = new Size(800, 400); 
     CenterToScreen(); 
     Button button = new Button(); 
     TextBox txt = new TextBox(); 
     txt.Location = new Point (20, Size.Height - 70); 
     txt.Size = new Size (600, 30); 
     txt.Parent = this; 
     txt.KeyDown += submit; 
     button.Text = "SEND"; 
     button.Size = new Size (50, 20); 
     button.Location = new Point(620, Size.Height-70); 
     button.Parent = this; 
     button.Click += new EventHandler(sSubmit); 
    } 

    private void submit(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Enter) { 
      Console.WriteLine ("txt.Text");//How do I grab this? 
      Submit(); 
     } 
    } 
} 

我試圖從表單外部訪問txt.Text,谷歌也沒有幫助。我如何訪問它?從方法外訪問表單變量

+0

'Console.WriteLine()'在一個winform中? – 2013-08-05 22:55:07

+0

查看此msdn鏈接以獲取有關變量作用域的更多信息http://msdn.microsoft.com/en-us/library/ms973875.aspx –

+0

@ Precious1tj實際上很常見,用於故障排除目的。 –

回答

1

你的txt變量是在Simple()構造函數的局部範圍內聲明的。您將無法像訪問您的提交方法那樣訪問此範圍之外的任何地方。

您可能想要做的是在您的Simple類中創建一個私有實例變量,然後您將能夠從任何聲明屬於此類的方法訪問該變量。

例子:

public class Simple : Form 
{ 
    //now this is field is accessible from any method of declared within this class 
    private TextBox _txt; 
    public Simple() 
    { 
     Text = "Server Command Line"; 
     Size = new Size(800, 400); 
     CenterToScreen(); 
     Button button = new Button(); 
     _txt = new TextBox(); 
     _txt.Location = new Point (20, Size.Height - 70); 
     _txt.Size = new Size (600, 30); 
     _txt.Parent = this; 
     _txt.KeyDown += submit; 
     button.Text = "SEND"; 
     button.Size = new Size (50, 20); 
     button.Location = new Point(620, Size.Height-70); 
     button.Parent = this; 
     button.Click += new EventHandler(sSubmit); 
} 

private void submit(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Enter) { 
     Console.WriteLine (_txt.Text);//How do I grab this? 
     Submit(); 
    } 
} 

}

+0

啊,謝謝,那正是我需要的! –

1

你必須定義的TextBox一些變量txt在你的窗體類的地方,其實這是自動由設計師,當你拖N - 下降爲你做將TextBoxToolbox拖到您的表單上。這個變量是TextBox的一個實例。它應該使用構造函數TextBox()進行初始化,並使用您在代碼中所做的一些屬性。您可以在表格類Simple的範圍內使用此變量。它有屬性Text(類型string)可以修改或獲取顯示。要訪問一個屬性,只要使用這種模式:在使用設計形式創作[instance Name].[Property name]

public class Simple : Form 
{ 
    public Simple() 
    { 
    Text = "Server Command Line"; 
    Size = new Size(800, 400); 
    CenterToScreen(); 
    Button button = new Button(); 
    txt = new TextBox(); 
    txt.Location = new Point (20, Size.Height - 70); 
    txt.Size = new Size (600, 30); 
    txt.Parent = this; 
    txt.KeyDown += submit; 
    button.Text = "SEND"; 
    button.Size = new Size (50, 20); 
    button.Location = new Point(620, Size.Height-70); 
    button.Parent = this; 
    button.Click += new EventHandler(sSubmit); 
    } 
    TextBox txt; 
    private void submit(object sender, KeyEventArgs e) 
    { 
    if (e.KeyCode == Keys.Enter) { 
     Console.WriteLine (txt.Text); 
     Submit(); 
    } 
    } 
} 
+1

你能解釋一下你爲他做了什麼,所以很明顯嗎? :P –

+0

@MohammadAliBaydoun增加了一些非常基本的解釋,我認爲這不應該被添加。 OP應該首先閱讀一些關於'class'和'winforms'的基本書。 –

+1

@KingKing這可能是,但沒有任何解釋或上下文的代碼塊你試圖展示OP是恕我直言反生產。 –

0

默認情況下(有很好的理由)控件是私有的。您可以將其更改爲公開,但更好的解決方案是僅在Form上創建公共屬性以公開它。

public string MyTextField { get { return txt.Text; } } 

當然如果你想從外面改變它,你也可以在那裏添加一個setter。但是,請記住,如果您嘗試訪問其他線程上的控件而不是它們創建的線程上的控件,您將有一個單獨的跨線程問題需要處理,但是有關於如何處理該問題的大量帖子已經在SO上。