Solved

passing vlaues from a Form to a panel in a form

Posted on 2011-02-21
8
478 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
Comment Utility
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
Comment Utility
0
 
LVL 8

Expert Comment

by:crysallus
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 167 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

771 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

10 Experts available now in Live!

Get 1:1 Help Now