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

combobox in vfp7

Status
Not open for further replies.

engan

Programmer
Jul 15, 2001
53
ID
Hi, Does anyone know to set combobox property ? I have a table with numeric datatype but on combobox I want to show it the content of the numeric field with words.
For example : 1 for Car
2 for bus
3 for airplane
so when it shows on the combobox it shows car,bus but when I save it, it will save 1 when I choose car, 2 when I choose bus.

Eng An
 
engan

You half to put both values in the comboxbox, but set the columnwidth of column2 to 0 and set the columnbound property to 2 (So the value shown in the combobox will be the column 1 and the return value will be column 2). Copy the following in a program and run it to see how its done.:
Code:
PUBLIC oform1
oform1=NEWOBJECT("form1")
oform1.Show
RETURN
DEFINE CLASS form1 AS form
	DoCreate = .T.
	Caption = "Form1"
	Name = "Form1"
	ADD OBJECT combo1 AS combobox WITH ;
		BoundColumn = 2, ;
		ColumnCount = 2, ;
		ColumnWidths = "80,0", ;
		RowSourceType = 6, ;
		RowSource = "listcombo.name,number", ;
		Height = 24, ;
		Left = 96, ;
		Top = 24, ;
		Width = 96, ;
		Name = "Combo1"
	PROCEDURE Load
		CREATE CURSOR listCombo (name c(10), number n(1))
		INSERT INTO listCombo (name,number) VALUES ("Car",1)
		INSERT INTO listCombo (name,number) VALUES ("Bus",2)
		INSERT INTO listCombo (name,number) VALUES ("Airplane",3)
		GO top
	ENDPROC
	PROCEDURE combo1.Valid
		MESSAGEBOX("The selected value is: "+TRANSFORM(this.Value))
	ENDPROC
ENDDEFINE


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Mike,
If you include a ControlSource, you'll need a BoundTo = .T., so you use the real value rather than the index value. You can see this if you change the data to ("Bus", 15) and add the bolded lines below:
DEFINE CLASS form1 AS form
DoCreate = .T.
Caption = "Form1"
Name = "Form1"
nCombovalue = 0
ADD OBJECT combo1 AS combobox WITH ;
BoundColumn = 2, ;
BoundTo = .F., ;
ColumnCount = 2, ;
ColumnWidths = "80,0", ;
ControlSource = "ThisForm.nCombovalue", ;
RowSourceType = 6, ;


Rick
 
rgbean

Always good to make slight adjustments as the situation arises. Thanks Rick.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Mike, what I have is this :
I have a table let's say Table A with a field numeric code.
code 1 for car
code 2 for bus
code 3 for airplane

so instead of creating cursor, I will get the data from my table A and it will show and highlight the correct definition. If the content of code is 1 it will highlight car, if the content of code is 3 it will highlight airplane. When I save the form it will automatically save 1 into my table A if I choose car. so I want to bind my combobox into my table A. I am still confuse with the Control Source. Should I use Control Source = tableA.code to bind it ?
Eng An

 
engan

The code I showed, explains that when the combobox show "car" for example, the return value in the valid event is "1", so when the user selects car, in the value you just need to send the combobox.value to the table.
I used a cursor as an example, but your can use a table.
Tio further explain, the rowsource of you combobox should be a small table (lookup table) that will create you list for your combobox. The controlsource of your combo should be the field of the from your main table that is to receive the value that is selected on your combo. Bugt apply Rick's suggestion in order to display the correct value in your combobox when it s not selected.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Mike, Ricks Thanks for your info, I just use second table to create a list and set my control source to my main table. now my combobox works the way I want.
Eng An


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top