Solved

C# crap

Posted on 2011-09-26
4
227 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
  • 2
4 Comments
 
LVL 51

Expert Comment

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

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 29

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 85

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

11 Experts available now in Live!

Get 1:1 Help Now