PDA

Click to See Complete Forum and Search --> : [RESOLVED] using ListView


Pasvorto
Jul 23rd, 2007, 09:14 AM
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.

Outshynd
Jul 23rd, 2007, 09:39 AM
Something like... (note: I'm writing this from memory in notepad =p)
listView2.Items.Add("");
listView2.Items[0].SubItems.Add((Convert.ToInt32(txtUnits.Text) * 0.4047).ToString("N5"));
listView2.Items[0].SubItems.Add("Hectares");

Pasvorto
Jul 23rd, 2007, 09:40 AM
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...

nmadd
Jul 23rd, 2007, 09:57 AM
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?
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/library/system.windows.forms.listview.aspx

Pasvorto
Jul 23rd, 2007, 10:58 AM
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.

Pasvorto
Jul 23rd, 2007, 11:00 AM
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

Pasvorto
Jul 23rd, 2007, 11:01 AM
Never mind. I found it. I forgot the (). old VB habits are hard to break :-)

nmadd
Jul 23rd, 2007, 11:05 AM
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/library/system.windows.forms.listview.items(VS.80).aspx

Pasvorto
Jul 23rd, 2007, 11:13 AM
I ran the code. I gives me a blank listview.

nmadd
Jul 23rd, 2007, 11:27 AM
I ran the code. I gives me a blank listview.
What code?

If your referring to AddRange it should be something like this:
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});

Pasvorto
Jul 23rd, 2007, 11:32 AM
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();

nmadd
Jul 23rd, 2007, 11:36 AM
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.

Pasvorto
Jul 23rd, 2007, 11:55 AM
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.

nmadd
Jul 23rd, 2007, 12:12 PM
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.
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();
}

Pasvorto
Jul 23rd, 2007, 12:43 PM
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.

nmadd
Jul 23rd, 2007, 12:46 PM
Glad you got it all squared away. Good luck on your project. :)