2011-09-08 62 views
0

我是C#的新手,所以仍然找到我的方法。_textChanged事件給我錯誤「對象引用未設置爲對象的實例」

我有一個按鈕,我只想在用戶輸入文本到文本框時啓用。 我得到這個錯誤 - 「對象引用未設置爲對象的實例」。

下面是相關的代碼(不使用和變量):

public MainWindow() 
    { 
     MessageBox.Show("Make sure to edit Settings tab."); 
     InitializeComponent(); 
     if (startTextBox.Text == "0") // Checks to see if a textbox has some text other than zero. if no than the user cannot press button1 yet. 
     { 
      button1.IsEnabled = false; 
     } 
     else 
     { 
      button1.IsEnabled = true; 
     } 

    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 

     if (radioButton1.IsChecked == false) 
     { 
      label17.Content = "No Hourly wage was set."; 
     } 

    } 

    private void add(object sender, RoutedEventArgs e) /// here is a very long method so I've removed its content. 


    } 


    public void printTime() 
    { 

     int Sum = (this.EndInt - this.StartInt); 
     int Money = (Sum * this.L1001); 


     label16.Content = Sum; 
     label17.Content = Money; 
     if ((textBox1.Text == "0") && ((textBox2.Text == "0") || (textBox3.Text == "0"))) 
     { 
      label17.Content = "No Hourly wage was set."; 
     } 
    } 

    public void printTime2() 
    { 

     int Sum = (this.EndInt - this.StartInt); 
     MessageBox.Show("Is it possible that you've worked - " + Sum + " Hours?"); 
    } 

    public void printTime3() 
    { 

     int Sum = (this.EndInt - this.StartInt); 
     int Money = (Sum * this.L1001); 

     label16.Content = Sum; 
     label17.Content = Money; 
     if (textBox1.Text == "0") 
     { 
      label17.Content = "No Hourly wage was set."; 
     } 
    } 


    public int Convert(String S) 
    { 
     int i = int.Parse(S); 
     return i; 
    } 


    // Input Validation For Excepting Integers Only! 
    private void input(object sender, TextCompositionEventArgs e) 
    { CheckIsNumeric(e); } 
    private void CheckIsNumeric(TextCompositionEventArgs e) 
    { 
     int result; if (!(int.TryParse(e.Text, out result) || e.Text == ".")) 
     { e.Handled = true; MessageBox.Show("Numbers Only"); } 

    } 


    private void startTextBox_TextChanged(object sender, TextChangedEventArgs e) 
    { 

     button1.IsEnabled = true; 
    } 




} 

}

+0

你在哪裏得到的錯誤,我不認爲它在你們中間發表 – V4Vendetta

回答

1

它的範圍問題。你沒有顯示button1的定義。但是在你的事件處理程序startTextBox_TextChanged中,button1定義無處可尋(實際上它也需要實例化)。由於您嘗試在尚未實例化的對象(button1)上調用方法,因此拋出了該異常。

如果您發佈的不僅僅是這些片段,我或其他人可能會進一步幫助您。

+0

我已經編輯我的片段顯示整個代碼減去點擊一個按鈕(私人無效附加一個非常大的事件代碼的代碼示例(對象發件人,RoutedEventArgs e))。你能找到我這個問題嗎? – Yosi199

相關問題