Windows Develop Bookmark and Share   
 index > Windows Forms General > Passing Variables Between Forms
 

Passing Variables Between Forms

This is probably a silly question to ask here, but I need to be able to pass variables between forms. What I mean is, for example, you open a form that asks for a URL and after you hit the OK button, the function that called it can use the URL.

I can use a global variable to accomplish this, but I'm sure there must be a way to do it more elegantly. I know how to do this sort of thing in MFC, but I'm fairly new to .NET programming in VC++ 2005.

Any help would be greatly appreciated.

Thanks.
Johan Cyprich

Johan Cyprich  Wednesday, May 24, 2006 6:00 PM
Create a public property called URL. On OK save the entered URL to the property. Set your OK button to DialogResults.OK.
JRQ  Wednesday, May 24, 2006 10:09 PM
Create a public property called URL. On OK save the entered URL to the property. Set your OK button to DialogResults.OK.
JRQ  Wednesday, May 24, 2006 10:09 PM

Lets say you have two forms Form1 and Form2

The example is in C#-you should be able to port it to C++

public class Form1 : Form

{

public Form1()

{

InitializeComponent();

...

...

}

public string GetUrl()

{

Form2 f = new Form2();

DialogResult result = f.ShowDialog(this);

if(result == DialogResult.OK)

{

//Get Url from Form2

return f.Url;//Retrieve the URL here

}

}

}

public class Form2 : Form

{

private string url;

public Form1()

{

InitializeComponent();

...

...

}

public string Url

{

get

{

//Get the value

return m_url;

}

set

{

//May be gets saved when the user clicks an OK Button

m_url = value;

}

}

}

Karthik Krishnaswami  Thursday, May 25, 2006 12:47 AM

If your not using a modal form then you can provide the notification by using an event instead of the DialogResult from the show modal call. Just add a standard event to your form as a public property and then fire this when your form is finished. The original form can hook into this event before showing and so be notified when the action is complete.

Phil Wright
http://www.componentfactory.com
Free user interface controls for VS2005

Philip Wright  Thursday, May 25, 2006 7:50 AM

Johan,

is your question answered and you are able to pass variables between forms?

Andreas Johansson  Tuesday, May 30, 2006 8:20 PM

Hi Andreas. I'm going to try these solutions tonight. I've been too busy at work to do anything else this week. I'm working on application with multiple forms that need to pass variables to other forms.

I'll post my solution if it works. :)

Johan Cyprich  Thursday, June 01, 2006 12:56 AM

I finally was able to use a variable from another form.

As a test, I created a form that had a text box (named txtForm1_URL) and a button. When the button was pressed, it would open a form (frmEnterURL) requesting the user to enter a URL. On that form, there was a text box (txtURL) for entering the URLand anOK (btnOK) and Cancel (btnCancel) button.

I created a property on frmEnterURL for storing the value entered:

public:
property String ^URL;

I wrote two events for it:

System::Void btnOK_Click(System::Object^ sender, System::EventArgs^ e)
{
URL = txtURL->Text;
}

System::Void btnCancel_Click(System::Object^ sender, System::EventArgs^ e)
{
URL =
""
;
}

I had to set the URL="" for the Cancel button because it was saving the txtURL->Text value to URL when Cancel was pressed. I don't know why it would do that.

Back to the main form, I was able to set the txtForm1_URL->Text valueby reading the property in frmEnterURL.

frmEnterURL ^box = gcnew frmEnterURL ();

box->ShowDialog ();

if (box->URL != "")
txtForm1_URL->Text = box->URL;

I hope my code makes sense here. I'd like to thank everyone for their help. I'd like to try Philip Wright's solution since it is a different approach from the others.

Johan Cyprich  Friday, June 02, 2006 12:44 AM

You can use google to search for other answers

Custom Search

More Threads

• error when log in window(too many parameter)
• To get the process Id from .exe name or to get the ProcessId while execution of batch file name
• Need Help with Wireless issues (WLAN - SSID) automation
• Using Threads in Serial port Connection
• Which method is better for internal paging ?
• Parent Form Picture Box shows though Child forms!
• Property Grid with file path
• How to get hDC for PictureBox?
• What permission is required for Process.Start()?
• Testing for auto-size doing the right thing