user123_456
user123_456

Reputation: 5795

How to load class into form and access it

I need help with accessing class from Form.

So I'll put my code so you can see what I mean.

So I have made several classes. For example:

public class Landscape
{
    public DataGridView grid;

    public void init()
    {
        grid = new DataGridView();
        // 
        // grid
        // 
        grid.AllowUserToAddRows = false;
        grid.AllowUserToDeleteRows = false;
        grid.AllowUserToResizeColumns = false;
        grid.AllowUserToResizeRows = false;
        ...
        grid.AutoSizeColumnsMode = grid.Size = new System.Drawing.Size(790, 427);
        grid.TabIndex = 0;
    }
}

So basically this would make me datagridview on my form.

When I create form:

public partial class MyScreen: Form
{
    public MyScreen()
    {
        InitializeComponent();
        Landscape land=new Landscape();
        land.init(); //this should draw me datagrid on my form
    }
}

Shouldn't this code draw me my datagrid into form?

How to achieve this?

You mean something like:

public partial class MyScreen: Form
{
    public MyScreen()
    {
        InitializeComponent();
        Landscape land = new Landscape();
        this.Controls.Add(land.grid);
        land.init(); 
    }
}

But it's not working...

Upvotes: 1

Views: 623

Answers (1)

Ben
Ben

Reputation: 559

No, because you have just created a data grid view in a class but not added it to the forms controls. Add the line

land.init();
this.Controls.Add(land.grid);

To add the control to the form

Upvotes: 2

Related Questions