C# Write Listview, and Read Listview (how to add indicator for listview for subitem)

StackOverflow https://stackoverflow.com/questions/11623490

  •  22-06-2021
  •  | 
  •  

سؤال

I got this project going on for myself to keep some thing in order.

What I have is this:

+---Date---+----Time----+----Error-----------+
|24-24-2012|    4:42    | Warning - test/test|
+----------+------------+--------------------+

Now I got all that working, at least the reading & writing. but its not going as I was hoping it would.

I wanted these text to be like:

Date (items.add)
time (subitem)
error (subitem)

I want it to be written like:

date | time | error

and then once the program read it, he knows:

| = Start of subitem ( || end of subitem )

so what it would look like in the text file to be written to is:

date | time ||| error || ( which means : time start+end / error start+end (add to subitem)

I want the program visual studio to know that he knows that ( | 1x = subitems.add(" - And | 2x = ");

ugh quite hard to explain. I'll try less complicated.

| = subitems.add("<br>
|| = ");

so writing a file would look like :

writer.writeline( | + "hello" + || + | + "test" + || );

which will result in the text file as :

| hello ||| test ||

Once program boots up, it will read it again this way:

Listview.additems.add( hello ) *with no | or ||*

and the test will be :

listview.subitem( test ) *with no | or ||*

And so on...


Screenshots to explain it a little bit better for someone with autism like me.

Click here for screenshot

هل كانت مفيدة؟

المحلول

So adding the response as another answer. You're code will look something like this:

using(StreamReader reader = new StreamReader(logFileName))
{
  StringBuilder currentItem = new StringBuilder();
  char currentChar;
  while (true)
  {
    if (reader.Peek() == 'specialChar')
    {
      if (currentChar == '|')
      {
        // This indicates that the currentItem is subItem
        AddNewSubitem(currentItem.ToString());
      }
      else
      {
        AddNewItem(currentItem.ToString());
      }
      currentItem.Clear();
      reader.Read();
    }
    else if (reader.Peek() != null)
    {
      curentItem.Append(reader.Read());
    }
    else
    {
      break;
    }
  }
}

نصائح أخرى

I'm not sure I've fully understood what you need to achieve, but what I understood I think is that you want each item in the listbox to follow a specific template - so the main one will be in some X template, and the subitems will have a different template - a bit intended. So when addig - to the listbox depending on the item type you're adding set the

listboxitem.template = isSubitem?subItemTemplate:mainItemTemplate;

And that's all what I understood from your question. Hope it makes sense.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top