Solved

C# crap

Posted on 2011-09-26
4
233 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 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 36663381
:) you have to wait for VS2126 for this... not supported yet...
0
 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
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 500 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

Independent Software Vendors: 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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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