locked
Can’t pass values between forms! RRS feed

  • Question


  • I’m using vb.net, framework 3.5, with the Windows Mobile 6 Profesional SDK platform, to write an app for an ecom i.roc Ci70 hand held scanner unit.

    All I want to do is pass values held in lable.text fields to another form. Normally it would be as simple as saying form1.label1.text = form2.label1.text but this doesn’t work.

    I did get values imported into one form from another by using the Load event handler

    e.g.  

    Public Sub ShowDisgorging()
            ofrmScan_TryDispose()
            AddHandler ofrmDisgorg.Load, AddressOf SetD
            ofrmDisgorg.Show()
            ofrmDisgorg.BringToFront()
        End Sub
        Private Sub SetD(ByVal sender As Object, ByVal e As System.EventArgs)
            ofrmDisgorg.Label_UID.Text = UserInfo.USERID
        End Sub


    But I can’t get the same thing to work in reverse using the focus event.

    Private Sub btn_EXIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_EXIT.Click
            'Me.Close()
            If _lineChange Then
                AddHandler ofrmDisgorg.GotFocus, AddressOf Set1
            Else
                AddHandler ofrmDisgorg.GotFocus, AddressOf Set2
            End If
            ofrmDisgorg.Focus()
            Me.Hide()
        End Sub
        Private Sub Set1(ByVal sender As Object, ByVal e As System.EventArgs)
            ofrmDisgorg.label_CasksLeft.Text = _returnCasksLeft
        End Sub
        Private Sub Set2(ByVal sender As Object, ByVal e As System.EventArgs)
            ofrmDisgorg.label_CasksLeft.Text = Me.label_CasksLeft.Text
        End Sub


    Compared to ‘normal’ windows this all seems a bit complicated to pass text between forms! So what am I doing wrong?

    Friday, March 14, 2014 4:00 PM

Answers

  • If you don't see these updates then there are couple possible reasons:

    1. You're setting values on a difference instance. That is, one instance gets the updated values and another instance is shown. You can put a break point into, say, InitializeComponent() on a form to see if you're creating multiple instances.

    2. Something else if changing these values after you set them, e.g. data binding, some initialization called in a wrong place (e.g. from OnFocus), etc. You can add an event which fires on value change and put a break point to see if something is destroying your values.

    If you find and eliminate these values should be set just fine without unneded events.

    By the way, for each event you hook there must be an event unhooked.



    This posting is provided "AS IS" with no warranties, and confers no rights.

    • Marked as answer by Eileen ni Monday, March 31, 2014 8:46 AM
    Tuesday, March 18, 2014 4:10 PM

All replies

  • Try this:

    Private Sub btn_EXIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_EXIT.Click
            If _lineChange Then
                ofrmDisgorg.label_CasksLeft.Text = _returnCasksLeft
            Else
                ofrmDisgorg.label_CasksLeft.Text = Me.label_CasksLeft.Text
            End If
            ofrmDisgorg.Focus()
            Me.Hide()
        End Sub


    This posting is provided "AS IS" with no warranties, and confers no rights.

    Friday, March 14, 2014 11:16 PM
  • Hello Ilya,

    That's pretty much what I started out with, but it doesn't update the ofrmDisgorg form, which I thought it should.

    ofrmDisgorg  is defined as:      Public ofrmDisgorg As New frmDisgorge()

    The current form is called from frmDisgorge by the following code:

        Private Sub btn_ManualEntry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ManualEntry.Click
            'Set Focus back to the text box
            Me.tbx_Scan.Focus()
    
            'Displays the Manual Entry screen for the currently selected Allocation
            Dim oManager As New clsManager
            oManager.ofrmDisgorgManual.Manager = oManager
            oManager.ShowManualDisgorging(Me.label_LastCask.Text, Me.Label_UID.Text, _currentLines, Me.label_CurrentAllocation.Text, Me.label_CasksLeft.Text, Me.cbx_LineNo.SelectedValue)
            
        End Sub

    Which calls the process that loads the values into the form.

    Using ofrmDisgorg.Show() works, but this opens a new instance of the form, where I want to update the existing form, which called the current form.

    I've also tried using frmDisgorge directly, it doesn't complain, but doesn't work ether.  

    Tuesday, March 18, 2014 12:55 PM
  • If you don't see these updates then there are couple possible reasons:

    1. You're setting values on a difference instance. That is, one instance gets the updated values and another instance is shown. You can put a break point into, say, InitializeComponent() on a form to see if you're creating multiple instances.

    2. Something else if changing these values after you set them, e.g. data binding, some initialization called in a wrong place (e.g. from OnFocus), etc. You can add an event which fires on value change and put a break point to see if something is destroying your values.

    If you find and eliminate these values should be set just fine without unneded events.

    By the way, for each event you hook there must be an event unhooked.



    This posting is provided "AS IS" with no warranties, and confers no rights.

    • Marked as answer by Eileen ni Monday, March 31, 2014 8:46 AM
    Tuesday, March 18, 2014 4:10 PM
  • I had a time limit to get this to work so I ended up using a module, but that always seems wrong some how.

    The values do seen to be being passed across, but not kept when the sending form is hidden!?

    I've still no idea why it wasn't updating, but I will try and spend some time on it soon, as my module solution works but clearly isn't the best way to do this.

    Thanks for all the help...

    Tuesday, March 18, 2014 5:01 PM