Click here to Skip to main content
11,921,491 members (62,720 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#3.0 C#

can a form reflect a value(like a function)?
how? Smile | :)
when calling in other form.

calling in frmAccount
frmPerson f1 = new frmPerson(); f1.ShowDialog();
Posted 6-Mar-12 1:59am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Why not use DialogResult if you want to return back the success or failure. In case you want to pass some data between forms them their is a very good article [here]explaining how it can be done.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Hi faezun, I think you should work on your general .net/OOP understanding. A constructor like you have given in your example NEVER returns a value!!!! (only the constructed object) So someproperty = new frmPerson(); never works (same in C++, Java...). Just expose properties and maybe initialize with parameterized constructor (like pashad showed you).

Maybe I can give you a similar (but full)example:

using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
    static class Program
        static void Main()
            string strSomeProperty = "Hello Form!";
            // create a form
            FormWithProperty form = new FormWithProperty(strSomeProperty);
            MessageBox.Show("Property was: " + form.SomeProperty);
        class FormWithProperty : Form
            TextBox m_textbox; 
            // don't forget to write a default constructor if you provide a parametrized one, and want the possibility to construct the form with a default value...
            public FormWithProperty() : this("DefaultString") { }
            public FormWithProperty(string strSomeProperty)
                SomeProperty = strSomeProperty;
            private void InitializeComponentDummy()
                m_textbox = new TextBox();
                m_textbox.Multiline = true;
                m_textbox.Dock = DockStyle.Fill;
            public string SomeProperty
                get { return m_textbox.Text; }
                set { m_textbox.Text = value; }
(copy to a new WindowsForms-Project and replace the file Program.cs content with my example code -> run)
faezun 6-Mar-12 9:13am
thanks. :)
johannesnestler 6-Mar-12 11:25am
I'm glad I was able to help - good luck with your project!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

int a = 10;
calling in frmAccount
frmPerson f1 = new frmPerson(a); 
FrmPersons Code: on this form create a parameterized constructor on this as shown below,
int _a =0;
public frmPerson(int a)
      _a = a;
Access _a where ever you wants.
Message if any problem..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 553
1 Dave Kreskowiak 245
2 phil.o 160
3 KrunalRohit 134
4 Sergey Alexandrovich Kryukov 132
0 OriginalGriff 6,951
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,171
3 George Jonsson 2,840
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web04 | 2.8.151120.1 | Last Updated 6 Mar 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100