Question Changing values within a usercontrol.

KKW

Member
Joined
Nov 21, 2009
Messages
10
Programming Experience
10+
Greetings All!

I'm building a user control that when certain buttons are clicked, it shows Form2. In my Module1 I have declared several variables as Public. In my UserControl1 code, these variables are set to a specific value. When Form2 is loaded and displayed, the Public variables appear to be available (no errors) but do not contain the value set previously. Also, if they are set in Form2, they appear to not be set when in the UserControl form..?

Anyone got a clue?

Thanks,
KKW
 
Can you post some code, and explain how form2 interacts with the usercontrol so we can get a better idea of your needs. Is form2 an MDIChild?
 
Thanks newguy!

I've trimmed this down conciderably.
I have tried several different approaches, and some of these attempts are still present, I really don't know the correct approach to take.

What's happening is this.
I set 2 breakpoints at the lines hilighted in blue in the UserControl Code at the btnAddress_Click.
The value of mtrAddress, mtrMeterType, and mtrDataSource are noted.
Continue to execute.
The values after IZONMeterForm2 command2_1 has been clicked (hilighted in red) show new values for mtrAddress, mtrMeterType, and mtrDataSource. After IZONMeterForm2 has closed, the same variables (Second blue hilighted line) should reflect the changes made in IZONMeterForm2.
They contain the original values as prior to the first blue hilight break.

Module Code:
VB.NET:
Option Strict Off
Option Explicit On
Module IZONMeterModule1

    Public mtrAddress As Integer
    Public mtrMeterType As String
    Public mtrDataSource As String

End Module

Usercontrol Code:
VB.NET:
Option Strict Off
Option Explicit On
<System.Runtime.InteropServices.ProgId("UserControl1_NET.UserControl1")> Public Class UserControl1
    
    Inherits System.Windows.Forms.UserControl

    Public Event Initialize()
    Public Event AddressChange()
    Public Event MeterTypeChange()
    Public Event MeterDataSourceChange()

    'Default Property Values:
    Const m_def_Address As Integer = 1
    Const m_def_MeterType As String = "CUB5"
    Const m_def_MeterDataSource As String = "RTE"

    'Property Variables:
    Dim m_Address As Integer
    Dim m_MeterType As String
    Dim m_MeterDataSource As String

    'Event Declarations:
    Event AddressChanged(ByVal sender As System.Object, ByVal ByVale As System.EventArgs)
    Event MeterTypeChanged(ByVal sender As System.Object, ByVal ByVale As System.EventArgs)
    Event MeterDataSourceChanged(ByVal sender As System.Object, ByVal ByVale As System.EventArgs)


    Private Sub btnAddress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddress.Click
        Dim Frm2 As IZONMeterForm2
        Frm2 = New IZONMeterForm2

        btnAddress.Enabled = False

        mtrAddress = MeterAddress
        mtrMeterType = MeterType
        mtrDataSource = MeterDataSource

        Frm2.TextBox1.Text = Str(MeterAddress)
        Frm2.TextBox2.Text = MeterType
        Frm2.TextBox3.Text = MeterDataSource
[COLOR="Blue"]        Frm2.ShowDialog()
        MeterAddress = mtrAddress[/COLOR]
        btnAddress.Enabled = True

        RaiseEvent AddressButtonClick(Me, Nothing)

        m_Address = mtrAddress
        m_MeterType = mtrMeterType
        m_MeterDataSource = mtrDataSource

        RaiseEvent AddressChanged(Me, Nothing)
    End Sub


    Public Property MeterAddress() As Integer
        Get
            MeterAddress = m_Address
            mtrAddress = m_Address
        End Get
        Set(ByVal Value As Integer)
            m_Address = Value
            mtrAddress = Value
            If Initialized = True Then
                RaiseEvent AddressChange()
            End If
        End Set
    End Property

    Public Property mtrAddress() As Integer
        Get
            mtrAddress = m_Address
        End Get
        Set(ByVal value As Integer)
            m_Address = value
        End Set
    End Property

    Public Property MeterType() As String
        Get
            MeterType = m_MeterType
            mtrMeterType = m_MeterType
        End Get
        Set(ByVal value As String)
            m_MeterType = value
            mtrMeterType = value
            If Initialized = True Then
                RaiseEvent MeterTypeChange()
            End If
        End Set
    End Property

    Public Property mtrMeterType() As String
        Get
            mtrMeterType = m_MeterType
        End Get
        Set(ByVal value As String)
            m_MeterType = value
        End Set
    End Property

    Public Property MeterDataSource() As String
        Get
            MeterDataSource = m_MeterDataSource
            mtrDataSource = m_MeterDataSource
        End Get
        Set(ByVal value As String)
            m_MeterDataSource = value
            mtrDataSource = value
            If Initialized = True Then
                RaiseEvent MeterDataSourceChange()
            End If
        End Set
    End Property

    Public Property mtrDataSource() As String
        Get
            mtrDataSource = m_MeterDataSource
        End Get
        Set(ByVal value As String)
            m_MeterDataSource = value
        End Set
    End Property
