Passing a Form as an argument

  • Thread starter Thread starter Dom
  • Start date Start date
D

Dom

In VB I was able (I think, it's been a long time after all) to pass a
Form as an argument to a procedure, and then within that procedure, to
access the controls. For example,

public void MyProc (frmMain f)
if (f.OptionButton.value = 1) ...

It seems I can't do this in CSharp. What do you do to take it's
place? I have several controls I need to check in the method.
 
Dom,

You can pass a form to a method in C#. A Form (or rather, a class which
you derive from Form) is nothing more than a class, like any other classs.

C# is different from VB6 in that the designer, when creating controls,
makes those controls publically accessible. You can do this in C# by
changing the access modifier of the control (through the designer) to
public.

Of course, you have to be careful, as exposing your control on the form
might lead to it being used in unintended ways. You might be better off
creating a method on your Form class which takes the parameters you need and
then performs the operations you need.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top