0

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.

Ilja
  • 44,142
  • 92
  • 275
  • 498
  • Is this WinForms? Wrt teminology: a `Tab Control` contains `TabPages`. These can either act as your views, ie contain all the elements you need ot may contain a `UserControl` which then would act as the (reusable) container(s). There is no way (to my knowledge) to make a TabPage invisible. I also would not recommend it, as it would most likely confuse the users.. – TaW Apr 23 '14 at 09:48
  • @TaW I changed my question slightly to use proper terminology. I wouldn't want to make tabPage invisible, but say elements inside it, maybe a view inside a tabPage? Also, I can see how users can be confused, but using tabControl is a requirement for me. – Ilja Apr 23 '14 at 09:53

1 Answers1

1

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

TaW
  • 53,122
  • 8
  • 69
  • 111
  • I see, UserControl sounds like a best solution for me based on your questions. Could you provide a quick usage example, I googled it, but all walkthroughs seem to be for web based asp.net applications, maybe you could link a useful resource that explains how to use it please? – Ilja Apr 23 '14 at 10:20