Hello,

I am doing a side project to teach myself some c# and I am having some issues with Accessing Controls on a User control from a form and hope someone can help.

I made a user control (UCCities) with a TabControl through the designer. This will show up on every new tab that's created on my form form1.

The UC with TabControl will contain:
Tab 1(Units) contains a datagridview dgvUnits
Tab 2(Buildings) contains textbox and a label
Tab 3(Town) contains datagridview2 dgvTown.

I do not want to expose the whole control and want to setup properties. I am having trouble figuring out how to expose the datagrids properly so I can set up the columns text. Also the data is read from a text file from the form.

I was successful setting up the textbox and label properties but cant figure out how to properly call the UC and access the Datagrids from the form.

Do I have to make properties for every thing I want to change or control?


Just to make sure, is this the correct way to call the User control into one of the tabs on my form?
kpCity1 is a tabpage

UCCities test = new UCCites();
kpCity1.Controls.Add(test);

Here is UC code for the data grid that i tried.

public DataGridView DataGrid 
{
get {  return _dataGrid; }
set { _dataGrid = value; } Also tried the DatagridName.
}

And then on the form, no matter what I still don't have access to change settings. I either get an error saying there no instance for the object or no access rights..

So if anyone is confused to put it simple. I just want to Use a user control that has say a datagridview on it and access the UC and be able to modify the grid such as read and write data, change colmnames and etc...

Hope I explained enough o yeah I am not a student :)

Buddy, You are confused. You are right and what you have done is absolutely right.

I think problem in your control - UCCites.

Buddy, You are confused. You are right and what you have done is absolutely right.

I think problem in your control - UCCites.

yes, you are correct I am very confused--lol.

So if what I have is correct how would I access the datagrids from the form? I have the same issue with the tab controls too. I can call the UC and everything shows but i cant change anything. Like add a new tab or fill the datagridview's columnsText and data.

This is the other part but i doubt this is the problem. The last thing i tried was to instantiate the grid no luck either.

public partial class UCCities : UserControl
    {
        private KryptonDataGridView _dataGrid = new KryptonDataGridView();

as it's a user control, you can make the Modifier properity of each control ( tab control , datagrids or any other control) to Public .

Then you can change it as you like from the form.

as it's a user control, you can make the Modifier properity of each control ( tab control , datagrids or any other control) to Public .

Then you can change it as you like from the form.

okay I see what you mean now. Is this the correct way of doing things? I learned exposing the whole control is not good. If not how can I do this the proper way.

thanks

This article has been dead for over six months. Start a new discussion instead.