none
How to pass a variable up through Dialog Boxes?? RRS feed

  • Question

  • I have a VB wondows form which opens a Dialogue Box (call it dialogue1) to gather some more information.  Within this dialogue box is a button which opens another Dialogue box (dialogue2) which allows the user to define more detail on a particular field.  On dialogue2 is yet another button which allows even more detail to be specified, should the user want to, in dialogue3.

    Yes I know thats a lot of dialogues but it is really useful for what I want to do, to add detail where required.  I know how to get data from say dialogue3 to 2 (extracting from textboxes etc) no problem.  However, I want to get data from dialogue3 to dialogue1.

    I am currently reading data from dialogue3 to dialogue2 into a variable.  However, when I tried to get data from a variable in dialogue2 to dialogue1, I couldnt work it out.  Is there a way of returning a variable from a dialogue box (once OK is selected) to where is was called from??

    Friday, April 20, 2018 6:13 PM

Answers

  • You could also subcribe to an event from Dialog to Dialog as this does from Dialog1 to Form1.

    In this instance Form1 subscribes to an event in Dialog1 which when raised in Dialog1 passes the TextBox's string to any sub handling the event.

    ' Form1's code
    
    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            AddHandler Dialog1.TBoxTextChanged, AddressOf Dialog1s_TextChanged
        End Sub
    
        Private Sub Dialog1s_TextChanged(ByRef s As String)
            TextBox1.Text = s
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dialog1.ShowDialog()
        End Sub
    End Class
    
    
    
    ' Dialog1's code
    
    
    Imports System.Windows.Forms
    
    Public Class Dialog1
    
        Public Event TBoxTextChanged(ByRef s As String)
    
        Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        End Sub
    
        Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Me.Close()
        End Sub
    
        Private Sub Dialog1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            RaiseEvent TBoxTextChanged(TextBox1.Text)
        End Sub
    End Class
    


    La vida loca

    • Marked as answer by Mister S Sunday, April 22, 2018 7:42 AM
    Saturday, April 21, 2018 3:31 AM

All replies

  • Hi

    Dialog box is a term usually used for system Dialogs such as ColorDialog etc.

    For your 'Dialog' boxes, are these separate Forms within your project?


    Regards Les, Livingston, Scotland

    Friday, April 20, 2018 6:50 PM
  • Does it need to be a variable? This passes text from the TextBox's of Dialogs 2 and 3 to Dialog1 and Form1.

    So Form1's button launches Dialog1 and typing in any TextBox of Dialog1 will cause the corresponding TextBox on Form1 to receive the text.

    Launching Dialog2 will cause TextBox2 on Dialog1 and Form1 to receive Dialog2's text. Because Dialog1 receives it and passes it on to Form1.

    Launching Dialog3 will cause TextBox3 on Dialog1 and Form1 to receive Dialog3's text. Because Dialog1 receives it and passes it on to Form1.

    Anyhow variables need to be public and I believe shared for use between classes.

    ' Form1's code
    
    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
        End Sub
    
        Public Shared Sub D1sTxtChanged(ByVal s As String)
            Form1.TextBox1.Text = s
        End Sub
    
        Public Shared Sub D2sTxtChanged(ByVal s As String)
            Form1.TextBox2.Text = s
        End Sub
    
        Public Shared Sub D3sTxtChanged(ByVal s As String)
            Form1.TextBox3.Text = s
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dialog1.ShowDialog()
        End Sub
    End Class
    
    
    ' Dialog1's code
    
    Imports System.Windows.Forms
    
    Public Class Dialog1
    
        Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        End Sub
    
        Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Me.Close()
        End Sub
    
        Private Sub Dialog1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            Form1.D1sTxtChanged(TextBox1.Text)
        End Sub
    
        Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
            Form1.D2sTxtChanged(TextBox2.Text)
        End Sub
    
        Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
            Form1.D3sTxtChanged(TextBox3.Text)
        End Sub
    
        Public Shared Sub Dialog2sTxtChanged(ByVal s As String)
            Dialog1.TextBox2.Text = s
        End Sub
    
        Public Shared Sub Dialog3sTxtChanged(ByVal s As String)
            Dialog1.TextBox3.Text = s
        End Sub
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dialog2.ShowDialog()
        End Sub
    End Class
    
    
    ' Dialog2's code
    
    
    Imports System.Windows.Forms
    
    Public Class Dialog2
    
        Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        End Sub
    
        Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Me.Close()
        End Sub
    
        Private Sub Dialog2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
            Dialog1.Dialog2sTxtChanged(TextBox2.Text)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dialog3.ShowDialog()
        End Sub
    End Class
    
    
    ' Dialog3's code
    
    Imports System.Windows.Forms
    
    Public Class Dialog3
    
        Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        End Sub
    
        Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Me.Close()
        End Sub
    
        Private Sub Dialog3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
            Dialog1.Dialog3sTxtChanged(TextBox3.Text)
        End Sub
    End Class


    La vida loca

    Saturday, April 21, 2018 2:52 AM
  • You could also subcribe to an event from Dialog to Dialog as this does from Dialog1 to Form1.

    In this instance Form1 subscribes to an event in Dialog1 which when raised in Dialog1 passes the TextBox's string to any sub handling the event.

    ' Form1's code
    
    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            AddHandler Dialog1.TBoxTextChanged, AddressOf Dialog1s_TextChanged
        End Sub
    
        Private Sub Dialog1s_TextChanged(ByRef s As String)
            TextBox1.Text = s
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dialog1.ShowDialog()
        End Sub
    End Class
    
    
    
    ' Dialog1's code
    
    
    Imports System.Windows.Forms
    
    Public Class Dialog1
    
        Public Event TBoxTextChanged(ByRef s As String)
    
        Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        End Sub
    
        Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
            Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Me.Close()
        End Sub
    
        Private Sub Dialog1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            RaiseEvent TBoxTextChanged(TextBox1.Text)
        End Sub
    End Class
    


    La vida loca

    • Marked as answer by Mister S Sunday, April 22, 2018 7:42 AM
    Saturday, April 21, 2018 3:31 AM
  • Thanks for all the proposed solutions.  Id rather not add it into a textbox in Dialogue2 so I think the most recent post is the answer Im looking for!
    Sunday, April 22, 2018 7:42 AM
  • Thanks for all the proposed solutions.  Id rather not add it into a textbox in Dialogue2 so I think the most recent post is the answer Im looking for!

    Tks. Yes you could just pass it to a variable along the way rather than setting a TextBox's text.

    La vida loca

    Sunday, April 22, 2018 3:37 PM