WPF reference and collection
Dec 16, 2009 12:54 PST
1/ In windows Forms, when we want to call a Control in the form from Module we refer to it by:
For example if we want assign the Text of a TextBox to a String variable from Module:
This is not working in WPF, I had to define in code in Module level as Public a Window As Window1, set it to be equal to Window1 in Window1_Loaded and close it in Window1_Close, then I could use it to pick up data from Text boxes in Window1.
Here is the code:
Public WindowBuffer As New Window1
Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
WindowBuffer = Me
Private Sub Window1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
2/ I was surprised when I noticed that TabIndex of the text boxes in Window1 was 2147483647
I set them from 0 to the number of text boxes I have increasing by 1, in order to use the For Each statement to move the focus from one TextBox to the other on Key.Enter. Here again I had to define a Collection to which I added all my text boxes then I could use this :
Private Sub Text_GotFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles TxtGrade.GotFocus, TxtPort.GotFocus, TxtRef.GotFocus, TxtVessel.GotFocus
TextBoxIndex = sender.tabindex
Private Sub Txt(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles TxtVessel.KeyDown, TxtGrade.KeyDown, TxtPort.KeyDown
If e.Key = Key.Enter Then
For Each Control As TextBox In TextBoxCollection
If Control.TabIndex = TextBoxIndex + 1 Then
I want to understand why is it this way in WPF or if I am wrong in something, please tell me?
I am trying to think in WPF way when I am working with WPF and not in Windows forms way.
Windows Live: Friends get your Flickr, Yelp, and Digg updates when they e-mail you.