Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello

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

example:
calling in frmAccount
frmPerson f1 = new frmPerson(); f1.ShowDialog();
Posted

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.
 
Share this answer
 
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:

C#
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            string strSomeProperty = "Hello Form!";

            // create a form
            FormWithProperty form = new FormWithProperty(strSomeProperty);
            form.ShowDialog();

            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)
            {
                InitializeComponentDummy();

                SomeProperty = strSomeProperty;
            }

            private void InitializeComponentDummy()
            {
                m_textbox = new TextBox();
                m_textbox.Multiline = true;
                m_textbox.Dock = DockStyle.Fill;
                Controls.Add(m_textbox);
            }

            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)
 
Share this answer
 
v2
Comments
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!
int a = 10;
calling in frmAccount
C#
frmPerson f1 = new frmPerson(a); 
f1.ShowDialog();

FrmPersons Code: on this form create a parameterized constructor on this as shown below,
C#
int _a =0;
public frmPerson(int a)
   {
      InitializeComponent();
      _a = a;
   }

Access _a where ever you wants.
Message if any problem..
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900