Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

passing vlaues from a Form to a panel in a form

Posted on 2011-02-21
8
Medium Priority
?
486 Views
Last Modified: 2013-12-17
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
Comment
Question by:ferocious
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 10

Assisted Solution

by:Asim Nazir
Asim Nazir earned 664 total points
ID: 34948487
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
 
LVL 8

Expert Comment

by:crysallus
ID: 34948508
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 8

Accepted Solution

by:
crysallus earned 668 total points
ID: 34948555
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
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 668 total points
ID: 34948593
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
 
LVL 8

Expert Comment

by:crysallus
ID: 34948633
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
 
LVL 8

Expert Comment

by:crysallus
ID: 34948653
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
 

Author Comment

by:ferocious
ID: 34948684
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question