Do ListView columns have a max length they can display? - c#

I'm encountering a strange ListView problem and will do my best to explain it. I have a ListView with 4 columns, the last one being a message string of varying length. I have some functionality that will change the ListView item red if it contains certain keywords (fail, exception, ect).
I first noticed this issue when one item was red and I didn’t see any word in the column that would trigger the red coloring code. So, I had the Length of the incoming string prepending to the item and added a textbox that would display that column's text when selected. What I found was that the prepending length (actual length of incoming string) would be like 953, the extracted ListViewItem's Text length would be 960 (str length + prepended length info), but the text that would be in the text box's length was 253...
What's going on here? Its like all the text made it into the ListViewItem but it can't/won't show it all (and no, its not column width, I had it set to over 1000 in the above case).
Adding the ListViewItem and checking for error strings:
ListViewItem listItem = new ListViewItem(msg.Date);
// Add sub-items for Details view.
listItem.SubItems.Add(msg.Time);
listItem.SubItems.Add(msg.Thread);
listItem.SubItems.Add("L: " + msg.Message.Length + " " + msg.Message);
if (!msg.Message.Contains("FA_FAILCNT"))
{
if (msg.Message.Contains("fail", StringComparison.OrdinalIgnoreCase) ||
msg.Message.Contains("exception", StringComparison.OrdinalIgnoreCase) ||
msg.Message.Contains("db q", StringComparison.OrdinalIgnoreCase))
{
listItem.Font = new Font(listItem.Font, FontStyle.Bold);
listItem.ForeColor = Color.Red;
}
else
listItem.ForeColor = Color.Black;
}
Obviously its the last subitem thats giving me the issues (the one that gets msg.Message)
EDIT: Well crap, this explains it.... any ways around this?

You have already found the reason why not all of the text is displayed.
The best solution that I've found so far is to put the information in the tooltip so that the entire string is visible when the user hovers their mouse over the column - see Listview subitem text is not shown fully in the UI eventhough the length of the text is correct.
Another way I've seen this work is allowing users to copy the value of a cell. Although the displayed text is truncated, copying and pasting the cell value into another application allows you to view the full text.
I imagine that the only other "workaround" would involve writing your own control - alternatively I don't think the ListView control in WPF has this same limitation.

Related

How can I know the index of duplicated text from a selectedtext? [duplicate]

Ok, I'm trying to do something a little specific here. I want to get the location of the selected text in a textbox.
To elaborate- I can use location to select text. If I have a textBox1 I could do:
textBox1.SelectionStart = 1;
textBox1.SelectionLength = 4;
That would start at the second letter and select 4 letters.
What I want to do is the opposite: when the user selects text, I want to find out what the start is and what the length is (or what the start is and what the end is. Either will work).
I thought about just searching the string for the selectedtext (textBox1.SelectedText). The problem comes if it is a common word or a string that is used multiple times. For instance.
This is a cat. This is a cat. This is a cat.
If they select the second sentence, using SelectedText to search the string for that specific sentence does me no good. It could be either of the 3.
So, my question is: When the user clicks a button, how do I determine the exact elements that are selected by the user, so that I can later manipulate those specific elements? Important to note the later part- I likely will not only want to manipulate the text when the button is pressed. I will also want to manipulate it later, at a time when the text may no longer be highlighted. This means I'll want to store SOMETHING to tell me what specific parts of the sentence I'm dealing with. If that solution isn't viable, is there a solution you can think of where, in the above "this is a cat" example, the user could select the second sentence, hit a button, and then later I know which sentence was selected when he hit that button?
According to the documentation, SelectionStart and SelectionLength can be both set and read. Just use those.
You dont even need to know the position of selected text to manipulate them, to edit the text that you have selected in the text you can simple set the SelectedText property to the new edited value.
// if textBox1.text = "Hello World World"; with first "World" selected
textBox1.SelectedText = textBox1.SelectedText.Replace("World", "Raj");
// then it becomes "Hello Raj World"

How can I get the length of string for a page of screen?

