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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preselecting Listbox values issue 3

Status
Not open for further replies.

TechGrrl21

Programmer
Dec 26, 2013
7
US
I am having an issue with a form preselecting listbox values. I have a form that has a grid with a button that pops up a different form on top with a listbox. In this listbox, the user may select records that are saved to dbf. If the user is editing their records I need the second form to open with the preselected values that were saved to the table. Now, I have looked through the forum and found that setting the focus to the listbox before selecting the listbox records should keep the listbox from losing the selected values. And this helps, but does not solve the issue. Most of the time, values are preselected without issues, but sometimes, the listbox initializes with no selections. I have commented out code that calls to anything but Load and the function that populates the preselection of the listbox. Still no help. I have tried moving the function to different parts of the form such as, the Init of the form, Init of the listbox, GotFocus of the listbox...I have tried changing the Tab order as well as disabling Tabbing to the listbox. Still no better. I have tried moving the SetFocus of the Listbox to different parts of the form, from the Activate and Init to before and after the the Preselect Function..
The listbox RowSource is set to a table and the RowSourceType is Alias. I have tried Field and Value as the RowSourceType but haven't had any luck there either. I have included my code below.

Code:
SELECT Table1


thisform.ListBox.RowSource = 'AliasedTable'
thisform.ListBox.RowSourceType = 2

IF !USED('Table2')
	thisform.ListBox.SetFocus()
	FOR ncnt = 1 TO thisform.ListBox.ListCount
		chkfield = PADL(ALLTRIM(STR(nCnt)),2,'0') + '_c'
		IF !EMPTY(Table1.fieldname&chkfield)
			lst = VAL(ALLTRIM(Table1.fieldname&chkfield))
			thisform.ListBox.Selected(lst) = .T.
		ENDIF
	ENDFOR

ELSE
	SELECT Table2
	SCAN
		thisform.ListBox.SetFocus()
		FOR nCnt = 1 TO THISFORM.ListBox.ListCount
			IF Table2.fieldname = THISFORM.ListBox.List(nCnt,1)
				thisform.ListBox.Selected(nCnt) = .T.
			ENDIF
		ENDFOR
	ENDSCAN
ENDIF

					
RETURN

I'm not discounting the idea that the class for the form may be causing issues, but I'm not catching anything from the debugger. I don't see it going through class code after it runs this function. Would any of you lovely people have any suggestions??
 
In the first branch, maybe
lst = VAL(ALLTRIM(Table1.fieldname&chkfield))
is not between 1 and thisform.ListBox.ListCount

Code:
PUBLIC ofrm
ofrm = CREATEOBJECT("MyForm")
ofrm.show()

DEFINE CLASS myform as Form
	ADD OBJECT lst as listbox WITH rowsourcetype=1, rowsource = "aa,bb"
	PROCEDURE init
		This.lst.setfocus
		This.lst.Selected[0] = .T. && no error
		This.lst.Selected[3] = .T. && no error, although ListCount = 2
	ENDPROC
ENDDEFINE

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
If I've understood your problem correctly, you are trying to mulit-select various items in the list. Is that right?

If so, I believe you can't do that if the rowsourcetype is an alias or table (2 or 6). You will have to use an array to populate the list, or alternatively use rowsourcetype 0 and build the list with AddItem().

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike Lewis said:
I believe you can't do that if the rowsourcetype is an alias
Interesting, I wasn't aware of this inconvenience. Thank you !
To solve the problem I added a timer.
Incidentally, I started a funny animation.

Code:
PUBLIC ofrm
ofrm = CREATEOBJECT("MyForm")
ofrm.show()

DEFINE CLASS myform as Form
	ADD OBJECT lst as listbox WITH multiselect =1, rowsourcetype=2, rowsource = "cc"
	ADD OBJECT tim as timer WITH interval = 100
	PROCEDURE load
		CREATE CURSOR cc (aa C(20),bb C(10))
		INSERT INTO cc VALUES ('Mama','Leone')
		INSERT INTO cc VALUES ('Mammy','blue')
		INSERT INTO cc VALUES ('Alba','tross')
		GO TOP
	ENDPROC
	PROCEDURE tim.timer
		ThisForm.lst.setfocus
		ThisForm.lst.Selected[1] = .T.
		ThisForm.lst.Selected[2] = .T.
		This.Enabled = .F.
	ENDPROC
****************************
* For fun, uncomment this
****************************
*!*		PROCEDURE paint
*!*			ThisForm.lst.setfocus
*!*			ThisForm.lst.Selected[1] = .T.
*!*			ThisForm.lst.Selected[2] = .T.
*!*		ENDPROC
ENDDEFINE

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Mike - that's not true. You can use MultiSelect with a number of RowSourceTypes, including 2 and 6.

Tamar
 
vgulielmus said:
In the first branch, maybe
lst = VAL(ALLTRIM(Table1.fieldname&chkfield))
is not between 1 and thisform.ListBox.ListCount

