Windows Develop Bookmark and Share   
 index > Windows Forms General > Setting variables on Parent form
 

Setting variables on Parent form

Hi,

I have a Parent form (lets call it Payment)that has allowed a user to open a search form (lets call it Search). I use ShowDialog to open the Search, then the user enteres a string and clicks the search button. Once a selection has been made from the results, I need to send the selected value back to the Payment form and have it reload with the applicable data.

How do I send the selected value back to the Payment form? I have a public variable set up in the class:

namespace PaymentsRemittance

{

public partial class frmPayments : Form

{

// Global Parameters

// The variable below is the one that needs to be set by the users selectionon the search page.

public string strEftid = "";

public frmPayments()

{

InitializeComponent();

//TODO: Get this working on deployment.

try

{

ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;

string str = ad.ActivationUri.ToString();

}

catch { }

}

However I dont know how to set this value from the child dialog (Search).

Can someone please shed some light on this for me?

cheers

chamelionboy  Monday, August 27, 2007 6:22 AM

In the Payment form, just add this code:

/// <summary>

/// Gets or sets the selected value from\to textBox1

/// </summary>

public string SelectedValue

{

get { return textBox1.Text; }

set { textBox1.Text;

}

In the main form, after the call:

Payment.ShowDialog();

you can get the value like this:

string value = Payment.SelectedValue;

Eli Gazit  Monday, August 27, 2007 6:29 AM

I have added that code you specified (changing Textbox1.Text to be my c# variable strEftid instead), however I get the following error when attempting to build the app:

Error3Only assignment, call, increment, decrement, and new object expressions can be used as a statementE:\VisionEFT62\SmartClient\PaymentsRemittance\frmPayments.cs41919PaymentsRemittance

Any ideas?

Also, I am unable to add the code string value = Payment.SelectedValue as the Intellisence does not have SelectedValue as an option to select after Payment. Code on screen is:

Search child page:

private void lstSearch_ItemDoubleClick(object sender, ItemDoubleClickEventArgs e)

{

selectItem(e.Item.Text);

}

private void selectItem(string p)

{

This is where I am trying to set the value back on the Parent form for SelectedValue. I tried

Payment.SelectedValue = p;

However this refused to compile.

}

Payment parent form:

public string strEftid = "";

private void btnSearch_Click(object sender, EventArgs e)

{

frmSearch frmSearch = new frmSearch();

frmSearch.ShowDialog();

}

public string selectedValue

{

get { return strEftid; }

set { strEftid; }

}

Cheers

chamelionboy  Monday, August 27, 2007 6:59 AM

In the Payment form, just add this code:

/// <summary>

/// Gets or sets the selected value from\to textBox1

/// </summary>

public string SelectedValue

{

get { return textBox1.Text; }

set { textBox1.Text;

}

In the main form, after the call:

Payment.ShowDialog();

you can get the value like this:

string value = Payment.SelectedValue;

Eli Gazit  Monday, August 27, 2007 6:29 AM

I have added that code you specified (changing Textbox1.Text to be my c# variable strEftid instead), however I get the following error when attempting to build the app:

Error3Only assignment, call, increment, decrement, and new object expressions can be used as a statementE:\VisionEFT62\SmartClient\PaymentsRemittance\frmPayments.cs41919PaymentsRemittance

Any ideas?

Also, I am unable to add the code string value = Payment.SelectedValue as the Intellisence does not have SelectedValue as an option to select after Payment. Code on screen is:

Search child page:

private void lstSearch_ItemDoubleClick(object sender, ItemDoubleClickEventArgs e)

{

selectItem(e.Item.Text);

}

private void selectItem(string p)

{

This is where I am trying to set the value back on the Parent form for SelectedValue. I tried

Payment.SelectedValue = p;

However this refused to compile.

}

Payment parent form:

public string strEftid = "";

private void btnSearch_Click(object sender, EventArgs e)

{

frmSearch frmSearch = new frmSearch();

frmSearch.ShowDialog();

}

public string selectedValue

{

get { return strEftid; }

set { strEftid; }

}

Cheers

chamelionboy  Monday, August 27, 2007 6:59 AM

I think I didn't understand you the first time (and this time, not completly...).

The data you want to return to the main form is from a ListView\ListBox?

Anyway, I can't understand why using:

strEftId = the value selected

in the selectItem(...) method does not work?

The idea is that if you have a public memeber, like the strEftId, you can get it from the instance of the Payment form.

If you call from the main form to the payment form like this:

Payment payment = new Payment();

payment.ShowDialog();

string value = payment.strEftId;

It should complie.

The only thing left to do is set the value of strEftId after user made a selection in the Payment form (as I explained above).

Hope this is clear, if not, let me know.

Eli Gazit  Monday, August 27, 2007 7:08 AM

Got it. I had some code around the wrong way.

Many thanks for your help Smile

Cheers

chamelionboy  Monday, August 27, 2007 7:39 AM

You can use google to search for other answers

Custom Search

More Threads

• How to set Font Size in code?
• last line of TextBox vs. RichTextBox when resizing form (and thus bottom anchored control)
• Can a button contain both bold and unbolded text?
• Combobox issue when showing a form with showdialog
• Need help with finding a control and setting it's value
• Disable items in CheckedListBox
• Smart Client. What exactly are we talking about?
• == != .Equals
• Modal windows help...
• Sessions