2016-12-01 85 views
0

我一直在試圖找出在文本框中輸入文本時禁用文本框。我能夠做到這一點,但我也遇到了另一個問題,比如說你有一個帶有「歡迎」字樣的文本框。如果我編輯並添加更多字母,即添加SSS的「WelcomeSSS」,則啓用文本。但是,當我從該文本框中刪除「SSS」時,按鈕仍處於啓用狀態,因爲文本與編輯前相同,所以不會禁用。當在文本框中正確輸入文本並在c中添加對話框時禁用按鈕#

如何確保在這種情況下禁用文本?

而且我想添加對話框,當用戶點擊不同的按鈕去不同的頁面而不保存編輯的內容。我該怎麼做呢?

這是到目前爲止我的代碼:

private void textbox1_IsChanged(object sender, KeyEventArgs e) 
{ 
//SaveButton.IsEnabled = !string.IsNullOrEmpty(TextBox1.Text); 
if (TextBox1.Text.Trim().Length > 0) 
{ 
SaveButton.IsEnabled = true; 
} 

if (WpfHelpers.Confirmation(resources.QuitWithoutSaving, resources.Changes)) 
{ 

} 
} 

這是在使用WPF KeyUp事件處理程序。

+0

就這樣,我更好地理解這一點......你想禁用一個文本框,如果某個短語/單詞在裏面? – ephtee

+0

是的基本上,我想要的是當文本框爲空時,禁用按鈕。當它不是空的,然後啓用。我可以用上面的代碼來做這件事。我還想要做的是,如果文本框中已有文本可以說「歡迎」,當我嘗試編輯它並使其成爲「WelcomeSSS」時,它將啓用,但是如果您從文本框中刪除SSS並且使之成爲「歡迎」的方式應該是禁用文本框之前。 – user6574269

+0

我們都花了很多時間回答你的問題。所以請付出一些努力來提問。例如。你想禁用文本框「如何確保文本在這種情況下被禁用?」或按鈕「SaveButton.IsEnabled = true」?請先分開你問過的問題,現在你想要什麼。這將幫助人們理解下面的一些答案。 – user2154065

回答

0

如果我理解正確你的問題......

private void textbox1_IsChanged(object sender, KeyEventArgs e) 
{ 

    if (textbox1.Text == "Welcome"){ 
    SaveButton.IsEnabled = false; 
    } 
    else{ 
    SaveButton.IsEnabled = true; 
    }  
} 
0

你需要存儲保存的值的數據結構。例如。字符串列表。在以下片段中,這些值存儲在SavedTextBoxTexts列表中。

首先,SaveButton被禁用(您也可以在XAML中執行此操作)。當點擊SaveButton時,textBox1.text值將被存儲在列表中,並且按鈕被禁用。

當編輯textBox1.text並且SaveButton存在(已經)時,將檢查不同的條件。

如果textBox1.text已存儲在SavedTextBoxTextstextBox1.text爲空或僅包含空格字符,則SaveButton被禁用。否則,SaveButton將被啓用。

public partial class MainWindow : Window 
{ 
    private List<string> SavedTextBoxTexts = new List<string>(); 

    public MainWindow() 
    { 
     InitializeComponent(); 

     // Disable button right from the beginning. 
     SaveButton.IsEnabled = false; 
    } 

    private void textBox1_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     // The function may be called, while the window has not been created completely. 
     // So we have to check, if the button can already be referenced. 
     if (SaveButton != null) 
     { 
      // Check if textBox1 is empty or 
      // textBox1.text is already in the list of saved strings. 
      if (String.IsNullOrEmpty(textBox1.Text) || 
       textBox1.Text.Trim().Length == 0 || 
       SavedTextBoxTexts.IndexOf(textBox1.Text.Trim()) >= 0) 
      { 
       // Disable Button 
       SaveButton.IsEnabled = false; 
      } 
      else 
      { 
       // If textBox1.text has not been saved already 
       // or is an empty string or a string of whitespaces, 
       // enable the SaveButton (again). 
       SaveButton.IsEnabled = true; 
      } 
     } 
    } 

    private void SaveButton_Click(object sender, RoutedEventArgs e) 
    { 
     // Store the text in textBox1 into the SavedTextBoxTexts list. 
     SavedTextBoxTexts.Add(textBox1.Text.Trim()); 

     // Disable the SaveButton. 
     SaveButton.IsEnabled = false; 
    } 

    // This is executed, when the other button has been clicked. 
    // The text in textBox1 will not be saved. 
    private void AnotherButton_Click(object sender, RoutedEventArgs e) 
    { 
     if (WpfHelpers.Confirmation(resources.QuitWithoutSaving, resources.Changes)) 
     { 
      // Move to other page ... 
     } 
    } 
} 
+0

沒有默認值。 「歡迎」只是一個例子。通常情況下,文本框是空的。當您在空文本框中添加一些文本「abc」時,保存按鈕將被啓用並保存。 「abc」被保存。然後如果編輯內容爲「abc」的文本框,編輯它並將其設置爲「abcddd」並刪除ddd而不單擊保存,則按鈕仍處於啓用狀態。我想要的是如果文本處於最後狀態(即「abc」),按鈕將被禁用。 – user6574269

+0

此工作是否適用於多個保存的值或僅適用於最新保存的值? – user2154065

+0

多個保存的值。 – user6574269

相關問題