Results 1 to 16 of 16

Thread: [RESOLVED] using ListView

  1. #1

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Resolved [RESOLVED] using ListView

    I have a simple conversion program (my "Learn C#" program).

    I have a 2 column listview. The user enter an amount in a text box (txtUnits). Tehy selecty a "convert from" unit of measure. The program then converts the value to various new units of measure and populates the listview.

    This is my code for one of the subroutines:

    this.listView2.BeginUpdate;
    listView2.Items.Add("");
    listView2.Items(0).subitems.add((singleparse(txtUnits.Text) * .4047).ToString("N5"));
    listView2.Items(0).subitems.add("Hectares");
    this.listView2.EndUpdate;

    This is almost verbatum from the VB.NET code. Can someone help me with the correct syntax? I can then change all my other routines to run properly.
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  2. #2
    Junior Member
    Join Date
    May 2004
    Posts
    26

    Re: using ListView

    Something like... (note: I'm writing this from memory in notepad =p)
    Code:
    listView2.Items.Add("");
    listView2.Items[0].SubItems.Add((Convert.ToInt32(txtUnits.Text) * 0.4047).ToString("N5"));
    listView2.Items[0].SubItems.Add("Hectares");

  3. #3

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: using ListView

    How about this:

    this.listView2.Clear();
    this.listView2.BeginUpdate;
    // create the subitems to add to the list
    double newvalue;
    newvalue = singleparse(txtUnits.Text) * .4047;
    // update the listview
    listView1.InsertItem(listView2.ListItems.Count, newvalue, 0, "Hectares");
    this.listView2.EndUpdate;

    ...add additional rows...
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: using ListView

    You shouldn't need to call BeginUpdate and EndUpdate unless you are going to add a bunch of items individually. Those are to prevent the control from redrawing every time you add an item in order to prevent flicker.
    You should also check the user input before you try to convert it. What if they enter a letter? You'll have some trouble multiplying "s" by .4047.

    Maybe something like this?
    Code:
                double result;
                if (double.TryParse(this.textBox1.Text, out result))
                {
                    ListViewItem itm = new ListViewItem("");
                    itm.SubItems.Add((result * .4047).ToString("N5"));
                    itm.SubItems.Add("Hectares");
                    this.listView2.Items.Add(itm);
                }
                else
                {
                    MessageBox.Show("Please enter a double.");
                }
    Read more here: http://msdn2.microsoft.com/en-us/lib....listview.aspx

  5. #5

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: using ListView

    I check for numeric values when they type something into the field.

    Your other code seems to work. However, I will be adding a bunch of rows to the listview so the begin & end update will be required I guess.
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  6. #6

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: using ListView

    I get this error when I include them (this.listView2.BeginUpdate:

    Error 1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  7. #7

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: using ListView

    Never mind. I found it. I forgot the (). old VB habits are hard to break :-)
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  8. #8
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: using ListView

    If you are going to add a number of items at one time you should use .AddRange instead of .Add.

    Look at this example here:
    http://msdn2.microsoft.com/en-us/lib...ms(VS.80).aspx

  9. #9

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: using ListView

    I ran the code. I gives me a blank listview.
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  10. #10
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: using ListView

    Quote Originally Posted by Pasvorto
    I ran the code. I gives me a blank listview.
    What code?

    If your referring to AddRange it should be something like this:
    Code:
                ListViewItem itm = new ListViewItem("Item1");
                itm.SubItems.Add((1 * .4047).ToString("N5"));
                itm.SubItems.Add("Hectares");
    
                ListViewItem itm2 = new ListViewItem("Item2");
                itm2.SubItems.Add((1 * .3256).ToString("N5"));
                itm2.SubItems.Add("SomeOtherMeasurement");
    
                ListViewItem itm3 = new ListViewItem("Item3");
                itm3.SubItems.Add((1 * 1.234).ToString("N5"));
                itm3.SubItems.Add("SomeOtherMeasurementAgain");
    
                this.listView2.Items.AddRange(new ListViewItem[] {itm, itm2, itm3});
    Last edited by nmadd; Jul 23rd, 2007 at 11:31 AM.

  11. #11

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: using ListView

    This is the current code. The listyview remains blank. I set it up as having 2 columns.

    this.listView2.Clear();
    this.listView2.BeginUpdate();
    // create the subitems to add to the list
    double newvalue;
    newvalue = double.Parse(txtUnits.Text) * .4047;
    ListViewItem itm = new ListViewItem("");
    itm.SubItems.Add(newvalue.ToString("N5"));
    itm.SubItems.Add("Hectares");
    this.listView2.Items.Add(itm);
    this.listView2.EndUpdate();
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  12. #12
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: using ListView

    As the documentation states, .Clear() removes all items AND columns from the control. You don't have any columns to put those values in to.

  13. #13

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: using ListView

    I'm getting confused here. I'm missing something. I step through the code and it navigates as I would expect.

    I added thsi to the combo box routine:

    private void cboFrom_SelectedIndexChanged(object sender, EventArgs e)
    {
    this.listView2.Clear();
    this.listView2.Columns.Add("Value", -2, HorizontalAlignment.Right);
    this.listView2.Columns.Add("Units", -2, HorizontalAlignment.Left);

    switch (cboFrom.Text)
    {
    case "Acres":
    FromAcres();
    break;
    case "Centimeter":

    This is what the FromAcres routine looks like:

    private void FromAcres()
    {
    this.listView2.BeginUpdate();
    // create the subitems to add to the list
    double newvalue;
    newvalue = double.Parse(txtUnits.Text) * .4047;
    ListViewItem itm = new ListViewItem("");
    itm.SubItems.Add(newvalue.ToString("N5"));
    itm.SubItems.Add("Hectares");
    this.listView2.Items.Add(itm);
    this.listView2.EndUpdate();
    }

    It executes all the lines, but nothing shows in the listview. It must be something simple. But it eludes me. I appreciate your patience.
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  14. #14
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: using ListView

    Quote Originally Posted by Pasvorto
    private void FromAcres()
    {
    this.listView2.BeginUpdate();
    // create the subitems to add to the list
    double newvalue;
    newvalue = double.Parse(txtUnits.Text) * .4047;
    ListViewItem itm = new ListViewItem("");
    itm.SubItems.Add(newvalue.ToString("N5"));
    itm.SubItems.Add("Hectares");
    this.listView2.Items.Add(itm);
    this.listView2.EndUpdate();
    }
    1) Is your ListView's view set to Detail?
    2) The red line is adding a blank value in the first column. Then you add the other values. The value is placed in the second column and the name is placed nowhere since you ran out of columns.


    Play around with it a little bit and see what you can come up with.
    Code:
            private void cboFrom_SelectedIndexChanged(object sender, EventArgs e)
            {
                this.listView2.Clear();
                this.listView2.View = View.Details;
                this.listView2.Columns.Add("Value ID");
                this.listView2.Columns.Add("Value", -2, HorizontalAlignment.Right);
                this.listView2.Columns.Add("Units", -2, HorizontalAlignment.Left);
    
                switch (cboFrom.Text)
                {
                    case "Acres":
                        FromAcres();
                        break;
                }
            }
    
            private void FromAcres()
            {
                this.listView2.BeginUpdate();
                // create the subitems to add to the list
                double newvalue;
                newvalue = double.Parse(txtUnits.Text) * .4047;
                ListViewItem itm = new ListViewItem("Value 1");
                itm.SubItems.Add(newvalue.ToString("N5"));
                itm.SubItems.Add("Hectares");
                this.listView2.Items.Add(itm);
                this.listView2.EndUpdate();
            }

  15. #15

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: using ListView

    I got it to work. I had to go back to the properties menu and set the view to grid and set the column headers.

    I think I can experiment with it now. Thanks for all your help.
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  16. #16
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [RESOLVED] using ListView

    Glad you got it all squared away. Good luck on your project.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width