?
Solved

Forms returning values

Posted on 2010-01-11
5
Medium Priority
?
255 Views
Last Modified: 2012-05-08
Hey, there. For my application I want to be able to exchange information between forms. (Only on execution and termination).

For example: On the form "main" there is a button called choose number. When you press it form "number " opens, lets a user type in the number, the value is returned by the form "number" to a variable in form "main".

Another example: I want to create a large form dedicated to displaying images, when I create it I want to pass a bitmap so that the form can display it.
0
Comment
Question by:freefrag
[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
  • 2
5 Comments
 
LVL 30

Assisted Solution

by:Reza Rad
Reza Rad earned 400 total points
ID: 26282299
you can create public or internal properties of controls on your form
and then use them in second form. like this:

public class form1:Form
{
public static string myValue;
}

and everywhere you want to use it , use it like this:

form1 frm=new form1()
frm.myValue="test";


you can do it for any controls in your forms
0
 
LVL 9

Expert Comment

by:magicdlf
ID: 26282355
@reza_rad: I have a question: why do you make string static? Doesn't this causing problem when you have several instances of the same class?

@freefrag: Yes, properties is the best way to solve your problem, the following is another way to define a property in .NET:
public class form1:Form
{
   private string _myValue;
   public string MyValue
   {
      get { return _myValue; }
      set { _myValue = value; }
   }
}
0
 
LVL 30

Expert Comment

by:Reza Rad
ID: 26282408
@magicdlf:
you are right, I had a mistake. there is no need to define this property as static.
Thanks.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 600 total points
ID: 26284265
For your first example, involving the "number" form, create a property on it as reza_rad and magicdlf have discussed.

Now show the "number" form using ShowDialog() which causes execution to stop until it is closed.  Then you can query the property afterwards.

In "number", you can set the DialogResult property to indicate whether the operation was cancelled or not.

Put all together, with Form2 as the "number" form:

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            if (f2.ShowDialog() == DialogResult.OK)
            {
                this.label1.Text = f2.Number;
            }
        }

    }

    public partial class Form2 : Form
    {

        public Form2()
        {
            InitializeComponent();
        }

        private string _number;
        public string Number
        {
            get { return _number; }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this._number = textBox1.Text;
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

    }
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 26284313
For your second example you can either use a property as previously discussed or you can pass in the image to the constructor:

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

        private void button1_Click(object sender, EventArgs e)
        {
            // ...get your Bitmap somehow...
            Bitmap bmp = new Bitmap(100, 100);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.Red);
            g.Dispose();

            Form2 f2 = new Form2(bmp); // pass "bmp" into the constructor
            f2.Show();
        }
    }

    public partial class Form2 : Form
    {

        public Form2(Image img)
        {
            InitializeComponent();

            this.pictureBox1.Image = img;
        }

    }
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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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 video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

765 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