Optimizing of winforms - c#

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.

Related

Too much data in C# windows form combobox, taking 30 sec to show the windows form

I am creating a windows form, that contains a simple combo box, which has around 100000 items. After loading all those items when I try to show the form useing showDialog() method it is taking 30 sec to show the form.
Is there any way that I can show it immediately ?
As pointed out by #jmcilhinney, it is not practical to load all this data to a combobox. Your system could (and it is) getting unresponsive on other situations.
There might be a lot of reasons for you have to do this, and since you are not disclosing everything, I can give a suggestion to answer your question - remember, not the good way, but would make things better for a while.
You might be binding your information on your load process. Separate it, make it async.
There are several ways of doing this, like making an async call to a internal method and set your ComboBox.DataSource or ComboBox.DataBind.
Also, on the end of your load/initialization of your window, set the cursor do hourglass/wait. Then set it back to the original one (might be the normal pointer). This would give the effect that the dialog is shown but it is yet loading. It is a lot better for the user showing that you are loading info.
But after that, try filtering data as you type, as suggested by #jmcilhinney. Even if it a fixed list, make it editable and filter data. Don't let the user see more than +1000. You should find a way to show the user that has too much thing and provide means to make your data filterable.
If you have data loading code for combobox in the load event, which I think you do. Move that code to a separate function and call that from a timer or thread. Or you can make the function async and then call that function from load event. This will not keep your form waiting till all the data is loaded in the combobox.

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

Display of records in datagrid takes time!

I have to display information in datagrid some information is about 1000 record from the database (sqlserver) but to display this information c# take unless 4 sec to do that ….
And how work with the application when he don’t see the result immediately he continue to press the button and the application repeat the same function many time due to that (sad) …
I make a break point the time is between the end of the function and the display of window or datagrid … are that normal !!!!
If yes there’s any solution to display a part and when he use the scroll the datagrid add another part (like facebook) ??
The best solution is to implement paging. There is an article at http://www.eggheadcafe.com/tutorials/aspnet/8a2ea78b-f1e3-45b4-93ef-32b2d802ae17/wpf-datagrid-custom-paging-and-sorting.aspx that covers it.
Regardless, make sure you disable the button until the grid has finished displaying, and show a wait cursor as well. A user should never have to wonder if the application is working or failing.
I've seen that happen too - when loading a large amount of data, the grid can take a few seconds to bind to the datasource, making the UI appear to freeze. You'll need to look up info on paging data in WPF, such as this link:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/f72b38f1-38d6-4894-ab86-9a2e5ab5cdac

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.

How to design the UI for settings according to different options in C# WinForm?

This is a .NET winform application question.
I come to this design from time to time. A simple form. On top of the form you have a list of options to select. According to different option chosen, the body of the form displays a unique panel of setting detail.
How to design this UI in Visual Studio neatly? I have two ideas. First idea is to have many panels, each panel contains setting controls. In runtime, hide all panels but the one according to the selection of option. In this solution, it is hard to organize the controls of the form in VS designer. You need to set the form big to hold all the panels. Panels are put one next to each other. There are many runtime loading code to write. For example, when loading the form, you need to hide panels, reset the form size. When you pick an option, you need to relocate the panels and show/hide them. Tedious!
Second idea is to use TabControl. TabControl is good because the tabs are well organized for you. You don't need to relocate panels and resize the form. All you need to do in runtime is to select the right tab according to options. One thing, you need to hide parts of the TabControl from user because after all it is not a real TabControl. Hiding the tab buttons of the TabControl is not hard but I find that after that there is always a big gap between the tab area and the following part on the form.
Dose anyone have a decent way of designing the UI? Maybe not using panels or TabControls but some smarter way? If TabControl is used most of the time, how to hide and show the tab parts of the TabControl and how to set the margin and border size of the TabControl so no big gap exists? Many thanks to any answer and suggestion.
When I need to do this, I put each group of controls in its own UserControl, and then I can use something else to switch between them. See, for example, Implementing a paged Options dialog on my blog.
I suggest you create UserControls for each of your "setting details" and when the user selects an option you load the accordant UserControl. You might have to adjust the forms size, but therefore you can easily manage all the "setting details" in your IDE.
Using user control is a good way to solve your problem. But you need set them probably in panels and play with properties "Visible" and "Dock".
You don't need to Dock them at "Fill" in design mode. You can set this property à runtime or when needed.
Hope this help.
Sounds like you need some design pattern.
Why not create a UI factory that returns your UI objects as needed/required?

Categories

Resources