listView to display time on button click?

click me button makes listView display time?

Page 1 of 1

10 Replies - 4594 Views - Last Post: 01 October 2010 - 08:26 AM Rate Topic: -----

#1 XMEGA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 17-November 08

listView to display time on button click?

Posted 28 September 2010 - 07:10 PM

how do I make a button "Click Me" populate a simple listView with the current time? I did so for a listbox like so

           // add items to listbox
            listBox.Items.Add("Button clicked at " + DateTime.Now.ToLongTimeString());
            listBox.SelectedIndex = listBox.Items.Count - 1;



I know I must construct a ListViewItem object and add to its SubItems...I want to display
Line | Clicked at
1 | time clicked
2 | 5:30:59 PM
3 | time clicked ect...

This post has been edited by XMEGA: 28 September 2010 - 07:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: listView to display time on button click?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: listView to display time on button click?

Posted 28 September 2010 - 09:05 PM

Have you looked at the MSDN page for ListView? Have you experimented *at all* on this on your own? Sorry, but this just seems like a lazy question.

Don't get up.. here, let me google that for you.

<2 seconds later>

http://msdn.microsof...y/2fds7919.aspx
Was This Post Helpful? 0
  • +
  • -

#3 XMEGA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 17-November 08

Re: listView to display time on button click?

Posted 28 September 2010 - 09:15 PM

yes I did google came across this one which scared me a bit, I would like it to generate the numbers automatically when the button is clicked along with the current time...not me manually putting them in thats sort of easy to do.
http://msdn.microsof...w(v=VS.90).aspx

I basically want what the listbox(original post example above) is doing with another column called line with checkbox in it...using a listview. I tried a buch of methods and a foreach didn't work out.

This post has been edited by XMEGA: 28 September 2010 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: listView to display time on button click?

Posted 29 September 2010 - 02:03 AM

View PostXMEGA, on 28 September 2010 - 08:15 PM, said:

yes I did google came across this one which scared me a bit,
http://msdn.microsof...w(v=VS.90).aspx


Maybe it's time to switch your major to cooking then. Or flower arranging. I'm not sure how it could scare you. Are you afraid this code is going to damage your PC? Screw up your O.S.? Drop 50 points on your credit rating? It's code, not Freddy Kruger or Sara Palin. It makes a ListView. Did you trying making a new project and work with the scary example? Did you trying learning from it? Or did you just read it and run away? Grow a pair: The keyboard isn't going to take your fingers off if you make a typo or your code doesn't work.


Quote

I would like it to generate the numbers automatically
"It" being the ListView? You want the ListView to generate the line numbers automatically? I don't think they do that. You'll need to insert those when you make a new ListView.Item. It's not tough to do this dynamically. Get the ListView.Items.Count then add 1 to it. You could make a custom control that inherits from ListView. That way your line numbering method would become part of the control and it would auto-number for you when you add items.


Quote

I tried a buch of methods and a foreach didn't work out.
There is no "didn't work out" error in Visual Studio. And you haven't posted any of your attempts. So there is really nothing for anyone here to help you with. If you want to post your most successful (least un-sucessful) code we'll see what we can do to help you.
Was This Post Helpful? -1
  • +
  • -

#5 XMEGA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 17-November 08

Re: listView to display time on button click?

Posted 01 October 2010 - 07:26 AM

giving me error at "AddRange" at bottom don't know why also grindline not working either thats why its commented out

            listView.View = View.Details;

            listView.CheckBoxes = true;

            listView.FullRowSelect = true;
            //listView.GridLines = false;

            listView.Columns.Add("Line", 40, HorizontalAlignment.Left);
            listView.Columns.Add("Clicked at", 100, HorizontalAlignment.Left);

            ListViewItem item1 = new ListViewItem("1");
            item1.Checked = false;
            item1.SubItems.Add("1");
            item1.SubItems.Add(DateTime.Now.ToLongTimeString());

            ListViewItem item2 = new ListViewItem("2");
            item2.Checked = false;
            item2.SubItems.Add("2");
            item2.SubItems.Add(DateTime.Now.ToLongTimeString());

            listView.Items.AddRange(new ListViewItem[] { item1, item2 });


This post has been edited by XMEGA: 01 October 2010 - 07:27 AM

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: listView to display time on button click?

Posted 01 October 2010 - 07:38 AM

What is the actual error message? "an error" is a LOT vague.
Was This Post Helpful? 0
  • +
  • -

#7 XMEGA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 17-November 08

Re: listView to display time on button click?

Posted 01 October 2010 - 07:41 AM

View PosttlhIn, on 01 October 2010 - 06:38 AM, said:

What is the actual error message? "an error" is a LOT vague.

this, what reference should I add if I'm missing cause none examples I've looked at mentioned any

Error 1 'System.Windows.Forms.ListView.ListViewItemCollection' does not contain a definition for 'AddRange' and no extension method 'AddRange' accepting a first argument of type 'System.Windows.Forms.ListView.ListViewItemCollection' could be found (are you missing a using directive or an assembly reference?)
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: listView to display time on button click?

Posted 01 October 2010 - 08:15 AM

View PostXMEGA, on 01 October 2010 - 06:41 AM, said:

View PosttlhIn, on 01 October 2010 - 06:38 AM, said:

What is the actual error message? "an error" is a LOT vague.

this, what reference should I add if I'm missing cause none examples I've looked at mentioned any

Error 1 'System.Windows.Forms.ListView.ListViewItemCollection' does not contain a definition for 'AddRange' and no extension method 'AddRange' accepting a first argument of type 'System.Windows.Forms.ListView.ListViewItemCollection' could be found (are you missing a using directive or an assembly reference?)


As you are typing the line, does Intellisense show you that .AddRange is available to you like it does here?
Attached Image

Testing what you've done I did this
        void Test()
        {
            ListView bob = new ListView();
            bob.GridLines = false;
            ListViewItem one = new ListViewItem("One");
            ListViewItem two = new ListViewItem("Two");
            bob.Items.AddRange(new ListViewItem[]{one, two});
        }



Which compiles fine.
Was This Post Helpful? 0
  • +
  • -

#9 XMEGA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 17-November 08

Re: listView to display time on button click?

Posted 01 October 2010 - 08:17 AM

no .AddRange doesn't pop up just .Add does
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: listView to display time on button click?

Posted 01 October 2010 - 08:19 AM

The code you've provided doesn't show where you create the listView object. Can we see that, and your using statements at the top?
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: listView to display time on button click?

Posted 01 October 2010 - 08:26 AM

Attached Image

Really? You're going to repeatedly give me negative reputation hits WHILE I'm helping you with your problem?

Obviously trying to help you is not in my best interest here. I'll be unsubscribing from this thread now. I wish you the best of luck working this out. Here is the MSDN page for the ListView control:
http://msdn.microsof...737(VS.85).aspx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1