Actioning a C# GUI from beneath - c#

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.

Related

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

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

Adding controls/usercontrols in Constructor/XAML VS adding controls in Loaded Event in WPF window

I was wondering if there is any difference between adding some controls/usercontrols (that take time to initialize ) in XAML or constructor and in Loaded event of some window.
I have a window where I use same usercontrol thrice. This is a usercontrol and takes some time to initialize itself. This window takes around 5 - 7 seconds to initialize itself with all three usercontrols initialized.
How should I approach adding them in window?
( Someone suggested that adding control after window is loaded will allow window to load in faster way than adding them in XAML/Constructor of the window.)
I would like to have some suggestion from experts on this topic as I don't know much about this.
Thanks.
There is nothing inherently wrong with deferring the loading of a window's content until after the window has been shown. The event you probably want is ContentRendered, which tells you the window's handle has been created, and the window shown.
I would suggest wrapping your window's content in a single "view", say a UserControl, such that you only need to set the window's Content and be done with it, as opposed to imperatively injecting multiple controls. It just makes things simpler. In essence, make your view a user control, and treat the window as the host of your view.
None of this is likely to reduce the time required to populate your window, but at least you'll be able to show something while the content is processed, even if it's just an empty window with a wait cursor.
Also, look for any places where you perform expensive operations on the UI therad (e.g., in your views or view models), and consider replacing those with asynchronous operations. You might find that you're able to load the UI faster than the underlying data, in which case you can display a more complete view with the appropriate wait/progress indicators until your data comes in.

Moving Controls from One Tab Page to Another

I currently am working on a WinForm project in which there are several different tabs. Within each tab there are various controls such as buttons, sub-tabs, text-boxes, ect...
I need to consolidate the overall application which involves taking certain controls from one tab and moving them to another. When I first tried doing so, I simply copy and pasted the controls. As you can imagine this didn't work due to the fact that I didn't move the properties with the controls, I really just created NEW ones on a different tab. Therefore when I complied the code, nothing worked because there was no code assigned to the new controls.
When I tried it again, this time I CUT and paste which also maintains the same properties as the old controls (specifically the reference name in the code), so as far as I can tell, the code should identify the controls by name, and apply the same actions. However, when I compile the code, the application successfully builds but the controls do not perform any actions.
At this point I am not sure what to do...
Use the Document Outline.
View... Other Windows... Document Outline.
Select the required component and drag it from one tab page to the other in the tree control. I did this and the actions are preserved in the new tab page.
Drag the item out of the tab control and onto the form itself. Change to the other tab. Then drag the item into that tab. It is essentially 2 drag moves, but since you do not ever cut, all code linking is maintained. If your tab control takes up the entire form, simply make it smaller while you do the preceding steps and then make it large again when you are done.
When you "cut" the controls, you sever the connections between the controls and their respective events. When you "paste" them again, they're not hooked up to the events anymore, so they don't appear to do anything.
The "event" methods should still be present in your code, but you'll have to manually go through and subscribe each event to each control again (via the Properties window).
Alternatively, revert those changes, then open the .Designer.cs file and look for something like this:
this.tabPage1.Controls.Add(this.dataGridView1);
Which (for example) places dataGridView1 inside tabPage1.
If you wanted to move the DataGridView to another TabPage, you could just change this.tabPage1 in the above code to this.tabPage2.
this.tabPage2.Controls.Add(this.dataGridView1);
Then you can flip back over to the designer view and move the control around to wherever you want it within the TabPage.
I just tested it. What is happening when you cut and paste your controls, you losing the wiring of the events. What you need to do after cut and paste is to go to control properties-events, find the event in question and on the right, select a method that you want to handle that event.
This will cut them from the first TabPage and paste them on the second, i think you can do this as often as you want. And with a small change you can make it a truly copy.
hope it helps
private void ControlsToTabPage(TabPage from, TabPage to)
{
Control[] ctrlArray = new Control[from.Controls.Count];
from.Controls.CopyTo(ctrlArray, 0);
to.Controls.AddRange(ctrlArray);
}

WPF C# Updating label on second window

Hi guys I have a problem
i need the info I enter in a text box on my main window to update a label on my second window called script.
The text box name is client and the label name is client-label
I have tried many different ways to do this and still not coming right
how can I do this?
PS. I am new to the programming world so please give me step by step instructions anywhere possible :)
Pertinent to your requirement (i.e. two WPF windows with sync controls), it will require quite a bit of coding. Better (simpler) way is to create a pseudo-window, i.e. just a nested layout Grid within you main window containing all controls pertinent to that second window (you can set its visibility to collapse/visible in order to "simulate" pop-up window), and update a second TextBlockon .TextChanged event of the first TextBox (using code behind). Alternatively, you can apply data binding technique is XAML of the same single window.
Note: you can implement a splitter control to resize two 'sub-windows'.
In case you do prefer to implement second window, then refer to this example: Data Binding between two TexBoxes in different windows
Rgds,
The "correct" way for this would probably be to have a view model for each view part in your gui and have them communicate through events.
The Prism framework for WPF will help you with most of the plumbing for this.
Se more about Prism here: http://compositewpf.codeplex.com

Removing control from panel doesn't remove it from the Form?

I have a routine where I loop recursively through all the controls on a form and process some code on some of them.
I add and remove controls through the use of the screen depending on selections the user makes.
I found that panel.Controls.Remove(control1) didn't actually remove it from the form. When I would run the routine that loops recursively through the controls on the form, the control I thought I had remove was still being found.
It didn't "disappear" until I did:
panel.Controls.Remove(control1);
this.Controls.Remove(control1)
Is this expected? Can someone explain this to me, and or point me to somewhere that explains control behavior in Windows Forms.
Thanks!
Clearly the control has the form as its Parent, not the panel. These kind of accidents tend to happen easily with the designer. You can use View + Other Windows + Document Layout to get a good view of the child-parent relationships. You can use drag+drop in this list to fix.

Categories

Resources