views:

119

answers:

3

I am of the understanding that this can't be done but opted for a public method that would do the specific work I need without exposing my control and keeping it private. So exposing specific properties of a control that I want access to for simple dsicrete set and get operations is fine by me via a public property is also fine.

URL: http://stackoverflow.com/questions/728852/unable-to-access-winforms-control-in-a-class

"Also, make sure that your controls are public, or better add public methods to your form that can be used to manipulate the controls indirectly."

The above quote doesn't make much sense to me ... If I can't see the target classes controls and members from within another winform class that is my calling class then I am completely stuck trying to understand what is going wrong ??? = (

Either approach has been fruitless .... my targetted control is private, my exposed property is public and my exposed method is also public .... what am I doing wrong ???? Threads wasn't as hard as this ... "?:"@#$%!!!

*** PLEASE be as through as possible incase I am being an idiot !!!

A: 

Keep your controls private. You shouldn't be able to directly manipulate controls on a form from outside the form.

So.

If for example you want to disable controls on a form, you would want to write code like this:

form.DisableSomeControls();
// Do some work.
form.EnableSomeControls();

You'd implement those methods a bit like this:

class MyForm : Form
{
    public void DisableSomeControls()
    {
        this.textbox1.Enabled = false;
        this.btnOK.Enabled = false;
    }
}

I expect you get the idea.

Neil Barnwell
A: 

The idea is to hide the implementation from the functions the form provides. If you have a text box control, for example, or the windowTitle, and you want the value set by the caller, it's better to indirectly set the value using a property or method of an instantiated form so if the implementation changes, the calling routine doesn't.

So the calling form creates an instance of the target form and changes it using friend/public properties or methods.

Public Class frmCaller
Private WithEvents _edit As frmEdit

Private Sub editCell()
 If _edit Is Nothing Then
  _edit = New frmEdit
  _edit.MdiParent = Me.MdiParent
 End If
 _edit.init<params>)
 If Not _edit.isNothing Then
  _edit.Show()
  _edit.WindowState = FormWindowState.Maximized
  _edit.BringToFront()
 End If
 End Sub

Public Class frmEdit Friend Sub init()

Beth
A: 

Thanks for the replies. I have implemented as you describe. My targetted class is public and its controls are private and I have exposed a few properties and methods to do some work for me. Problem is when I instantiate the class from within in a calling class, intellisense doesn't show me anything but FORM related members and not my classes members. That is where I am consistently falling over. I though surely after instantiating the class within the calling class, I will have a chance to muck about with properties and methods I deliberately set up. Thing is i can't see them =* (

IbrarMumtaz