Click here to Skip to main content
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 1: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)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 6 Mar 2012
Copyright © CodeProject, 1999-2014
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