Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populating listbox

Status
Not open for further replies.

thegame8311

Technical User
Jan 6, 2012
133
US
greetings

I can seem to populate my listbox. When I run my code nothing is showing in the listbox

here's what I have so far:

Code:
LPARAMETERS cItem

lp1.listbox1.RowSourceType = 3
lp1.listbox1.RowSource = "SELECT * FROM LivePlayers INTO CURSOR myCursor"

lp1.listbox1.value = myCursor.pid

I have values in my table, so what could I be missing?
 
When you change the contents of a listbox, you have to call the Listbox.Requery()

 
When you use Rowsourcetype = 3, the cursor (MyCursor in this case) is dummy cursor. The actual cursor that's used to populate the listbox is created behind the scenes.

So lp1.listbox1.value = myCursor.pid doesn't have any effect.

Either change the above line to lp1.listbox.ListIndex= 1. Or, better, do this:

Code:
SELECT * FROM LivePlayers INTO CURSOR myCursor
lp1.listbox1.RowSourceType = [b]2[/b]
lp1.listbox1.RowSource = "MyCursor"
lp1.listbox1.Listindex = 1

Also, if you want to see all the fields from LivePlayers, then you need to set the ColumnCount accordingly.

In fact, you could also do this:

Code:
lp1.listbox1.RowSourceType = [b]2[/b]
lp1.listbox1.RowSource = "LivePlayers"
lp1.listbox1.Listindex = 1

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
cool, well now that does not set any of the properties, but why, does it need to go into an event or somewhere else?
 
Mike's code can replace your code at the position it is right now.

The ideal place to put initialisation code is the Init() event of course. Data retreival can also go into the OpenTables() method of the forms dataenvironment or into the Load() of the form, previous to any init of any control. However you like it.

Bye, Olaf.
 
A little bit beyond the scope of this question:

Yes Mike, that's good from the OOP encapsulation perspective, of course.

But I rather see it from the perspective of application layers. And while it's good an object to be self contained, responsible for it's own state including data binding, it makes this control object work throughout many application layers, as it's part of the GUI and will then drill down to the database, which is a no go with seperating the concerns of presentation and persisting data to seperate modules.

Actually you can combine both the OOP thought and the thought on decoupling aspects in the N tier application layer model, by letting a control not directly access data in it's init, but still let it call an object representing the next layer of the app to delegate fetching the data for it.

That way the control would take care of missing data on it's own anyway and the data access layer would only query data the control really needs to display.

I still prefer the push of data into the GUI and not an intelligent GUI requesting what it wants to display, because having the presentation centrelised gives more overview to the programmer for interdependencies and the business layer can more easy be adopted to a whole new form in one central databinding method, perhaps.

Bye, Olaf.
 
ok, that worked, but now I am adding values from one table to another, but when I try to use the following the listbox does not refresh or requery

Code:
thisform.list1.refresh
thisform.list1.requery

I have those in the add button so after it adds it to the table it should show me the updated list, but it is not
 
would it help to put that same select code into the requery function?
 
Mike initially gave you 2 alternatives. Alternative 2 was just setting lp1.listbox1.RowSource = "LivePlayers" instead of a query into a cursor named "MyCursor".

If you do that the listbox requery() will show the new records.

Bye, Olaf.
 
it's ok I figured that part out.

The part that is getting me now is 'field' phrase is not found

Here is the code:
Code:
thisform.list1.RowSourceType = 2
thisform.list1.RowSource = "TestTeam"
thisform.list1.Listindex = 2 
thisform.list1.ColumnCount = 3
thisform.list1.ColumnWidths = "100,150,150"

it's pointing at my Rowsource line. the table exists, so I'm confused why I keep getting this error
 
Sepcifying a table not yet opened is not possible, unlike SQL-SELECT From opening a table a rowsource, even in rowsourcetype 2 can not only be a table name, but an expression with table and field names and thus vfp just errors this way instead of opening TestTeam.dbf

Bye, Olaf.
 
Just to clarify: With "can not only" I meant it's also possible the rowsource is an expression in general, it does not need to be a table or alias name, even if you specify rowsourcetype alias. So vfp does not know it should open that table, unlike with SQL-Select, where it actually does open not yet opened tables.

Bye, Olaf.
 
So if I insert a
Code:
Use "table"
before the rowsource code then it should work
 
Also I was wondering if you can do this:

Code:
thisform.list2.RowSourceType = 2
thisform.list2.RowSource = lnTeam
thisform.list2.Listindex = 2 
thisform.list2.ColumnCount = 3
thisform.list2.ColumnWidths = "100,150,150"

where lnTeam is a variable
 
Use "table"

No. Remove the double quotes. For example:

Code:
USE TestTeam

But you need to do that once only, before you call the code that sets the RowSource. For example, do it in the Load event of the form. (And you should only do it if the table is not already open.)

This is a better way:

Code:
IF NOT USED("TestTeam")
  USE TestTeam IN 0
ENDIF

thisform.list2.RowSource = lnTeam

What's lnTeam? Is it a variable that holds the name of the table? If so, that's fine.

The stuff with column count and column widths look OK too.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top