.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Accessing a control from a code module
 

Accessing a control from a code module

I have a Textbox in Window1.xaml and want to change the value of the text in a code module called LoadData.vb which has a public sub where the sub name doesn't matter.

When I type in Window1. I seem to have nothing to reference the windows controls, how do I access controls such as the textbox?

Thanks...Kevin
KSG
Kevininstructor

You have to pass the Window as argument to your method.

VB:

Module LoadData

    Sub TryingToChangeTextInTextBox(ByRef window1 As Window1)
        window1._textBox.Text = "Changed"
    End Sub
End Module
But I would not do that, its a bad design. Try touse the MVVM pattern and bind the textbox to a property of the ViewModel.
Guenter Schwaiger
Add "Name" to the textbox...

<TextBox Name="myTextBox" Text="Some Text"/>


then inside code behind...

myTextBox.Text = "Changed text"


I advice you to look some introductory WPF articles so that you do things the right way instead of the winforms way.
Software Engineer 1, My Blog
KrishnaBhargava
Hi Kevininstructor,

you can give the TextBox a name.

<TextBox x:Name="_textBox" Text="Hello"/>
WPF creates a variable with the name _textBox for you.

You can access it in Window1 with Me._textBox = "My new Content".

Hope this helps.


Guenter Schwaiger
Add "Name" to the textbox...

<TextBox Name="myTextBox" Text="Some Text"/>


then inside code behind...

myTextBox.Text = "Changed text"


I advice you to look some introductory WPF articles so that you do things the right way instead of the winforms way.
Software Engineer 1, My Blog

Thanks for your reply.

To be more specific, here is the TextBox in the XAML

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" 
    Height="452" 
    Width="502" 
    xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" 
    Name="Window1">
    <Grid>
        <TextBox Height="18" 
                 Name="TextBox1" 
                 Width="100" Text="Hello" />
    </Grid>
</Window>

Here is what I was thinking would work
Module LoadData

    Sub TryingToChangeTextInTextBox()
        window1.TextBox1.Text = "Changed"
    End Sub
End Module
When I try Window1 followed by the dot operator I get nothing.
KSG
Kevininstructor
Hi Kevininstructor,

you can give the TextBox a name.

<TextBox x:Name="_textBox" Text="Hello"/>

WPF creates a variable with the name _textBox for you.

You can access it in Window1 with Me._textBox = "My new Content".

Hope this helps.



Thanks for your reply!
I did provide a name for the textbox as shown below. but in my code module I get nothing for accessing the TextBox. If I try this directly in the window or a partial class of the window I then get access to the textbox but want to access the textbox in the speparate code module.

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" 
    Height="452" 
    Width="502" 
    xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" 
    Name="Window1">
    <Grid>
        <TextBox Height="18" 
                 Name="TextBox1" 
                 Width="100" Text="Hello" />
    </Grid>
</Window>


KSG
Kevininstructor

You have to pass the Window as argument to your method.

VB:

Module LoadData

    Sub TryingToChangeTextInTextBox(ByRef window1 As Window1)
        window1._textBox.Text = "Changed"
    End Sub
End Module
But I would not do that, its a bad design. Try touse the MVVM pattern and bind the textbox to a property of the ViewModel.
Guenter Schwaiger

You have to pass the Window as argument to your method.

VB:

Module LoadData



    Sub TryingToChangeTextInTextBox(Window1 window1)

        window1._textBox.Text = "Changed"

    End Sub

End Module
But I would not do that, its a bad design. Try touse the MVVM pattern and bind the textbox to a property of the ViewModel.

I agree, not good to pass the window as an argument. I will have to study up on ViewModel. Didn't think it would be this much effort thou.
Thanks!
KSG
Kevininstructor

You can use google to search for other answers

Custom Search

More Threads

• ListView-DetailView model?
• Why the lack of Window Inheritance ???
• Strange behavior with routed event data.
• Retrieve next sibling in Xaml tree
• Using Same Xaml file for both Windows and Web application.
• WPF Style problem
• Tabcontrol and data validation
• VS 2005 Extensions for .NET 3.0 (WPF)
• WPF TabControl Left Side Bottom and Vertical alignment
• Using a GridView in an ItemsControl