Click here to Skip to main content
11,642,233 members (64,392 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
hello

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

example:
calling in frmAccount
frmPerson f1 = new frmPerson(); f1.ShowDialog();
Posted 6-Mar-12 0:59am
faezun360
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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
    {
        [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)
  Permalink  
v2
Comments
faezun at 6-Mar-12 9:13am
   
thanks. :)
johannesnestler at 6-Mar-12 11:25am
   
I'm glad I was able to help - good luck with your project!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

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



Advertise | Privacy | Mobile
Web03 | 2.8.150731.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