End Class

Form2 Code
VB.NET:
Friend Class IZONMeterForm2
    Inherits System.Windows.Forms.Form
    Dim TempValue As Integer
    Dim TempAddress As Integer
    Dim TempMeterType As String
    Dim TempDataSource As String

    Private Sub IZONMeterForm2_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

        Dim a As Integer

        If mtrAddress < 1 Then
            mtrAddress = 1
        End If

        TempAddress = mtrAddress
        TempMeterType = mtrMeterType
        TempDataSource = mtrDataSource

        Label3.Text = TempMeterType
        Label4.Text = mtrDataSource
        Label6.Text = Str(TempAddress)

        'TextBox1.Text = Str(TempAddress)
        'TextBox2.Text = TempMeterType
        'TextBox3.Text = TempDataSource

        ListBox1.Items.Clear()
        ListBox2.Items.Clear()

        For a = 0 To 6
            ListBox1.Items.Add(MeterTypeName(a))
        Next a
    End Sub


    Public Sub _Command2_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Command2_1.Click
        'OK
        Dim UC1 As IZONMeter.UserControl1
        UC1 = New IZONMeter.UserControl1

        UC1.MeterAddress = TempAddress
        UC1.MeterType = TempMeterType
        UC1.MeterDataSource = TempDataSource

[COLOR="Red"]        mtrAddress = TempAddress
        mtrMeterType = TempMeterType
        mtrDataSource = TempDataSource[/COLOR]

        Me.Close()
    End Sub
End Class
 
The general approach I use is:
VB.NET:
'Usercontrol
Public Event SomeEvent(ByVal something As String)

'click event
Raise Event SomeEvent(Textbox1.Text)

If you don't need to pass data then omit the ByVal part in the event and sub

VB.NET:
'form2
Dim uc As New UserControl1
Addhandler uc.SomeEvent, AddressOf SomeRoutedEvent

'the routed sub with matching signature
Private Sub SomeRoutedEvent(ByVal something As String)
Textbox1.Text = something
End Sub

The module could just be a class, just an object that holds data right?
 
newguy,

I think I'm going to take a slightly different approach. I'm going to take a closer look at my overall progression of changing values thru the property routines.

I am still a little confused about why I can't seem to effect a value change from within Form2 on a global variable located in Module1....

Thanks,
KKW
 
OK,

I have a clearer understanding of what's not happening.

When a button in a UserControl1 is pressed, it launches Form2. I want to populate a few textboxes on Form2 with properties from the UserControl1. After modification of the value of these properties is made in Form2, when Form2 closes it needs to update the property values of the UserControl1.

2 problems:
1. Populating the textboxes from within Form2.Load shows default property values from the newly created UC1, not the property values from UserControl1. (If I populate the textboxes from within the button_Click on the UserControl1, then the textbox(s) is correct.)

2. The modified data(properties) does not appear altered in the UserControl1 properties. This is accomplished by creating a reference to UserControl1 by use of the dim statement and new statement in Form2.Command2_1_Click.

I think the problem may be that I don't have a path into the UserControl1, but only into the newly created UC1.

Am I thinking this thru correctly?

KKW
 
Last edited:
So your form2 should be raising the events and the UC should be prescribing to them like I showed you in my last post. If the properties on the UC are public them you can load them directly when creating the form2:
VB.NET:
Dim f2 As New Form2
f2.someProperty = something

Late post - been on vacation - oh still am...
 
Changing values within a usercontrol.: RESOLVED

Hi!

Well I think I've got the issue dealt with.

I declare variables in a module as Public.
I set the values of these public variables in Form1 with selected properties.
Launch Form2, and populate Form2 items with values from the public variables.

I then make modifications to the public variables in Form2.
Then after Form2 is destroyed, the values in the public variables are used to modifiy the properties in Form1.

Seems to work just fine!

Thanks everyone for you input!
 
Back
Top