Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 488
  • Last Modified:

passing vlaues from a Form to a panel in a form

hey guys,
I am having problem with passing values from a form to the parent form's panel(created at run-time),

i have a panel and inside the panel i have a textBox and a button(will open the 2nd form),When im i the 2nd form(contains a textBox and a button) i would like to enter a name and click the button to be able to view the name in the form1's panel.

i have other values which i'm passing between forms using Event handler and it works fine
but having problem with passing the value from the 2nd form to the first form's panel.
plz help
0
ferocious
Asked:
ferocious
3 Solutions
 
Asim NazirCommented:
Hi,

Ok. So on clicking child forms button, you close this child and then want to view the name entered on child form to be displayed on parent. Is it or ?

If so, then you can expose a property for value entered on child, before disposing child, you can set this property value to parent area. You can either Find a control in parent form panel using a recursive method or using some other logic...

Let me know if you need further assistance.

Asim
0
 
crysallusCommented:
One method which I've got to work:

1. Create a simple method in form1 that set's the text of the text box in form 1, when passed a string as argument.

2. Create a delegate with that method signature.

3. Create a private variable in form 2 to hold the delegate, and add that delegate to form 2's constructor, so that when you create the second form, you can pass the method you created in step 1 to form 2.

4. In the click event for the button in form 2, simply call the delegate passed to form 2, passing it the value in the form 2 textbox.

Let me know if that made sense or you need further explanation or code samples.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
crysallusCommented:
Thought I'd give you code sample's anyway, for my steps above (you may wish to change the names, as mine are somewhat non-specific):

1. Form1:
private void SetTextBoxValue(string text)
{
    textBox1.Text = text;
}

Open in new window


2.
public delegate void SetTextBoxDel(string text);

Open in new window


3. Form2
private SetTextBoxDel SetForm1TextBox;

public Form2(SetTextBoxDel setTextBox)
{
    SetForm1TextBox = setTextBox;
    ...
}

Open in new window


Form1:
private void button1_Click(object sender, EventArgs e)
{
	Form2 form2 = new Form2(SetTextBoxValue);
	form2.Show();
}

Open in new window


4. Form2
private void button1_Click(object sender, EventArgs e)
{
	SetForm1TextBox(this.textBox1.Text);
	this.Close();
}

Open in new window

0
 
anarki_jimbelCommented:
I believe the code from crysallus is a bit overcomplicated.
I prepared simple example with two forms. We create a main form (Form1) and pass it's reference to a Form2 constructor. So - Form2 knows about the Form1.

Form1 has a method to set text on a panel. Form 2 can call this method. Simple...
namespace TwoForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(this);
            frm2.Show();
        }


        internal void setTextOnFormOne(string text)
        {
            this.textBox1.Text = text;
        }
    }
}
//------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TwoForms
{
    public partial class Form2 : Form
    {
        Form1 _parent;
        public Form2(Form1 parentForm)
        {
            InitializeComponent();
            _parent = parentForm;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            _parent.setTextOnFormOne("Hello");
        }
    }
}

Open in new window

0
 
crysallusCommented:
Overcomplicated?

Correct me if I'm wrong, but my method requires one more line of code than yours does, the creation of the delegate in step 2. Aside from that it is identical to yours, except that I'm passing the method, your passing the entire form.
0
 
crysallusCommented:
Though I will acknowledge, that if Form2 needs to access more than just that one method from Form1, then passing Form1 is simpler than creating a delegate for every single method that you might need, in which case you would go with your method instead of mine.

But still, what I said wasn't that complicated...
0
 
ferociousAuthor Commented:
hey thanks a lot guys, i was thinking it would it be very difficult but seems very doable :)

i have created a get method in my child form
 Form 2.cs
public string getName
{ get {return name;} }
and in the button just
this.dialogresult = dialogResult.OK
this.close.

and then in my form1
Form2 newForm = new Form2()
dialogResult result = newForm.ShowDialog();
if( result == dialogresult.ok)
parentTextBox.text = newForm.getName;

i dont know how to reward all of you guys.
Thanks :)
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now