Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Forms returning values

Posted on 2010-01-11
5
Medium Priority
?
264 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
  • 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

Industry Leaders: 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 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…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

580 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