Now, I'm trying to create a novel(boot) text reader by C# WPF.
I am facing on a problem.
I don't know how can I split a string(from a text file) as a page of screen. ( like a book page )
(a page of screen is implmented by a label UI control. )
Because the string has 'New Line \n' & 'Tab' character.
So I cannot count how many characters can be inserted into a label(a page of screen).
label.TextLength returns the numbers of characters which can insert into the label.
But, if the string has '\n', it makes a lot of blanks.
So I hope to know how can I count the length of string which can inserted into a label with '\n'.
Or, Please guide me how can I create this program easily by using another UI controls.
< Additional info >
Thanks for your opinion. I added how I use label control.
In default label UI control changed it's size by the string which is inserted the label.
However, In this program, I will fix the label control size. It's size will be fit in Form size.
So, I should know the width & height(lines) of the label.
Or I hope to know controls or libraries for my program.(Unfortunately, I can't found it yet.)
Thank you!
Your label can have AutoSize=true and MaximumSize=new Size(maximumPageWidth, 0);
This way your label will automatically word wrap when maximum width is reached. Every line will only increase the height of label.
Then you will need to add your text to this label line by line. Every time you add a line to this label, check if maximumPageHeight is reached or not.
If maximum height is reached then that's where your page ends. After this you need to add your next line to new page.
Edit: Above solution will work if you use System.Windows.Forms.Label
FlowDocument is the solution what I want!
Thank you to let me know that thing. - cicciorocca!!

c# listbox item size limit

When I add an item to a listbox in c#, if the string is > 4680 characters it displays as a blank line. I can still access the entire string from within the program.
Did I run up against a limit or am I doing something wrong?
The limit of text in a textbox is 64 KB worth of text. Here ya go.
displaying 4680+ characters for a single list item is not useful for the end user. You need to truncate the text or find another way to reference the text that is more user friendly.

ListView Icons not being removed. Display in another way?

I've got a ListView with several columns. The ListView's SmallImageList contains three icons; an up-arrow, a down-arrow and a folder icon. When one of the column-headers is clicked, the list is sorted by that value, and to illustrate this, an icon is inserted into the header showing the up or downarrow from its SmallImageList. This works fine.
My problem is that when clicking on a new column to sort by, the icon in the old header should dissapear, but it doesn't. Instead, a folder-icon is inserted. This happens each time a new header is clicked, so eventually all headers are filled with folder-icons.
Whenever a column is clicked, all the columns are iterated through and their ImageKey is set to an empty string, and later replaced by the ImageKey for the arrows, if it was the clicked column. The empty string apparantly loads the folder icon.
for (int i = 0; i < m_LVCases.Columns.Count; i++)
{
m_LVCases.Columns[i].ImageKey = "";
if (i == e.Column)
{
if (m_ListViewColumnSorter.Order == SortOrder.Ascending)
m_LVCases.Columns[i].ImageKey = SortOrder.Ascending.ToString();
else if (m_ListViewColumnSorter.Order == SortOrder.Descending)
m_LVCases.Columns[i].ImageKey = SortOrder.Descending.ToString();
}
}
I can't post screenshots yet, as I don't have enough reputation. Does anybody have a solution to completely remove an icon from a column-header, or do I have to do some magic to display this in another way?
--Edit-- This is a project that I took over, so I'm trying to piece things together. I found out that setting the ImageKey for the column to null, resulted in all the headers (except the one clicked) got the folder icon, which is very strange to me. I tried adding a new icon, with only transparent color on it. This did in fact clear all the icons, but it left a gap where the icon should have been. Is there a way to place the icon-allignment to the right instead, so it won't move the header text for every column?
There's a pretty clean example of this in the MSDN reference here
What you're doing is very different, but what might help you is to set the .visible property of the column sortimage to false for the other columns.
I fixed the problem myself. I was trying to place the icons to the right of the text instead, but by setting the column's TextAlign property to anything just after havning cleared the imageKey, the folder-icons stopped appearing.
So I removed the symptoms, but I still don't know what the disease was.

How to add large text to listview's(detailed) items?

I'm writing an analyzer,which shows the packets of a specific program.Some packets are very large and the listview shows only the first 15-20 characters :\
This is my code
string __str = String.Join(" ", data.Select(x => x.ToString("x2")).ToArray()); //covert the byte[](packet) to hex string
string __ascii = AsciiToString(data); //convert the byte[](packet) to ASCII
if (encrypted) FormMain.PFA(form => form.listViewAnalyzer.Items.Add("S<-C [ENCRYPTED] Blowfishkey = 0xFF"));
else FormMain.PFA(form => form.listViewAnalyzer.Items.Add("S<-C"));
ListViewItem item = new ListViewItem(__str); //create new item and place the packet as hex string
item.SubItems.Add(__ascii); //add the ascii variant as substring
FormMain.PFA(form => form.listViewAnalyzer.Items.Add(item)); //add the item
It must be a property that prohibits adding text with more than x lines,but I can't see it.
The listview will contain all the text, you just can't see it if it's too long or has multiple lines.
The way that Outlook and things like packet sniffers often work is that the listview is accompanied by a textbox or "preview" window. You could change your UI so that selecting the item in the listview displays the full details of the item in an outlook-style preview pane. Then you could have a large multiline textbox and anything else you wanted. I often do this by putting an object in the ListViewItem.Tag property, so that I can retrieve it in the UI and display in the preview when the ListView.SelectedIndexChanged event fires.
Alternatively, the preview could be on a dialog that pops up when you double-click. In fact, make the preview UI a UserControl, then you can do both!
listview shows only the first 15-20 characters :\
Maybe you need to make the column wider?
It must be a property that prohibits adding text with more than x lines,but I can't see it.
List view items don't wrap text, so technically they prohibit text with more than 1 line

Categories

Resources