Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 17th, 2005, 02:23 PM
Registered User
 
Join Date: Dec 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to access form controls from separate class?

I am trying to figure out how to change the properties of controls residing on a form from a separate class. I don't want to make the form controls public (I'm assuming the Windows Form Designer is smarter than I am ;). I created a sample project with a label, two buttons and an external class. (See code below)

One button sends a string to a method in an external class (imaginitively named external_class), which calls an accessor method to change the label. The other button calls the accessor method directly.

The problem is that when the accessor method is called from the external class, it fails silently - it doesn't change the text property of the label. When the accessor method is called via the other button, directly, without going through the external class, it works.

Can anyone explain to me what is going on here?
thanks

Code:
form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FormAccessTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_change_Click(object sender, EventArgs e)
        {
            external_class obj = new external_class();
            obj.chng_lbl("Change via External class");
        }

        public void accessor_method(string labeltext)
        {
            MessageBox.Show("accessor_method call: "+labeltext);
            lbl_status.Text = labeltext;
        }

        private void btn_change2_Click(object sender, EventArgs e)
        {
            // why does calling accessor_method from here work, 
        //but not from external_class.chng_lbl ?
            accessor_method("Direct Change");
        }

        private void lbl_status_Click(object sender, EventArgs e)
        {

        }
    }
}

external_class.cs:
using System;
using System.Collections.Generic;
using System.Text;

namespace FormAccessTest
{
    class external_class
    {
        Form1 frm_UI;
        public external_class()
        {
            frm_UI = new Form1();
        }

        public void chng_lbl(string desired_label)
        {
                frm_UI.accessor_method(desired_label);
        }
    }
}
 
Old December 17th, 2005, 04:50 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

pass the correct argument to the constructor
Code:
namespace FormAccessTest
{
    class external_class
    {
        Form1 frm_UI;
        public external_class(Form1 form1)
        {
            frm_UI = form1;
        }

        public void chng_lbl(string desired_label)
        {
                frm_UI.accessor_method(desired_label);
        }
    }
}
Code:
        private void btn_change_Click(object sender, EventArgs e)
        {
            external_class obj = new external_class(this);
            obj.chng_lbl("Change via External class");
        }


_____________
Mehdi.
software student.
 
Old December 18th, 2005, 03:15 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Mehdi's suggestion is only good for when you initially create the form.

You can leave the form's controls private but provide access to them thru standard property accessors. Chances are that you only need a few properties of a few controls so there's not reason to publicize the whole control. Instead just make the properties you need public. This also reduces later problems if you need to change the control types.

-Peter
 
Old July 31st, 2007, 01:40 AM
Registered User
 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Does anyone know how to do this but access the contents of text field:
ex
frm_UI = new Form1();

frm_UI.textBox1.text;

doesn't appear to be immediately possible

 
Old July 31st, 2007, 08:14 AM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

Phillip,

The earlier messages in this thread explain this. To elaborate, textBox1 is a private member of Form1 so cannot be accessed from an instance of Form1 (frm_UI is an instance).

To make the information available from the instance set up a 'get' accessor ...
public string textBox1Text {
  get {
    return(this.textBox1.text);
  }
}


What you don't know can hurt you!
 
Old August 1st, 2007, 04:06 AM
Registered User
 
Join Date: Aug 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Exactly what i was about to say

www.xhydra.com
 
Old August 1st, 2007, 06:18 AM
Registered User
 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks ever so much. guess I wasn't reading through properly. Sorry.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Access report print each page to separate PDF file conh2so4aq Access VBA 5 July 3rd, 2006 08:00 PM
Create/Add controls to Access Form: VBA program AccessBeginner Access VBA 1 October 13th, 2005 06:48 AM
Access DB from a separate machine rweide Classic ASP Databases 0 March 17th, 2005 05:57 PM
Using Datagrid...getting data on separate form Renu Classic ASP Databases 4 August 7th, 2004 09:52 PM
Opening a separate window from a Form PortGuy Javascript How-To 6 April 9th, 2004 01:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.