Change design of C# form without opening a new one? - c#

I'd like to know if there is a way that I can change the entirety of the layout on my form without having to open a new one, almost as though I am opening an new scene, similar to Unity.

If I'm understanding correctly, you want to display an entirely different page without opening a new form. A not efficient but functional way would be to lay your objects for each page on top of each other and disable/set visible to false for all of the objects you wish not to display. Then alternate which are visible depending on which page you want to display.
P.S I don't recommend doing this because of the inefficiency but if you choose to do so, putting controls for a specific page into a single List will help

Related

How do I make controls extend passed a control bound or overlap with others?

I have a wpf grid setup where I have two custom buttons that are next to each other. First picture is how the design window looks like, however, functionally, it looks like the second picture. I want them to function like the grid doesn't block them (closely resembling the first picture.)
The current xml I have is based on this MSDM which is very basic.
I do not know how to proceed. Do I have to use a different control panel/container or is there a setting to allowed them to extend passed the grid if the other button isn't above it (like zpanel?)
E: I couldn't find any other questions for this, so please link to any searches/posts with information on it.
Turns out I can use canvas and just do a bit more xml to keep the design the same. I would still like to know if it is possible to overlap them in any way for future use.

Change parent of a control in a size trigger in UWP

I'm creating a Windows 10 Universal app, there are two panels in my main page.
In tablet/desktop mode, both are visible at the same time (just two simple Grids, one fills the left side and the other one fills the right side)
However, in phone mode, there's a pivot and user can swipe between panels.
(I could use SplitView and hamburger menus which work easily in both Tablet/Desktop and Phone modes, but a pivot makes more sense in my case)
So, What I currently did is, I have two instances of each panel. PhonePanel1 and PhonePanel2 are two grids inside two pages of my pivot control, and TabletPanel1 and TabletPanel2 are two grids which can be shown side by side. and I use AdaptiveTriggers to detect page width and switch between these two view methods.
However, having two instances of each panel is not a good approach, because those are showing basically the same content.
Is there any better approach to do this? Maybe changing Grid's parent when triggers are changing view mode? or something better?
The only way to change the parent that I can think of is to write some code behind that will render them in the new location. A suitable solution may be to defer the loading of the controls until they are needed.
x:DeferLoadStrategy=”Lazy“
If you add this property to your controls that are duplicated, they will only be loaded when required. So on a phone the PC/Tablet view should never be loaded, unless the user is running in Continuum on an external display.
On a PC you may still end up with both loaded (If the user resizes the window) - On a PC there are usually more resources so it's less of a problem there in most cases.
I looked into the code behind approach to move the controls to a different parent and I think honestly it's premature to do that when it might not even improve performance.
I have gone for the Defer Load Strategy property and found performance to be fine, and it requires very little code. My duplicate controls are sharing the same ViewModel so I pretty much just added the property to ensure resources are only used when essential.
There is nothing wrong in your approach. To reduce memory usage, when adaptive trigger happens for example on the phone, set TabletPanel1 and TabletPanel2 visibility to collapsed.
If all panels use the same datacontext then leave it as is, if not set datacontext for collapsed controls to null.
You could create each of your panels as user controls then you've only need to define the contents once but each control could be included in each of the layouts you want.

Actioning a C# GUI from beneath

I have inherited a C# GUI that I need to make some changes to. The main thing I need to do is to action the view controllers to fill in forms and things automatically and without a user.
Being very new to C#, is it possible to initialise GUI elements and event handlers so that they are valid objects without actually displaying them on the screen? The end goal is to create a new API that effectively forms a command line style variant of what already exists.
I appreciate this question might come across a little confused, as I'm still dipping my toe in and trying to feel my way around the approach.
Being very new to C#, is it possible to initialise GUI elements and event handlers so that they are valid objects without actually displaying them on the screen?
Yes, you can set their visibility to false. And they won't be visible. Or alternatively you don't add them to any displayed control. If you go to designer.cs file of your form you will see this line.
this.Controls.Add
this is basically adding your control to the form. If you skip this line it won't be displayed.
If you are going to write a Command Line Interface for a GUI Application (not the usual approach), then how about instantiating and just not calling .Show() on it? You can control the individual elements from the .Controls -Property of the Form.

Optimizing of winforms

I have a program that loads a tab that has roughly 332 text boxes it loads. When loading a new tab there is a 2-3 second delay before the tab can be displayed. I have tried to suspend and resume and even looked into threading but found out that a winform can only use one thread.
I was wondering if there was a way to create the text boxes in another thread and then add them into the main form or a way to better create 332 text boxes.
thanks for your help.
Depending on your layout, I'd strongly recommend replacing the textboxes with a DataGridView, or otherwise loading the textboxes on scroll with some kind of continous-control .
The main bottle neck is the creation of handles. In winforms, each control gets its own handle (even labels), and when used in large numbers such as this, can indeed give performance problems.
If a datagridview is not an option, can you give some more information on the layout (a screenshot perhaps?), so the community can have a go at alternatives. Creating the textboxes in another thread will create problems when you try to add them, but you could create them only when needed.
Simple: don't use 300 text boxes. You can only use one text box at a time. The rest of the data you can just draw, and when the user wants to edit it you create the edit control for it. This is exactly how a grid works, so I recommend using a grid or similar control.
I also had this problem, and it goes further to affect initialisation code for controls on tabs that has not get activated before.
My workaround is to block the program with a "loading..." splash and programatically click the tabs so that the controls get loaded/activated. Once this is done, the tabpage shoud not take as long to display again.
Although I agree with others in that you may want to look into reducing the number of controls in a single tab.

What would be the best course of action for display different form in a single Winform?

Here's a screenshot of my application:
Basically, depending on what option is selected I'd like to show another 'content' which can be a buttons, or forms or whatever.
What would be the best choice for this? Using MDI? I'm really new to this type of thing.
This scenario lends itself well to tab pages, as you'd find on a TabControl.
However, since you already have a mechanism for switching between the content, you might prefer to create a series of Panels whose Dock property is set to DockStyle.Fill. When the user clicks the appropriate heading, you simply need to show the appropriate panel and call BringToFront() on it. This is essentially what the tab control does internally, anyway.
Don't forget to use SuspendLayout() and ResumeLayout() appropriately to reduce flicker, which can be a huge problem in WinForms applications, especially when there are lots of controls.
You can position a TabControl where the buttons are not visible and control it from your buttons.

Categories

Resources