tags:

views:

290

answers:

7

Hello,

I am new to C# (coming from Delphi) and find my main.cs file becoming quite large. In Delphi I can create classes in a .pas file and use it from my main application file. I simply add the uses (filename) keyword.

My question is how do I do something similar in C#? Example:

Delphi:

Button1.Visible = False;

I can refer to the buttons properties in another unit by adding a reference to the main unit:

frmMain.Button.Visible = True;

In C# I would like to move this code to another .cs file and call it from my main form. It does not work.

private void manageMainMenuButtons()
{
  if (rtbSharedSARP.Text == "")
  {
    utmToolBar.Tools["First"].SharedProps.Enabled = false;
    utmToolBar.Tools["Previous"].SharedProps.Enabled = false;
    utmToolBar.Tools["Next"].SharedProps.Enabled = false;
    utmToolBar.Tools["Last"].SharedProps.Enabled = false;
  }
  else
  {
    utmToolBar.Tools["First"].SharedProps.Enabled = true;
    utmToolBar.Tools["Previous"].SharedProps.Enabled = true;
    utmToolBar.Tools["Next"].SharedProps.Enabled = true;
    utmToolBar.Tools["Last"].SharedProps.Enabled = true;
  }
}

Not the best example, but I hope someone can help me.

+2  A: 

Unless you have hundreds of controls on your "main" form, it sounds like you are not separating business logic and presentation logic. You should set up separate classes (in separate files) to handle your business logic, and each form should get its own designer and code behind file.

Jess
+1  A: 

C# is object oriented, so you will need a reference to the instantiated window in your other classes. If you add Form mainForm as an argument in each of your methods, and from the form class pass it this, in your external methods you can say mainForm.property.

That being said, your question leads me to think you would benefit from some reading on object oriented design, and application architecture with c# and winforms.

Matt Briggs
To add to that last sentence, passing a reference to the main form to methods like that is not a great idea. If other classes need some access to your form, create events in the child controls that can be handled by the main form indstead.
Ed Swangren
+4  A: 

First, you need to make sure you're in the same namespace:

namespace mynamesspace {

Next, if your class members are declared privately, you need to indicate you're working on that class:

public partial class frmMyForm : Form {

Note though, that this is extending the original class. You may just need to specify the namespace, if you're accessing public members (which is a good way to go about things).

overslacked
While this is definitely an answer to his question...ick. I really dislike partial classes. I guess for generated code its ok, but otherwise they've got code smell all over.
George Mauer
I like partial classes for the ability to hide/separate boilerplate code; I also like the idea of different developers being able to check out from source control different aspects of the same class at the same time. At least in theory - I haven't found this to be an ideal solution in real life.
overslacked
+2  A: 

Assuming that you are using best practices and separating logic appropriately (if you're not that would be a good place to start), C# uses what's called partial classes to allow you break up a class into multiple files. For example:

Main1.cs:

namespace Program
{
    public partial class Main
    {
        private Form frmMain;
    }
}

Main2.cs:

namespace Program
{
    public partial class Main
    {
        void MainMethod()
        {
             frmMain.Button.Visible = true;
        }
    }
}

MainMethod can access frmMain because as far as the compiler is concerned, they are in the same class definition despite being in different files.

Jesse Dearing
A: 

In this particular case, utmToolBar is almost certainly a control on your form and a variable name in the scope of your form class. You could migrate it to another file, but that's probably not the right way to break your file down.

Any time you want to add a class to a C# project, you can do so by right-clicking on your project in the project explorer and selecting Add Class. Then, you can move functionality into that class with a simple cut-and-paste, replaced by a class reference.

This so barely touches the tip of the iceberg that I recommend you pick up any basic C# book and start reading. It will make far more sense than any answer fit for this forum.

Jekke
A: 

You could make the manageMainMenuButtons a static method of whatever other file and pass a reference, or use it as a utility or even create the other file with a Partial definition of your form. Here is an example as a static method with a little bit of refactoring. I am not sure what utmToolBar is so I tried what I thought.

XXX.manageMainMenuButtons(utmToolBar, string.IsNullOrEmpty(rtbSharedSARP.Text));

public static void manageMainMenuButtons(Object /*whatever utmToolBar actually is*/ utmToolBar, bool disable)
{
    utmToolBar.Tools["First"].SharedProps.Enabled = disable;
    utmToolBar.Tools["Previous"].SharedProps.Enabled = disable;
    utmToolBar.Tools["Next"].SharedProps.Enabled = disable;
    utmToolBar.Tools["Last"].SharedProps.Enabled = disable;
}
Quintin Robinson
A: 

If you want to access controls, such as a button on a form, from a code in a seperate file, you just need to make sure that the code in that file is contained in the same namespace as your form.

For example, in your form designer file:

namespace MyWindowsApp{
    class MyForm{
        //various control declarations
    }  
}

Then, in an external file:

namespace MyWindowsApp{
    class MyClass{
        //Miscellaneous code
    }
}

MyClass will have knowledge of and access to MyForm and all its public properties.

Hope this is some help, Lee

Lee D