Ilja
Ilja

Reputation: 46479

Changing and passing content inside tabControls tabPage

I am working in windows forms application and have a following issue. I use tabControl in my application and there is a need to change a content inside certain tabPages when users perform specific actions.

for example tabPage one contains a text area and a button, when user clicks button information inside a text area should be stored somehow, and that same tabPage should display new content e.g. more text areas, buttons etc, I assume it is easier to do by using views inside it, so one view can be hidden and another can be shown.

Upvotes: 0

Views: 144

Answers (1)

TaW
TaW

Reputation: 54433

This a to a degree a matter of taste. You can chose to show and hide controls one by one in a method or you can group them in a UserControl which you then show or hide in one command.

I would base my decision one way or the other by these questions:

  • Are there controls, that will always be visible and how is the layout for these?

  • How many controls are there to show/hide?

  • Is there a need to reuse one or more of your views?

The last question may make the big difference: If you want re-use, do go for the UserControl. It is basically meant to do just that: Group controls, like a form does.

For just a few controls doing it in a one by one (in a switchViewMode-method) would suffice, imo.

To add UCs you right-click your project in the project-explorer and chose add - usercontrol. Then chose a nice name, like UC_Goods or UC_Services or whatever your shop policy suggests.

You are then presented with the empty GUI. Now add the controls you need.

Here a decision is to be made: If you will reuse it make sure the controls get generic names! If not it doesn't matter. The reason is, that when you add two instances of the same UC, their controls will have the same names and you will have to qualify them by the parent (the UC)

Here you also script events etc..

Finally add instances to the TabPage as need like this:

public UC_Goods uc_goodsDelivered = new UC_Goods();
public UC_Goods uc_goodsOnHold = new UC_Goods();
public UC_Services uc_ItServices = new UC_Services ();

public Form1()
{
  InitializeComponent();
  tab.tp_goodsPage.Controls.Add(uc_goodsDelivered);
  tab.tp_goodsPage.Controls.Add(uc_goodsOnHold);
  goodsOnHold.Hide();
  tab.tp_goodsPage.Controls.Add(uc_ItServices);
  uc_ItServices .Hide();
  // take care of dock style or anchors..
  // ..and initialzing fields..
}

This delclares two UC classes and two and one instance of each respectively. Only one is visible. Since one class is used twice its controls have ambigous names until you qualify them e.g. like this: uc_goodsDelivered.Status...

hth

Upvotes: 1

Related Questions