List of notices, which control is best choice? - c#

I want to use a control to notice users what are happening and what've done. It's like a multiline textbox, each line contains a notice, such as:
Connecting to database...done
Current datetime:
Inserting data into database 10/1000...done
Inserting data into database 20/1000...done
...
Inserted data into database
Current datetime:
I tried textbox, but it seems to be too complicated to manipulate many lines.
Is there a better control to do this? If textbox was the best choice, so how can I do this?
Thanks in advance!

I would use a listbox. Set it to SelectionMode.None. Add to the items collection. If you have enough lines to scroll off the screen you will need to adjust the scrollbar to the bottom manually. Do so by setting the listbox TopIndex to the number of lines - 1.

I've seen people use ListBoxes and TextBoxes. I usually use RichTextBoxes because then I can easily apply some simple styles and colouring, but it's probably overkill.
I found this method in one of my "just-playing-around" projects:
private void writeToLog(String text, SolidColorBrush color)
{
TextRange tr = new TextRange(logTextBox.Document.ContentEnd, logTextBox.Document.ContentEnd);
tr.Text = text;
tr.ApplyPropertyValue(TextElement.ForegroundProperty, color);
}

Related

How to align the first column of listView to the right? [duplicate]

I have a Listview in detail mode with 3 columns. I want to set the text align for the headers to "center". This works for the last two columns but not for the first. If I want to change it to "center" and click on "center", the field keeps being set to "left". Can I change this using the properties or do I need to program this?
Thanks.
According to the documentation:
Due to a limitation in the underlying control, this property has no effect on the first column in the ListView control, which is always aligned to the left. To work around this limitation in .NET Framework version 2.0, you can handle the ListView.DrawColumnHeader event and paint the column header yourself.
Another alternative workaround is to not use the first column at all and hide it by setting its width to zero.
I have got a simple solution: Add a new (not needed) first column. Change the alignment of the second column (your real first column) to right or center (can now be done in the designer). In Form-Load-Event remove the first (temporary) column. Voila - the textalignent now should be correct.
I have testet this behavior under Windows 7, 8.1 and 10. It should work.
greetings from germany
I realize this is an old post, but I just found another workaround. If you insert whitespaces just before the header text you are inserting or changing in your code (I know it's rough and you need to calculate how many spaces gets your header centered, but it works!) then you can alter the position of the first column's (0) header text as follows:
ListView2.Columns(0).Text = " " & ListView1.SelectedItems(0).Text.ToString

How to add a Datagridview into a Scatterviewitem?

I am trying to display the contents of different tables dynamically using scatterview, is there an easy way to do that. Right now I have the results of a query in DatagridView and I want to add it to a ScatterViewItem. I tried directly assigning, however I must be doing something wrong here. Do I need to bind it to the xaml code?
dgv = QueryResult();
svi.Content = dgv.DataSource;
The answer for this question was already there, my mistake I did not search that well.
The last post in :
Can I programmatically add a row to a WPF datagrid?
Moderators please flag appropriately.
Thanks

How To Hide ListView ColumnHeader?

I am struggling to figure out the correct control to use for a list of predefined jobs in the included form. I currently have a ListBoxControl in the Predefined Job Name group that lists all of the predefined jobs for a marine service shop (i.e. oil change, tune up, etc...). Then, based on the item (i.e. job name) that is selected in my ListBox, I need to display the items that correspond to that job. For example, if oil change is the selected job I need to show 4 quarts oil, 1 oil filter, labor, etc...and so on.
Currently, when I load the form data I have a DAO that retrieves all of my jobs from the database using LINQ to SQL. Then I iterate over the results and put the job names into the ListBox. The problem that I am having is that there is no tag for ListBox items like there is for ListView items. So each time the user selects another item in the ListBox, I have to perform another LINQ query to get the job from the database again so that I can display its' corresponding items. If I could use a ListView and hide the column header I could set the entire job on the tag so that each time the user selects a new item I would have access to the details without having to make another call to the database. Is there a way that I can hide the column header of a ListView without hiding the entire column?
You can set the HeaderStyle member of the ListView to None.
listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
Checkout the ListView HeaderStyle property. It has the following options:
None
Nonclickable
Clickable
From MSDN:
The HeaderStyle property allows you to specify whether the column headers are visible or, if they are visible, whether they will function as clickable buttons. If the HeaderStyle property is set to ColumnHeaderStyle.None, the column headers are not displayed, although the items and subitems of the ListView control are still arranged in columns
You can also create simple object like ListItem which has two poperties: Text (string) and Tag (object). Then implement ListItem.ToString() and you can use these in the ListBox as well.
You can also check out Better ListView Express component, which is free and allows displaying items in Details view without columns. The advantage over ListBox and ListView is a native look and many extra features.
Easy way is using the ColumnWidthChanging event
private void listViewExtended1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.Cancel = true;
e.NewWidth = listViewExtended1.Columns[e.ColumnIndex].Width;
}
}
I found that if you know for a fact you are not displaying the headers it may be best to set the HeaderStyle property to None, as Rajesh mentions above.
When setting in the .CS when screen initially loads the headers are displayed until screen is fully rendered.

