PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] How to access ChildWindow TextBox from MainWindow in WPF-VBForums
Results 1 to 4 of 4

Thread: [RESOLVED] How to access ChildWindow TextBox from MainWindow in WPF

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2016
    Posts
    78

    Resolved [RESOLVED] How to access ChildWindow TextBox from MainWindow in WPF

    I want to access ChildWindow TextBox from MainWindow.


    MainWindow xaml codes are here;
    Code:
    <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="350" Width="525">
        <Grid>
            <Button x:Name="Button1" Height="20" Width="100" Content="Click Me"/>
        </Grid>
    </Window>

    MainWindow vb.net codes are here;
    Code:
    Class MainWindow 
        Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
            TextBox1.Text = "Hello"
            Dim myChildWindow As New ChildWindow()
            myChildWindow.Owner = Me
            myChildWindow.ShowDialog()
        End Sub
    End Class


    ChildWindow xaml codes are here;
    Code:
    <Window x:Class="ChildWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ChildWindow" Height="300" Width="300">
        <Grid>
            <TextBox x:Name="TextBox1" Height="20" Width="100" Text=""/>
        </Grid>
    </Window>

    There is a C# solution here: https://stackoverflow.com/a/2219218/10690106

    I need vb.net solution.
    Last edited by Kram Kramer; Nov 24th, 2018 at 06:15 PM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,349

    Re: How to access ChildWindow TextBox from MainWindow in WPF

    This C# snippet:
    Code:
    var window2 = Application.Current.Windows
        .Cast<Window>()
        .FirstOrDefault(window => window is Window2) as Window2;
    ...is equivalent to:
    Code:
    Dim window2 = TryCast(Application.Current.Windows
                          .Cast(Of Window)()
                          .FirstOrDefault(Function(window) TypeOf window Is ChildWindow)
                         , ChildWindow)
    ...and an alternative:
    Code:
    Dim window2 = TryCast(Application.Current.Windows.OfType(Of MainWindow).First
                         , ChildWindow)
    However, whether this is the right way to go about what you are doing is debatable, especially as this code doesn't link to a particular instance... but as you haven't given any indication of what you are actually trying to do (only the method you have chosen to use for it), we can't give the best advice.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,631

    Re: How to access ChildWindow TextBox from MainWindow in WPF

    Just a small note: this would not compile:
    vb.net Code:
    1. Dim window2 = TryCast(Application.Current.Windows
    2.                       .Cast(Of Window)()
    3.                       .FirstOrDefault(Function(window) TypeOf window Is ChildWindow)
    4.                      , ChildWindow)
    VB has different line-continuation rules to C# so you need to put the dot on the preceding line:
    vb.net Code:
    1. Dim window2 = TryCast(Application.Current.Windows.
    2.                                           Cast(Of Window)().
    3.                                           FirstOrDefault(Function(window) TypeOf window Is ChildWindow),
    4.                       ChildWindow)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,631

    Re: How to access ChildWindow TextBox from MainWindow in WPF

    I have to agree with si's implication here though. This is just bad. Basically, a form should not be accessing controls on another form directly anyway, but you should also be keeping a reference to a form instance when you create it if you need to access it later. Given that you're calling ShowDialog on this child form, the only time you should be needing to get anything from it is immediately after ShowDialog returns. You should be disposing it immediately after that, never to be used again. In order to access that the data in the control, you should be exposing it via a property in the child window itself. For instance, your child window should have code something like this:
    vb.net Code:
    1. Public ReadOnly Property TextBoxText As String
    2.     Get
    3.         Return TextBox1.Text
    4.     End Get
    5. End Property
    and then your main window should be doing something like this:
    vb.net Code:
    1. Using myChildWindow As New ChildWindow()
    2.     myChildWindow.ShowDialog()
    3.  
    4.     Dim textBoxText = myChildWindow.TextBoxText
    5.  
    6.     'Use textBoxText here.
    7. End Using
    Last edited by jmcilhinney; Nov 24th, 2018 at 08:33 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width