c# - fast ListBox data update - c#

I want to display in about 20 lines some text data. Every line can be longer than the size of the form is. In this case the rest of the line should be cut away.
I don't want to have any h or v scroll bar.
Since the ListBox control can deal with my requirements I tried to use it. Everything is working fine but if I have a heavy disk load it can happen that the control begins to flicker and I don't think this has to be. I am updating the data once a second and I always have less then 20 lines of text data. Seems that I'm using the ListBox wrong.
I have the possibility to prepare the data in an other thread as a string or a string array but in every case have to update the whole ListBox. Which technique is the best for filling the ListBox with data? Can I have two buffers which I can toggle to be used with the ListBox?
Hope there is a better solution...

Try calling SuspendLayout() for the ListBox before adding data to it and then call ResumeLayout() on the ListBox. You may lose that flicker a bit.

You could either create a databinding and then just update the binding, that should work better.
A cheap way would be to change the visibility of the listbox and make it invisible at the beginning and visible at the end. Cheap, but should work. ;)

Related

Better solution for 600+ elements in a scroll view in WP7

I have a scroll view that holds about 614 Grid Controls (it's used as a book index, with each grid points to certain place at the book), inside each grid about 4 textblocks showing information about that choice....
The content is static inside all the textblocks. The thing is, when loading all that content , the phone becomes quiet unresponsive for a while... it takes time to load that page and navigate to it from another pages.
I want another solution for all that items to be shown correctly and also each grid view of the 600 has it's own clicked event handler to be able to point it to the page in the book.
I read about some hard ways to do that, I was thinking maybe I can only load the index as a very "tall" image with the index written inside it and then detect where the user tapped and calculate the index page from that ? is that efficient? or maybe there's something else ?
What is happening is the scroll view is iterating through all 600 items to measure the height of each entry so that it knows how big to render the scrollbars.
It is better to use a ListBox in this case before WP7 will only render the visible items only. Even then, I've heard of performance issues when you hit 2000 rows.
If you are interested in how virtualization works, Samuel Jack has written one that scales well (albeit not for WP7), but he has detailed writeups on the decisions he made.
https://github.com/samueldjack/VirtualCollection/tree/master/VirtualCollection/VirtualCollection
See his write ups on:
Data Virtualization and Stealth Paging
Silverlights Virtual Collection
A Virtualizing Wrap Panel
Assume two observable collections A and B. Bind your collection A to your UI. Every time fill you collection B. Everytime whne UI is refreshed clear A. Once the UI is loaded, via an event trigger start filling of items from B -> A, as it is an Observable Collection and if you are using INotifyPropertyChanged correctly the items will start appearing on the UI one by one. (Lazy Loading). You may alter this approach according your implementation. I myself am following this approach. Hope it helps for you too.

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.

Adding a line to Textbox causes huge CPU load: how to optimize?

For some logging ouput, I have a Windows.Forms.Textbox which is updated via
TB.AppendText("some new text");
The method is hungry at redrawing the text field with >2000 lines inside, however it feels faster than with TB.text += 'some new text.
What should I use instead of Textbox to get quick inserts with low CPU load and fast refresh? I thought about Listbox but not sure if it will fit (however, DebugView's 'listbox' seems to be able to handle billions of lines without heavy load).
A ListBox or ListView would be much better suited for what you are proposing.
And if size becomes an issue, you can use ListView's VirtualMode to only load the items as needed, based on the user's scrolling.

Datagrid with large number of rows

In my WPF application, I've got a screen with a tab control. Five of these tabs contain datagrids which need to display a large number of rows (at least 5000). The tables are bound to ObservableCollections of Part objects. Each row displays around 20 points of part data. My problem is that, after the user enters the information they require and generate the data, clicking on a tab causes the application to hang for 30-60 seconds. After this the datagrid finally loads, and with the right virtualization settings, they perform at an acceptable rate (not exactly fast, but not too slow). If I disable virtualization, the program uses up way too much memory, and the loading time isn't really affected.
The most offensive tables consist of about half a dozen template columns. Each template contains controls inside a stackpanel or a grid; basically each row is split into two, like a double-row. This layout is a requirement, and paging is probably not something that the customer is willing to accept.
This is the most important screen in my application and I'm pretty much at a loss about making this work. Is there anything I can do to speed up this process? Perhaps ObservableCollection is the wrong choice?
Can you please provide more insights...
Can you check how much time is spent in "generating" the 5 collections of 5000 rows each? (this is what I assume you are saying)
With virtulaization "on" what is the UI loading time "after" we assign the collection to the items source?
What happen if you bind "ItemsSource" to the respective datagrid only when the tabItem is actually Visible \ Selected?
Do you datagrids have default sort member path? Grouping? Filter Paths?
These are a few things I would target to start on the issue.

Playing with buttons in C#.net using visual-studio

Well it's not playing actually.
I have a database with about 200 list of items in it. I've used DataTable to fetch all the data in single connection.
Then created a windows button that creates new button for all the items.
It is OK and I was able to do it easily.
But I stuck over two things..
First is, I have limited space in my windows form, that's why I want to load only 30 buttons at first and then upon second click event, I want to load buttons for remaining 30 items and so on..
Second problem is, even if i managed to solve the first problem? How to arrange them in proper row/column?
Please help.
Grab an ordered list of records, split it to a list of "pages" (which is also a list of records) and use navigation buttons to change the context of current page.
Why don't you take a DataGridView with a BindingSource and a DataGridViewButtonColumn? With this as a starting point you can simply glue them together by calling:
myDataGridView.DataSource = myBindingSource;
myBindingSource.DataSource = myDataTable;
Update
Surely you can try to do the whole visualization on yourself by using a TableLayoutControl. But the DataGridView is a control that is specialized to visualize data in a data grid (hence the name of it).
The grid view is a very complex control, but it has a lot of nice features which make your results looking more professional by simply configuring some properties of it. For example simply set the property AutoSizeColumnsMode to Fill to simply avoid horizontal scroll bars and set the Column.AutoSizeMode of some columns to e.g. DisplayedCells to enforce which columns should be wrapped, etc.
Also there are a lot of features regarding to data validation, formatting, etc. So i think even if the step-in hurdle is a little higher you got a much better visualization then trying to do all this stuff manually by taking a TableLayoutPanel. Last but not least there are lots of examples about how to use the specific properties within the MSDN and if you get really stuck just search for the problem here on SO or on the web and if you don't find a proper solution just ask a question here on SO.

Categories

Resources