2017-09-13 8 views
0

次の図は、私のプロジェクトの内容を示しています。ここでTextBox MainWindowのロードイベントが発生したときに発生するTextChangedイベント

My project

あなたのテストのニーズのための私のプロジェクトコードです。

<Window x:Class="MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Title="MainWindow" Height="150" Width="400"> 
<Grid> 
    <TextBox Name="TextBox1" Width="200" Height="30" HorizontalAlignment="Left" Background="Pink" /> 
    <Button Name="Button1" Content="Button1" Width="125" Height="30" HorizontalAlignment="Right" IsEnabled="False" /> 
</Grid> 
</Window> 

...

Class MainWindow 

Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded 
    TextBox1.Text = "Hello" 
End Sub 

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.Windows.Controls.TextChangedEventArgs) Handles TextBox1.TextChanged 
    Button1.IsEnabled = True 
End Sub 

End Class 

私はTextBox1_TextChangedイベント上記のコードを実行するためMainWindow_Loadedイベントを解雇しました。

MainWindow_Loadedの後にTextBox1_TextChangedイベントを発生させる方法はありますか?

答えて

0

textbo1がテキストプロパティの変更を生成していて、イベントがトリガされているとき。テキストボックスは、その中にいくつかのテキストを持っているとき、ボタンのみ有効にする必要がある場合は、この

Public Class Form1 
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    AddHandler TextBox1.TextChanged, AddressOf TextBox1_TextChanged 
End Sub 
Private Sub TextBox1_TextChanged() 
    Button1.Enabled = True 
End Sub End Class 
0

のようにロードされた形後のTextBox1にイベントハンドラを追加することができ、あなたは、イベントハンドラでそれを確認する必要があります。追加のバリデーションが必要な場合は、ここでもやります。

Private Sub TextBox1_TextChanged(sender As Object, e As TextChangedEventArgs) Handles TextBox1.TextChanged 
    Button1.IsEnabled = (TextBox1.Text.Length > 0) 
End Sub 
0

解決済み。

Private Sub TextBox1_TextChanged(sender As System.Object, e As System.Windows.Controls.TextChangedEventArgs) Handles TextBox1.TextChanged 
    If TextBox1.IsFocused Then 
     Button1.IsEnabled = True 
    End If 
End Sub 
関連する問題