Real Software Forums
http://forums.realsoftware.com/

inserting row on last row
http://forums.realsoftware.com/viewtopic.php?f=10&t=47608
Page 1 of 1

Author:  Rivo [ Tue Apr 16, 2013 1:32 am ]
Post subject:  inserting row on last row

Hi,

I want to add new row on last row in a listbox.

Listbox1.cell(Listbox1.LastIndex1,0)="Total"

this code is working, but its replacing the last row. What I need is just add new row on lastindex without replacing last row.
any helps.?


thanks

regards,
Rivo

Author:  msssltd [ Tue Apr 16, 2013 2:33 am ]
Post subject:  Re: inserting row on last row

Does this help?

ListBox1.AddRow "Col0"
ListBox1.Cell(ListBox1.LastIndex, 1) = "Col1"

Author:  Rivo [ Tue Apr 16, 2013 3:42 am ]
Post subject:  Re: inserting row on last row

Actually I want to add total on the last row every time the button was executed.
dim row as integer
dim dz as string
dim total as double
for row= 0 to ListBox1.ListCount-1
total = total + CDbl(ListBox1.Cell(row,5))
next
sDiskon.text=Format(total, "-###,##0")

if Listbox1.cell(Listbox1.LastIndex,0)<>"Total" then
ListBox1.AddRow "Total"
end if


this is work, but when I need to add new row, the Total should be removed and move to the lastindex again.

thanks
Rivo

Author:  lavalle [ Tue Apr 16, 2013 4:22 am ]
Post subject:  Re: inserting row on last row

Inserting a row ....

Why not simply

Listbox1.InsertRow(Listbox1.LastIndex-1,"")

Author:  Rivo [ Tue Apr 16, 2013 4:55 am ]
Post subject:  Re: inserting row on last row

Inserting row does not work.

the result should be displayed like


1 apple 10000
2 orange 20000
1 apple 10000
Total < this should printed on last row everytime the button is executed to add new row.


thanks

Regards,
Rivo

Author:  lavalle [ Tue Apr 16, 2013 5:37 am ]
Post subject:  Re: inserting row on last row

Supposed you have a list with 3 columns and 3 rows like

Listbox1.ColumnCount = 3
Listbox1.AddRow "1", "Apple","10000"
Listbox1.AddRow "2", "Orange","20000"
Listbox1.AddRow "#", "Total","30000"


In the Action-Event of the AddRow-Button:

dim i, x as Integer
dim sum As Double

x = Listbox1.ListCount // = 3
Listbox1.InsertRow(x-1,"3")

x = Listbox1.ListCount // now 4
Listbox1.Cell(x-2,1) = "Banana"
Listbox1.Cell(x-2,2) = "15000"

for i = 1 to x-1 // sum rows 1,2,3
sum = sum + CDbl(Listbox1.Cell(i-1,2))
next
Listbox1.Cell(x-1,2) = Str(sum) // put the sum in the Total-row 4

Author:  Rivo [ Thu Apr 18, 2013 1:37 am ]
Post subject:  Re: inserting row on last row

Yes,
This is worked nicely.
but what I need is, the same result with the different way.
I wanted to be like this code, but I cant use insert row method, Because I have written all code with add row method. Made some changes on it will take much more time for me.

Anyway, thanks for the helps.


Regards,
Rivo

Author:  kermit [ Thu Apr 18, 2013 3:29 am ]
Post subject:  Re: inserting row on last row

Quote:
I cant use insert row method, Because I have written all code with add row method.


Hmmm.
It is a useful programming and gambling skill to know when to hold, and know when to fold.

Anyway, if you must follow your current code, try this:

Your add button does this?:

//add a row
Mylist.addrow "Something"
//set some cells
Mylist.Cell(Mylist.lastindex,5) = 5000


Try this:

//add a row
Mylist.addrow "Something"
//set some cells
Mylist.Cell(Mylist.lastindex,5) = 5000

dim nLastindex as integer
dim nPreviousRow as integer
dim x as integer
dim nTotal as integer
//record the row numbers

nLastindex =Mylist.lastindex
nPreviousRow =nLastindex -1

//now move what you added up by one row, because it was a total
//(Im assuming you always have a total row as the last row..
//..if not, make sure you have one before this step)

for x = 0 to 5
Mylist.Cell(nPreviousRow,x) = Mylist.Cell(nLastindex ,x)
next

//now recalculate the total
for x = 0 to nPreviousRow
nTotal = nTotal + val(Mylist.Cell(x,5))
next

//finally, update the last row with the total
Mylist.Cell(nLastindex ,0) = "Total"
Mylist.Cell(nLastindex ,1) = str(nTotal)
Mylist.Cell(nLastindex ,2) = ""
Mylist.Cell(nLastindex ,3) = ""
Mylist.Cell(nLastindex ,4) = ""
Mylist.Cell(nLastindex ,5) = ""

Author:  Rivo [ Thu Apr 18, 2013 10:49 pm ]
Post subject:  Re: inserting row on last row

Cool..!

The same result ina different way.
Thanks alot Lavalle and kermit.
Its really save my time..


Regards,
Rivo

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/