ASP.net/C# newbie looking for help editing column width dynamically when data is retrieved

I've been messing around with this and have yet to find a clean solution. At the moment, I have a asp:DataGrid control on the aspx page (I am open to changing this). When I click a button, I retrieve a DataSet from a database and use a DataTable from the DataSet to fill up my DataGrid.
If my asp:DataGrid is named "tableData" and my DataSet is named data the following implementation will fill my grid successfully:
protected void renderData(object sender, EventArgs e)
{
DataSet data = hc.getDataSet();
tableData.DataSource = data.Tables[0];
tableData.DataBind();
}
But my column widths are not as wide as desired. I would like to look at all the data in each column of data.Tables[0] and choose my tableData column's widths accordingly.
I have tried many ways of accomplishing this and failed but here are my two main ideas and the problems I've encountered:
Solution 1: Alter column width after the DataBind(). I would use some type of code along the lines of
tableData.Columns[i].ItemStyle.Width = longestField;
The problem with this solution is the DataBind() seems to not have taken place yet and I get some null pointer like exceptions. I could do it after, but how would I know when it is done? Even if there was some event like AfterDataBind I would prefer not to use it because if I would have to determine what table I was dealing with in that particular function.
Solution 2: Create my own databind method and use that instead
The problem with this solution is I can't seem to add rows to tableData myself (I would expect something like a tableData.NewRow() function but I can't seem to find one fitting my needs.
I have also tried using OnDataItemBound function, but I realized I have know idea how to relate the DataGridItemEventArgs e back to datagrid's column collection.
I would appreciate some insight. Thanks in advance guys.
Calculating column widths is a tricky business, and is prone to glitches. I would suggest setting fixed widths for columns with a limited amount of data, and let the grid use percentage widths for the other columns.
For example, let's say you have a list of addresses.
Zip code should be less than 10 digits, so around 80px should be sufficient
City is not as limited, but 200px will probably do the trick
Street address could be really short or really long, so use a percentage width
If you're dead-set on calculating the column widths, take a look at this article:
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q877q

How to refresh DataGridView cells style

I have this simple piece of code on a windows form containing said DataGridView (dgvStatsTable) :
public void LoadStatsTable(DataTable statsTable)
{
dgvStatsTable.DataSource = statsTable;
var smallFont = new Font(dgvStatsTable.Font.FontFamily, dgvStatsTable.Font.Size * 0.67f);
dgvStatsTable.Rows[0].Cells[0].Style.Font = smallFont;
dgvStatsTable.InvalidateCell(0, 0);
//dgvStatsTable.Invalidate();
dgvStatsTable.Refresh();
}
Once that function has been called, my DataGridView contains the correct data to see.
However, that style change that I wanted is not showing (first cell in top-right corner has to have smaller text).
Why?
Is it because the table is set to a DataSource rather than building rows and columns?
Thanks!
The Solution to the problem was to write a handler for the DataGridView.CellFormatting Event
found in this MSDN article in the Setting Styles Dynamically section.
Here is a very nice answer from MSDN network, it appears that in order to have greater control you will need to override some functions.
http://msdn.microsoft.com/en-us/library/7fb61s43(VS.80).aspx

Categories

Resources