Solved

C# crap

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
C# Desktop Application 3 54
Create a WebAPI using a generic return parameter 9 32
insert value of checklistbox checked 4 32
C# LINQ ForEach() question 6 55
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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