I believe the it is stepping through the list correctly. It is one of the first things I checked. I will double check to make sure. The focus seems to end on the last record that SHOULD be selected.

MikeLewis said:
If I've understood your problem correctly, you are trying to mulit-select various items in the list. Is that right?

If so, I believe you can't do that if the rowsourcetype is an alias or table (2 or 6). You will have to use an array to populate the list, or alternatively use rowsourcetype 0 and build the list with AddItem().

Mike

You are correct. I am trying to select multiple items in the list. I was not aware of this limitation but I will try building an array with the records I need and see if that makes any difference.

If the array does not sort out my issue, I do have a few ideas but I don't like them, heh
 
So, switching to using an array seems have to cleared up the problem. Thanks so much for your help!!
 
Tamar, you are right, I also use multiselect with alias or fields, but automatically selecting the records is not working out, at least not in this way.

TechGrrl, it's not easy to understand the listbox and its arrays, and why there is a List and a ListItem array, AddItem and AddListItem methods and how this all works together. It's worth reading the help on the listbox and its List and ListItem properties. Also look at the example given in the main listbox topic, it's demonstrating multiselect and how to retrieve selected items, and it's also using an array as rowsource. It's a good indicator this works easier.

I'd only store item indexes with cacution, though. When the sort order or number of items changes you might reselect false items via the indexes stored in your _c fields, in the database tables I'd always store table primary keys of selected items. That's done in a dedicated table for n:m relations.

Bye, Olaf.

 
TechGrrl21 said:
You are correct .... switching to using an array seems have to cleared up the problem.

Tamar said:
.. that's not true. You can use MultiSelect with a number of RowSourceTypes, including 2 and 6.

Let me clarify. You can multiselect interactively (by clicking on each item while pressing Ctrl). But you can't multiselect programmatically (by explicitly setting the Selected property to .T.).

At least, that's what my tests show. When I tried to run TechGrrl21's original code, only the last of the relevant items was selected. Giving the listbox as RowSourceType of 0 or 5 fixed the problem.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I'm not sure if this is a bug or something else.
1 Indeed, I cannot select programatically from Init, Activate, Gotfocus, and so on.
2 By adding a commandbutton and placing the code inside his click event, no problems occurred.
Code:
PUBLIC ofrm
ofrm = CREATEOBJECT("MyForm")
ofrm.show()
DEFINE CLASS myform as Form
	ADD OBJECT lst as listbox WITH multiselect =1, rowsourcetype=2, rowsource = "cc"
	ADD OBJECT cmd as commandbutton WITH left = 100
	PROCEDURE click
		MESSAGEBOX(ThisForm.lst.Selected[1])
		MESSAGEBOX(ThisForm.lst.Selected[2])
	ENDPROC
	PROCEDURE load
		CREATE CURSOR cc (aa C(20),bb C(10))
		INSERT INTO cc VALUES ('Mama','Leone')
		INSERT INTO cc VALUES ('Mammy','blue')
		INSERT INTO cc VALUES ('Alba','tross')
		GO TOP
	ENDPROC
	PROCEDURE cmd.click
		ThisForm.lst.setfocus
		ThisForm.lst.Selected[1] = .T.
		ThisForm.lst.Selected[2] = .T.
	ENDPROC
ENDDEFINE
3 By removing the command button and placing again the code inside the Init event, followed by a Messagebox, the selection failed.
4 By removing the command button and placing again the code inside the Init event, preceded by a Messagebox, the selection succeeded.
Code:
PUBLIC ofrm
ofrm = CREATEOBJECT("MyForm")
ofrm.show()
DEFINE CLASS myform as Form
	ADD OBJECT lst as listbox WITH multiselect =1, rowsourcetype=2, rowsource = "cc"
	ADD OBJECT tim as timer WITH interval = 100
	PROCEDURE click
		MESSAGEBOX(ThisForm.lst.Selected[1])
		MESSAGEBOX(ThisForm.lst.Selected[2])
	ENDPROC
	PROCEDURE init
		ThisForm.lst.setfocus
		MESSAGEBOX('Ok')
		ThisForm.lst.Selected[1] = .T.
		ThisForm.lst.Selected[2] = .T.
	ENDPROC
	PROCEDURE load
		CREATE CURSOR cc (aa C(20),bb C(10))
		INSERT INTO cc VALUES ('Mama','Leone')
		INSERT INTO cc VALUES ('Mammy','blue')
		INSERT INTO cc VALUES ('Alba','tross')
		GO TOP
	ENDPROC
ENDDEFINE
5 By adding a timer and placing the code inside it's timer event, the selection succeeded (the code is in my previous reply).
6 I suppose the paint event is responsible. By placing the code in the paint event, it's interesting to see what happens.

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
By adding a commandbutton and placing the code inside his click event, no problems occurred.

That's not what I'm seeing. I'm still seeing only the last item selected.

However, if I add a messagebox after selecting the items, I see all the selected items as expected, but only while the messagebox is active. So it does look like some sort of timing / modal issue.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top