Getting Started with Adobe After Effects - Part 6: Motion Blur
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.

1st Prize - Apple iPad

DOTNET Quiz 2011 - Populate WPF TextBoxes with data

  • I have a WPF Window with 7 textboxes. Following is my code for it


    <Window xmlns="" 
            <WrapPanel Orientation="Vertical">
                <TextBox Name="TextBlock1" Width="100"/>
                <TextBox Name="TextBlock2" Width="100"/>
                <TextBox Name="TextBlock3" Width="100"/>
                <TextBox Name="TextBlock4" Width="100"/>
                <TextBox Name="TextBlock5" Width="100"/>
                <TextBox Name="TextBlock6" Width="100"/>
                <TextBox Name="TextBlock7" Width="100"/>


    Class MainWindow 
        Private Sub MainWindow_Loaded(ByVal sender As Object,
                ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
            TextBlock3.Text = "Window"
            TextBlock4.Text = "Presentation"
            TextBlock1.Text = "Foundation"
            TextBlock2.Text = "Question"
            If TextBlock1.Text = TextBlock3.Text _
                  AndAlso TextBlock2.Text = TextBlock4.Text Then
               TextBlock5.Text = "Asked"
                If TextBlock2.Text = TextBlock5.Text Then
                    TextBlock6.Text = "By"
                    If TextBlock6.Text = TextBlock1.Text Then
                        TextBlock7.Text = "Gaurav"
                    End If
                End If
            End If
        End Sub
    End Class

    Write down the missing code/XAML so that

    1. All the TextBoxes contains some Text.
    2. All the conditions in the MainWindow_Loaded should get fulfilled, which means TextBlock7 should contain Text "Gaurav"
    3. You cannot change code in above MainWindow_Loaded method.
    Posted on 01-17-2011 00:00 |
    Gaurav Khanna
    277 · 0% · 159

5  Answers  

Subscribe to Notifications
  • Score

    It took me a while to understand the question, and then another while to transform the coe from VB to C# (as I am a C# addict) and then the solution back to VB, but I think the answer lies in some TextChanged event handlers. The code below fills all boxes.

        Private Sub Text1_Changed(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles TextBlock1.TextChanged
            TextBlock3.Text = TextBlock1.Text
        End Sub
        Private Sub Text2_Changed(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles TextBlock2.TextChanged
            TextBlock4.Text = TextBlock2.Text
        End Sub
        Private Sub Text5_Changed(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles TextBlock5.TextChanged
            TextBlock2.Text = TextBlock5.Text
        End Sub
        Private Sub Text6_Changed(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles TextBlock6.TextChanged
            TextBlock1.Text = TextBlock6.Text
        End Sub
    Replied on Jan 23 2011 12:28PM  . 
    28 · 6% · 1888
  • Score

    alt text

    Replied on Jan 31 2011 3:06AM  . 
    2436 · 0% · 5
  • Score

    The Attribute Loaded=MainWindow_Loaded is missing. Anyway, I'd rather use MVVM to fill control's values.


    Replied on Jan 31 2011 10:03AM  . 
    1973 · 0% · 10
  • Score

    Gunter got it correct

    Alternatively I guess we can also use databinding . The following snippet works for me

            <WrapPanel Orientation="Vertical">
                <TextBox Name="TextBlock1" Width="100" Text="{Binding ElementName=TextBlock6, Path=Text}" />
                <TextBox Name="TextBlock2" Width="100" Text="{Binding ElementName=TextBlock5, Path=Text}" />
                <TextBox Name="TextBlock3" Width="100"  Text="{Binding ElementName=TextBlock1, Path=Text}"/>
                <TextBox Name="TextBlock4" Width="100" Text="{Binding ElementName=TextBlock2, Path=Text}" />
                <TextBox Name="TextBlock5" Width="100" />
                <TextBox Name="TextBlock6" Width="100" />
                <TextBox Name="TextBlock7" Width="100"/>
    Replied on Feb 14 2011 6:57AM  . 
    135 · 1% · 376
  • Score

    Thank you for this mind twisting question Gaurav. Unfortunately it looks like I am a little bit too late, so the easiest were already suggested. Please see my solution below. As you can see it doesn't utilise any of VB .NET features used in the previous answers. It's not the easiest, not the most straightforward way and not the best design, however it will do the job and completely satisfy the requirements of the question:


    <src:MyTextBox x:Name="TextBlock1" Width="100"/>
    <src:MyTextBox x:Name="TextBlock2" Width="100"/>
    <src:MyTextBox x:Name="TextBlock3" Width="100"/>
    <src:MyTextBox x:Name="TextBlock4" Width="100"/>
    <src:MyTextBox x:Name="TextBlock5" Width="100"/>
    <src:MyTextBox x:Name="TextBlock6" Width="100"/>
    <src:MyTextBox x:Name="TextBlock7" Width="100"/>

    You also need to add attribute xmlns:src="clr-namespace:[Project Name]" to your Window tag


    Public Class MyTextBox 
    Inherits TextBox Public 
    dependentTextBox As MyTextBox 
    Public Overloads Property Text As String 
    Return MyBase.Text 
    End Get 
    Set(ByVal value As String) 
    MyBase.Text = value 
    If Not IsNothing(dependentTextBox) Then 
    dependentTextBox.Text = value 
    End If 
    End Set 
    End Property 
    End Class 
    Class MainWindow 
    Public Sub New() 
    TextBlock6.dependentTextBox = TextBlock1 
    TextBlock5.dependentTextBox = TextBlock2 
    TextBlock1.dependentTextBox = TextBlock3 
    TextBlock2.dependentTextBox = TextBlock4 
    End Sub
    ... then the rest of MainWindow class provided in the question
    Replied on Mar 17 2011 10:20PM  . 
    Dmitry Kharlap (aka Docker)
    156 · 1% · 325

Your Answer

Sign Up or Login to post an answer.
Please note that your answer will not be considered as part of the contest because the competition is over. You can still post an answer to share your knowledge with the community.

Copyright © Rivera Informatic Private Ltd.