Link to home
Create AccountLog in
Avatar of cyimxtck
cyimxtckFlag for United States of America

asked on

WPF User Control not picking up variable values

I have a WPF appliacation in 4.0 framework.  The app has a user control in mainwindow.  The control contains a text box that I am trying to populate with error messages.  The buttons on the control instantiate routines in the mainwindow.  So should an error pop-up after clicking a button from the control - I want the control to have the error.

The error message is sent over to the control, but never sets the textbox text after returning back to the main window?
mainwindow.vb

    Public Shared Sub dosomething()

        Dim x As New CG.GUI.Controls.LookupControl.LookupControlHeader

        x.m_MessageText = "Did this work?"
        x.m_ShowMessages = True

        x.MessageBoxText(x.m_MessageText,
                         x.m_ShowMessages,
                         x.m_ShowMessages)


    End Sub


Control:

#Region "Variables"

        Public m_MessageText As String = String.Empty
        Public m_ShowMessages As Boolean = False
        Public m_ShowVoids As Boolean = True

#End Region

        Public Sub btnVoid_Click(ByVal sender As System.Object,
                                 ByVal e As System.Windows.RoutedEventArgs) Handles btnVoid.Click

            dosomething()

        End Sub

        Public Sub MessageBoxText(ByVal str As String,
                                  ByRef show As Boolean,
                                  Optional ByVal Type As Boolean = False)

            tbxMessages.Text = str
            tbxMessages.Visibility = CType(show, Windows.Visibility)

            If Type = True Then

                tbxMessages.Foreground = Brushes.Red

            End If

        End Sub

Open in new window

Avatar of wdfdo1986
wdfdo1986
Flag of Sri Lanka image

can u post some screen shots of the output and a description on what u want to change
Avatar of cyimxtck

ASKER

The screen shot would not be revealing since it has an empty text box.  All I want is the text box populated and it is currently not populated.

Just a button in a control and a textbox in that control.

Thanks,

B
The code above should place the text "Did this work?" in the text box but does not.
Try to do that MessageBoxText in the initialization
In other words move that part of code to the Constructor and see what happens.
Public m_MessageText As String = "Harry met sally."

Still no dice....
can u give me the wpf code of the user control
This isn't spot on with the imports since I have been messing with everything.

Thanks,

B
Option Explicit On
Option Strict On

Imports System.ComponentModel
Imports System.Data
Imports System.Windows
Imports System.Windows.Input
Imports System.Collections.Generic
Imports System.Windows.WindowCollection
Imports CG.GUI.MainWindow
Imports CG.GUI.Controls.LookupControl
Imports System
Imports System.Text
Imports System.Windows.Controls
Imports System.Windows.Data

Namespace CG.GUI.Controls.LookupControl

    Partial Public Class LookupControlHeader
        Inherits System.Windows.Controls.UserControl

#Region "Variables"

        Public m_MessageText As String = "Harry met sally."
        Public m_ShowMessages As Boolean
        Public m_ShowVoids As Boolean

#End Region

#Region "Constructors"



#End Region

#Region "Properties"

        Public Property MessageText As String
            Get
                Return tbxMessages.Text
            End Get
            Set(ByVal value As String)
                tbxMessages.Text = value
            End Set
        End Property

        Public Property ShowVoids As Boolean
            Get
                Return cbxShowVoids.IsChecked.Value
            End Get
            Set(ByVal value As Boolean)
                cbxShowVoids.IsChecked = value
            End Set
        End Property

#End Region

