Moving data between different forms/windows.

Kailen

Member
Joined
Jun 28, 2006
Messages
5
Programming Experience
Beginner
Alright, this is probably going to be a complete newbie question, but I need to figure this out. I have a program that requires some data entry, the data entry is more complicated than a standard dialog box will allow. I've got a program working that will accept the input data, and spit out the output data. The problem is, I need to work that program, form and all, into another program.

Basically, I need the main program to bring up the other program, as well as send it a variable that will accept the new data. The other program needs to be able to see the data being sent, complete it's calculations, and put the new data into the variable. It also needs to pause the main program's window, so that the main program doesn't keep going before the data is added. And, of course that means the new window needs to get the focus as soon as it appears.

*By "program" I'm refering to forms. I've already added both forms to one project, both "programs" are merged together. I just need to know how to get them to work properly with each other.

This is all being done in Visual Studio 2003. I'd lke to see the examples in code, if possible, as I learn best looking at source code and not trying to read tech docs. (Code doesn't have to be exact, it just needs to do what I need, and I can modify it to fit.)
 
Thanks for the great link. However, it is not working for me. I've been banging my head against this for a few more weeks now, and so far nothing is working right.

You see, I have a program that nees to pop up a series of dialog boxes when it initializes. However, it should only pop up one at a time, and then wait for that dialog box to finish before continuing with the next one.

The problem is that the main program simply refuses to stop working when a dialog box is up. I've even tried setting up an infinite loop inside the main program. On the times it didn't flat out ignore the loop, it just preformed an infinite loop that wouldn't even let me access the other window.

At this point, I can pass data from the windows, and open one when it's called, but now I need to tell the first window to STOP what it's doing. I've even tried breaking up the initialization so that preforming the second window's actions moves onto the next step, but that didn't work either.

I'm not going to post the entire program here, but I can give you a rundown of what I need it to do, in pseudocode.

VB.NET:
Form 1:
  Initalize:
    Loop:
      For each string, open Form 2
      Form 2:
        Get user data, calculate, send back series of information
      Form 1 receives information
    Next loop
  Finish calculations for initalize
Ready to run main program
It's not waiting to receive the information, and the second window simply will not go away. I've set visible = false, I've run hide(), I've tried to deacivate it, the best I can do is set enabled = false for the whole form. Form 1 needs to stop running and collect the data before continuing to the next loop. Please help, why is this not working for me?
 
Not sure if this is the best solution, but it works for me.

You can create a global variable (declare it in a module). In form1 you can write the values to the variables. In form2 you can add some logic and read those variable to calculate a function, or to just display on the screen.

Hope this helps.
 
The problem is, it continues through without waiting for the data to calculate. I can't set it up so that all of the calculations are done in the second form. And I can't have it loading staticly, becuase the user needs to input the data for the forms.
 
You need to show the form2 as a dialog, that way form1 code will not continue intil form2 is finished. Use the ShowDialog method to show the form instead of the Show method.
 
Before I start fighting the code yet again, I need to confirm a few things about this method.

Does ShowDialog() allow you to use custom forms?
Do I need to Return the information?
How does it tell when the dialog box is closed, with custom forms?

If this just pops up a normal Dialog box, it won't work. There is a lot of data to be added, more than just a single string.
 
You can call ShowDialog on ANY form. You create a form, pass any data to it that it requires either via the constructor or by setting public properties, then call ShowDialog. The ShowDialog method blocks the caller until the dialogue is dismissed. You dismiss the dialogue form by setting its DialogResult property. The value you set is then returned by ShowDialog to indicate what happened, the most common values being OK and Cancel. Usually your dialogue form will have at least an OK and a Cancel button to return those values. Once the dialogue has been dismissed the calling form can retrieve what ever data it requires from the dialogue before destroying it. Here's a simple example that you can extend to any level of complexity that you want:
VB.NET:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f As New Form2

        f.FieldText = "Enter Text Here"

        Select Case f.ShowDialog()
            Case Windows.Forms.DialogResult.OK
                If f.FieldText = "Enter Text Here" Then
                    MessageBox.Show("You didn't change the prompt text.")
                Else
                    MessageBox.Show(String.Format("You entered '{0}'.", f.FieldText))
                End If
            Case Windows.Forms.DialogResult.Cancel
                MessageBox.Show("You cancelled the dialogue.")
        End Select

        f.Dispose()
    End Sub

End Class
VB.NET:
Public Class Form2

    Public Property FieldText() As String
        Get
            Return Me.TextBox1.Text
        End Get
        Set(ByVal value As String)
            Me.TextBox1.Text = value
        End Set
    End Property

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

End Class
This example assumes that you have two forms named Form1 and Form2 where Form1 is the startup form. It also assumes that you've added one Button to Form1 and one TextBox and two Buttons to Form2, all with the default names.
 
That was it! That was the piece I was missing. ShowDialog is working perfectly and the code now does what it needs to do, passing information both ways. Thanks for your help, both of you!
 
Back
Top