How to load a new layout in C# when a button is pressed? - c#

I want to load a new layout (just like we do in Android programming as layout1.loadLayout()) in C# when a button is pressed?
How can this action take place when a method is called?
If I am not clear in my question, I am looking to load a new set of controls and hiding the current controls temporarily on a window form when a button is clicked (while they should retain their current properties, so that if I go back they should be in the same way where I left them).
How can I jump between different layouts while not completely deleting and creating them again and again?

if this is for a desktop app, look into user controls.
info here

You could create panels to group controls and toggle it's visibility.
Look for panel control in components, then add controls to it, toogle it's visibility.

Related

How to add multiple tabs or pages in Win forms Application

This is UI of the app which I'm creating, So if a user clicks Home button he should be able to play with some specific controls and again if he presses Sellers button, new tools and controls should be visible on the screen!
I tried to implement this by using 4 panels with
panel_name.visible=false
in respective button click event, but the problem is each panel should take up 1/4th of the space in the form to display all of its respective tools.This makes the panel area too small!
How can I make the application such that If a user clicks Home button, he should see one page/tab and different tabs/pages for different buttons clicks?
I suggest you use the user controls: right click on your solution> add> user control
C# UserControl
How to create and use UserControl
The user controls are similar to the forms, since you can place controls on them, the difference is that instead of showing them as common forms, you drag them as a control inside the form

why are new controls showing up in my form AND panel

i have a form with a few controls on it, i have a panel with different controls on, That panel is above the form controls.
i have added new controls to the form and when i place the panel back over the form, the new controls are showing through the panel itself, but only the new one controls and not the "old" ones, if i copy and paste a control or add a new one it has the same effect.
i have looked in the designer.cs and the new controls are being added to the form and NOT to the panel itself.
this is weird and iv checked various properties but cannot immediately see the reason for this.
i have made MANY forms before and this is the 1st time this is happening.
one the note of controls, is there a way to change the default value of the labels "AutoSize" property from TRUE to FALSE; i'm using visual studio CE2015
any ideas on what to check? im really stumped by this one.
As mentioned in the comments, you need to check the z-order of your controls.
In the picture below you can see a form I created with two buttons and a panel as you were describing. Neither button is one the panel, however button 3 has a z-order that puts it on top, just as the panel is on top of button 2
If you right click on the controls you want to change the z-order of you see Bring to Front and Send to Back. Choose the appropriate option.

Load User Controls dynamically based on other control events

I have a "cross section" of variables that I need to use to generate a set of user controls dynamically. In the center of the page I have a Telerik multitab/page. I have a custom tree menu and a custom menubar, based on the combination of menu input each of the tabs should load a user control relevant to that cross section of data.
For clarity, almost each tab, treeview, menubar combination needs a unique control.
My problem is that all the postback/loading happens well before the "OnMenuChanged" event triggers, so I'm one "set" of user controls behind. Even if I were to use session/viewstate they wouldn't get assigned until after I needed the value stored in them.
Currently what is happening is the default user controls are loaded in the "pageviewcreated" event, then in the onMenuItemChanged I go back and reload the user controls. It seems very inefficient and is complicating up the approach for selecting the right .ascx.
How do I manage this?
If I understand what you're trying to do could you not use a placeholder control and inject your needed usercontrol into that inside of the events you're managing? Try doing something like this inside of the "OnMenuSelected" event.
WebUserControl1 uc = (WebUserControl1) Page.LoadControl("WebUserControl1.ascx");
PlaceHolder1.Controls.Add(uc);

C# Winform Question

I'm just getting started with winforms in C# and things are going well, except I want to learn how to do something like:
if user presses "proceed" button, it will run some code, and change the form, so that there are more options available to the user on the form. In other words, I want to make my forms more than one "page". I hope that all makes sense. Any help would be appreciated, thanks!
You could have a tab control with separate tabs that you move through that are disabled until they are needed. Each one could have its own controls, etc... You could have the controls disabled on the main form and then enable them, etc... There are myriad ways that you could approach this problem.
For winforms:
private void button1_Click(object sender, EventArgs e)
{
textBox1.Visible = true;
}
One easy way of doing that is to add a container control, for example a GroupBox that contains a selection of controls that belongs together and then you make that GroupBox invisible. Then in the button click handler you make it visible again.
That way all the related controls are shown/hidden together and if needed you can move them around the screen without needing to worry about their locations related to each other.
I want to make my forms more than one "page"
When I do that, I normally build each "page" as custom control. This let's me separate the navigation elements from what it is you're navigating to, so that I can easily play with different ideas later. For example, do I want to use a tab control to host each page inside a separate tab? No bid deal - just drop one my controls on each tab. Swap out different pages on the same form? It's only one control to replace. Move between completely different forms? Still easy.
As a basic starting point, if you want to run some code when the user clicks the button just handle the Click event. Just double click the button to get to the code-behind and place your code in there.
In terms of exposing extra functionality when the user clicks proceed there are various options available to you, its really up to you to figure out which is best suited to your application. One simple option would be to perhaps have a Panel or GroupBox and place all the extra options on there and make it disabled, then enable when you see fit.

How do I make a single control appear across many tab control pages?

I have a WPF tabcontrol with 3 tabs. On the top of the first page is a scrollviewer with a couple buttons which make up a menu of common tasks (save, load etc). I would like this scroll viewer to appear at the top of every tab. Is it possible to do this without simply copying and pasting the code to every tab?
You can make a custom control that contains the UI and logic for the buttons, and then include that control on each tab. The best way to do this is to create a subclass of ScrollViewer, and in the XAML define each of the buttons. On each of your tab pages you can create and create an instance of your new subclass.
This will result in a different instance of your class on each page, but the logic for the buttons will only exist in the code once.
You could implement the scroll viewer and buttons outside and on top of the tabcontrol.

Categories