CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2012
    Posts
    2

    Unhappy Having issues in data handling of windows forms.

    Dear All,

    I am new in C# windows programming and having issues in handling data of windows forms.

    Here is the scenario.

    I have created one windows form (named as form1) in which i am taking input via various controls such as text boxes, list boxes, combo boxes and trying to show it in another form , till showing one windows form's input value in second form is working fine but when I try to access values of form1 in third form in order to do some computation based on form1 values I am unable to access them.

    Please help me in this scenario that how could access the values of form1 in the third form.

    If required I can paste the code here as well.

    Thanks,

    Fahad Bin Aziz.

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Having issues in data handling of windows forms.

    Well, to tell the truth, you should not use controls forms as a repository of data you could access from everywhere: forms controls usually have the main duty to represent data in some visual ways.

    In any case, I understand it might seem easy -as you could think you have data loaded therey - to try to access from some other forms

    You could do it from form2, but you could not for form3 - either your foem1 has been destroyed in the meantime or the controls are inaccessible due to their accessor (Private instead of Friend) or the real provenience of your third form (is it in same assembly?)

    A bit of code would be of help to understand what you're trying to achieve. In any case, you could expose data outside a form making public properties to let other pieces of assembly get and if needed set data of form1.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Mar 2012
    Posts
    17

    Re: Having issues in data handling of windows forms.

    Quote Originally Posted by fahadaziz85 View Post
    Dear All,

    I am new in C# windows programming and having issues in handling data of windows forms.

    Here is the scenario.

    I have created one windows form (named as form1) in which i am taking input via various controls such as text boxes, list boxes, combo boxes and trying to show it in another form , till showing one windows form's input value in second form is working fine but when I try to access values of form1 in third form in order to do some computation based on form1 values I am unable to access them.

    Please help me in this scenario that how could access the values of form1 in the third form.

    If required I can paste the code here as well.

    Thanks,

    Fahad Bin Aziz.
    the only way the others FORM can controls FORM1
    is to give them by parameter
    Code:
    class Form1
    {
         Form2 fr;
         public Form1()
         {
               fr = new Form2(this);
         }
    }
    class Form2
    {
         Form1 parent;
         public Form2(Form1 x)
         {
              parent = x;
         }
    }
    And setting all the component on the FORM1 to PUBLIC
    hope this help

  4. #4
    Join Date
    Mar 2012
    Posts
    2

    Re: Having issues in data handling of windows forms.

    Actually following is the code that how i am doing this
    Code:
    namespace SmallTestApplication
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                previewinput dos = new previewinput();
                dos.Show();
                dos.label2.Text = textBox1.Text;
            }
    
            public string getdataform3()
            {
                Form3 form3obj = new Form3();
    
                form3obj.getvar = textBox1.Text;
                return form3obj.getvar;
            }
        }
    }
    
    
    namespace SmallTestApplication
    {
        public partial class previewinput : Form
        {
            public previewinput()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form3 objform3 = new Form3();
                objform3.Show();
               
            }
        }
    }
    
    namespace SmallTestApplication
    {
        public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();
            }
    
            public string getvar;
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form1 objform1 = new Form1();
                 MessageBox.Show(objform1.textBox1.Text)
                //MessageBox.Show(objform1.getdataform3());
    
                //previewinput dos = new previewinput();
                //MessageBox.Show(dos.label2.Text);
    
            }
        }
    }
    Last edited by Cimperiali; March 22nd, 2012 at 12:44 PM. Reason: added [code][/code] tags

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Having issues in data handling of windows forms.

    Most likely the problem is that your forms are going out of scope or you aren't passing in a reference to the form you need to access.

    In general, accessing the controls of one form from another isn't an approach that scales well or is very maintainable because of the tight coupling between the forms.

    Sure, it's conceptually easy to understand, but in practice, it's tough to maintain because if a user changes the control in one form, it may very well break other forms.

    Another way to approach this problem is to use one of the MVC/MVP design patterns or a simplified version that is just a model/view pattern.

    The model/view pattern is pretty simple to understand once you've done it the first time. In this approach, you create a class that holds the data for all the forms (called the model). Then each form gets/sets the data to a single instance of the model. Each form has limited responsibility and only gets or sets the model data that the form exposes to the user.

    The important thing is that each form has no idea (nor cares) if other forms access the model data. This loosely coupled approach leads to better maintainability.

    It's actually harder to describe this in words rather than in code.

    If you zip up a complete test solution, I'll modify it to use the model/view approach I'm talking about.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured