Solved

C# crap

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

773 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