Solved

passing vlaues from a Form to a panel in a form

Posted on 2011-02-21
8
481 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

790 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