Solved

passing vlaues from a Form to a panel in a form

Posted on 2011-02-21
8
479 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
8 Comments
 
LVL 10

Assisted Solution

by:Asim Nazir
Asim Nazir earned 166 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 16

Expert Comment

by:Kalpesh Chhatrala
ID: 34948495
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
 
LVL 8

Accepted Solution

by:
crysallus earned 167 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 167 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Needing an event fired when a row is added on a datagridview 4 36
Syntax Error 2 43
oracle 11g 23 47
Eagerly loading related objects in entity framework 5 24
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
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…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

919 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now