?
Solved

C# crap

Posted on 2011-09-26
4
Medium Priority
?
234 Views
Last Modified: 2012-05-12
Ok... very complicated here.

Form1 has a textbox  with the words "very difficult" in it and a button that launches a Form2.

Is there any way,  in 300 lines of code or less, to have Form2 display a messagebox with the contents of Form1's textbox or will this be a feature in a few years?
0
Comment
Question by:classnet
[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
  • 2
4 Comments
 
LVL 57

Expert Comment

by:HainKurt
ID: 36663381
:) you have to wait for VS2126 for this... not supported yet...
0
 
LVL 57

Expert Comment

by:HainKurt
ID: 36664090
in form2 load event, get the Text of form1.textBox.Text and use it in msg(...)

MessageBox.Show (form1.textBox.Text , "My Application", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 36694939
The code is very simple. Almost as HainKurt showed. However, you need to have a reference to Form1 from the Form2. And it is very common question how we do that. See my code. I have a text box and a button on the Form1 and a button on the form 2.
public partial class Form1 : Form
    {
        Form2 frm = null;
        private void button2_Click(object sender, EventArgs e)
        {
            if (frm == null)
            {
                frm = new Form2(this);
                frm.Show();
            }
            frm.Focus();
        }
        public string GetTexBoxContent()
        {
            return this.textBox1.Text;
        }
    }

=============================
    public partial class Form2 : Form
    {
        Form1 parent = null;
        
        public Form2(Form1 _parent)
        {
            InitializeComponent();
            this.parent = _parent;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            MessageBox.Show(parent.GetTexBoxContent());
        }
    }

Open in new window

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 36707497
First, select the TextBox (textBox1) on Form1 and set its Modifiers() property to Public.

Next, when you display Form2 with Show(), pass in your instance of Form1 via "this" to set its Owner:  
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show(this); // <-- pass in this form as the Owner()
        }

    }

Open in new window


Finally, in Form2, cast the Owner() to Form1 and grab the value from the TextBox:

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Shown(object sender, EventArgs e)
        {
            if (this.Owner != null && this.Owner is Form1)
            {
                Form1 f1 = (Form1)this.Owner;
                MessageBox.Show(f1.textBox1.Text);
            }
        }
    }

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

777 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