#Region "Methods"

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub btnEdit_Click(ByVal sender As System.Object,
                                  ByVal e As System.Windows.RoutedEventArgs) Handles btnEdit.Click

            MsgBox("Editing has not been implemented yet, sorry.")

        End Sub

        Public Sub btnVoid_Click(ByVal sender As System.Object,
                                 ByVal e As System.Windows.RoutedEventArgs) Handles btnVoid.Click

            dosomething()

        End Sub

        Private Sub btnUnvoid_Click(ByVal sender As System.Object,
                                    ByVal e As System.Windows.RoutedEventArgs) Handles btnUnvoid.Click



        End Sub

        Private Sub btnCreate_Click(ByVal sender As System.Object,
                                    ByVal e As System.Windows.RoutedEventArgs) Handles btnCreate.Click



        End Sub

        Private Sub cbxShowVoids_Checked(ByVal sender As System.Object,
                                         ByVal e As System.Windows.RoutedEventArgs) Handles cbxShowVoids.Checked

            ShowUnvoid()

        End Sub

        Private Sub cbxShowVoids_Unchecked(ByVal sender As System.Object,
                                           ByVal e As System.Windows.RoutedEventArgs) Handles cbxShowVoids.Unchecked

            HideUnvoid()

        End Sub

        Private Sub HideUnvoid()

            m_ShowMessages = True

            If ShowVoids = True Then

                btnUnvoid.Visibility = CType(m_ShowMessages, Windows.Visibility)

            End If

        End Sub

        Private Sub ShowUnvoid()

            m_ShowMessages = True

            btnUnvoid.Visibility = CType(m_ShowMessages, Windows.Visibility)

        End Sub

        Public Sub MessageBoxText(ByVal str As String,
                                  ByRef show As Boolean,
                                  Optional ByVal Type As Boolean = False)

            tbxMessages.Text = str
            tbxMessages.Visibility = CType(show, Windows.Visibility)

            If Type = True Then

                tbxMessages.Foreground = Brushes.Red

            End If

        End Sub

#End Region '"Methods"

    End Class

End Namespace

Open in new window

u said that u are using wpf
then i need the xaml code
oh ok....here you are.

Thanks

<UserControl x:Class="CG.GUI.Controls.LookupControl.LookupControlHeader"
             x:Name="PanelHeader"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             Height="auto"
             Width="auto"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
    <Grid Width="auto">
        <Button Name="btnEdit"
                Content="Edit"
                Width="70"
                Height="20"
                Margin="2,1,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top" />
        <Button Name="btnVoid"
                Content="Void"
                Width="70"
                Height="20"
                Margin="78,1,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top" />
        <Button Name="btnUnvoid"
                Content="Unvoid"
                Width="70"
                Height="20"
                Margin="154,1,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top" />
        <Button Name="btnCreate"
                Content="Create"
                Width="70"
                Height="20"
                Margin="230,1,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top" />
        <CheckBox Content="Show Voids"
                  Height="16"
                  HorizontalAlignment="Left"
                  Margin="315,4,0,0"
                  Name="cbxShowVoids"
                  VerticalAlignment="Top" />
        <TextBox Height="auto"
                 HorizontalAlignment="Left"
                 Margin="400,1,0,0"
                 Name="tbxMessages"
                 VerticalAlignment="Top"
                 Width="400" />
    </Grid>
</UserControl>

Do u know about debugging
If so
put a break point on the line containing
tbxMessages.Text = str
then check whats the value of str
and then goto next line by hitting F10
then check the attributes of tbxMessages.
try removing this line also
tbxMessages.Visibility = CType(show, Windows.Visibility)
try changing this one first
<TextBox Height="auto"
                 HorizontalAlignment="Left"
                 Margin="400,1,0,0"
                 Name="tbxMessages"
                 VerticalAlignment="Top"
                 Width="400" />
to
<TextBox Height="auto"
                 HorizontalAlignment="Left"
                 Margin="0,1,0,0"
                 Name="tbxMessages"
                 VerticalAlignment="Top"
                 Width="400" />
changing the margin to 0 from 400 makes the whole control a text box
Str = "Harrry met sally" in the debugger as well
Its like when it picks it up in the main window it has the value but in the control it never sees the value?  Pretty weird....
normally when we use usercontrols we integrate it to a window.
I am not sure whats happening here
did u tried with the settlement of margin. cause it can be a problem
did u tried with the settlement of margin.

What do you mean?
Ok i didn't see that u gave a width there sorry about that
use a window instead of a user control
There has to be able to do this as a user control....a window really doesn't buy you much from what I know.  The same issues will still be faced.....this is a text box simple thing that should be a  3 second fix but....hahahahaha

Oh well I will keep trying here.

Thanks for your input,

B
ok
in debug u said that str value is there
after the next line did u check the attributes of tbxMessages
specially check the text attribute
from the window:

? tbxmessages.IsEnabled
True
? tbxmessages.Text
"Harry met sally."

From the control once it comes back to the code in the control (the code in the window is what is getting executed) it is "" 
let me check something and I'll get back to u quickly
For what purpose u use this line
tbxMessages.Visibility = CType(show, Windows.Visibility)
try removing it see what happens or set visibility to just Visible and see what happens
I have removed that and still no good....
"From the control once it comes back to the code in the control (the code in the window is what is getting executed) it is """
What does it mean?
ASKER CERTIFIED SOLUTION
Avatar of bansidhar
bansidhar
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account