how to use the display value of the Foxpro combobox - combobox

I have a combo box that I would like it to show a
category name ("Cat1" , "Cat2"...)
But when clicked or just displayed I would like to get the ID of this category. Any idea how to do this in Foxpro?

To populate your combobox:
Thisform.Combo1.AddItem("Cat1")
Thisform.Combo1.AddItem("Cat2")
You may use rowsource to add the entire item list into the combobox.
Get the selected value of combobox:
Thisform.Combo1.DisplayValue
Please refer to this article

Create a cursor that contains what you want to display in the first column and the ID in the second. Then, set up the combo like this:
RowSourceType = 2-Alias
RowSource = <your cursor name>
BoundColumn = 2
BoundTo = .T. && so you can have a numeric controlsource
Tamar

Related

Enter text from text box into combo box vb.net

I have two form, A and B.
On FORM A user will select a country code from combobox and it will then be saved to DB.
On FORM B a textbox shows the country code that was saved to the database earlier.
I want to change the country code in FORM B when edit is selected.
How to change:
1. the textbox will first be hidden
2. a combobox with all the country codes will be shown with selected value equals to the hidden textbox value.
I have tried putting the info into the combobox like the textboxes straight from the database when it is blank, e.g:
cbCountryCode.Text = CStr(dataTable.Rows(0).Item(2))
but this does not work. I also need to keep the country codes in the combobox as the user will need to change the country code if it's wrong. Is there a way of doing this? I have a work around where I don't let the user update the info if the combobox is blank, but I want the country code to be already there so that the user does not have to select a country code again if it is not wrong. Any help with this problem would be greatly appreciated.
EDIT:
datatable.Rows(0).Item(2)
holds a country code, for example, Ireland (+353), United Kingdom (+44) or U.S.A. (1).
This is the code I have for calling the information from the database:
sqlVisitorDetails = "SELECT * FROM visitorDetails WHERE idNumber=#idNumber"
sqlCon.Open()
sqlCmd = New SqlCommand(sqlVisitorDetails, sqlCon)
sqlCmd.Parameters.AddWithValue("#idNumber", txtIdNumber.Text)
dtVisitorDetails = loadDtVisitorDetails()
txtFirstName.Text = CStr(dtVisitorDetails.Rows(0).Item(1))
txtLastName.Text = CStr(dtVisitorDetails.Rows(0).Item(2))
txtContactNumber.Text = CStr(dtVisitorDetails.Rows(0).Item(3))
txtCountryCode.Text = CStr(dtVisitorDetails.Rows(0).Item(4))
txtAddress.Text = CStr(dtVisitorDetails.Rows(0).Item(5))
The country code (e.g. 'Ireland (+353)') is stored in dtVisitorDetails.Rows(0).Item(4) and this is put into the text box txtCountryCode.
When edit is clicked on the form, the text box txtCountryCode is hidden and the combobox cbCountryCode is visible (before edit is clicked txtCountryCode is shown and cbCountryCode is hidden). I then want the country code (in this case 'Ireland (+353)') to be shown in the cbCountryCode combo box. At the moment when the combobox is shown it is blank and the user has to choose a country code again, even if it's right. I hope this makes things clearer.
From the best i can understand from your question.
cbCountryCode.Text = CStr(dataTable.Rows(0).Item(2))
will not work if the DropDownStyle in the properties is set to DropDownList, change it to DropDown instead(if its not).
And to this:
I also need to keep the country codes in the combobox as the user will need to change the country code if it's wrong.
you have to bind the data to the ComboBox to make it work.
EDIT:
If possible, use ColumnName instead of Index for getting a data from datatable. Since you're selecting all record from your database, your may not know when then index would change(when a column is added or deleted from DB)
cbCountryCode.Text = CStr(dataTable.Rows(0).Item("CountryCodeColumn"))
How about that?
cbCountryCode.ClearSelection()
cbCountryCode.SelectedIndex = cbCountryCode.Items.IndexOf(cbCountryCode.Items.FindByText(datatable.Rows(0).Item(2).ToString))
Try this;
cbCountryCode.SelectedItem = CStr(dataTable.Rows(0).Item(2))
The .Text property is the currently selected text in the combo, not the currently selected item.
EDIT: (version 3!)
This definitely works! Create a new project, with a new WinForms project and Form1, etc. Add a button (btnTest) and a combobox (cboTest) and paste this code in;
Private Sub Form1_Load() Handles MyBase.Load
' Add some items
cboTest.Items.Add("U.S.A (+1)")
cboTest.Items.Add("Ireland (+353)")
cboTest.Items.Add("U.K. (+44)")
' Select the first item
cboTest.SelectedIndex = 0
End Sub
Private Sub btnTest_Click() Handles btnTest.Click
' Select the UK entry
cboTest.SelectedIndex = cboTest.FindString("U.K.")
End Sub
And I hate to have to say it, but it works like a charm! I've tested it with the combobox set to DropDown AND to DropDownList and the result is the same. Please check you have the right data going into and out of your routines from the dataTable/Row/Item you are using!!
Try simple:
comboBox.Items.Add("Item 1");

how to set selected item in ComboBox

This should be easy but Windows.Forms surprises me again:
var comboBox2 = new ComboBox();
comboBox2.Items.Insert(0,"Hoi");
comboBox2.Items.Insert(1,"Hoi");
comboBox2.Items.Insert(2,"Hoi");
comboBox2.SelectedIndex = 1;//I want to select the SECOND item
If I openup the combobox (by clicking on it with the mouse) the FIRST item is selected. This does not occur when the items have different texts. How can I select the second item on this combobox?
Use something like this,
comboBox2.Items.Insert(0,"Hoi");
comboBox2.Items.Insert(1,"Hui");
comboBox2.Items.Insert(2,"Hai");
and after this try giving
comboBox2.SelectedIndex = 1;
and also if you want select an item, always the index will starts from 0.
If you have a proper datasource set the following properties:
1. DataSource2. DisplayMember and 3. ValueMember
If you dont have the data source, try setting the following properties:
1. SelectedIndex and
2. Text

WPF - Setting the ComboBox Item Index and Item Value via LINQ Anonymous Types

I want to retrieve data from 2 columns of my database table and bind those 2 columns to the combobox item index and item value properties respectively. I retrieve my data from the samples table of my database with this anonymous query:
var result = from obj in context.Samples
select new { obj.ID , obj.Name };
I want to set the default index value of each item in my combobox to obj.ID and the value of each respective combobox item to obj.Name so that rather than the items in my combo box having default values starting from 0, 1, 2.... their index values will have the value of obj.ID returned by my LINQ query and the actual value of the item will be obj.Name.
Sorry if this is a silly/amateurish question but I've spent a few hours trying to fix it and had no luck. Thanks in advance.
The SelectedIndex property always goes from -1 (no item is selected) to count-1, you can't change that. If you want to store the ID and Name for each item in the ComboBox, you can do that:
Create non-anonymous type Sample that contains those two properties. Then assign (or bind) collection of Samples to the ComboBox's ItemSource and set DisplayMemberPath toName. Then you can access the ID of the selected item using ((Sample)yourComboBox.SelectedItem).ID.

ComboBox Input ,wpf

I have a ComboBox and its items source is an SQL table.
I would like that the user will choose the value he likes and if the value is not in the list, he would be able to type the value into the ComboBox. Is it possible?
Thanks.
You need to set the IsEditable property of the ComboBox to True. This will allow you to edit the contents as well as select any of the populated items in the combo box.

Get data into one form field based on another's field in access 2003

I am trying to update existing form field based on another combo box selection.
I have a DB with such relations: http://img696.imageshack.us/img696/7396/relationse.jpg
I have a such form: http://img233.imageshack.us/img233/9758/getdata.jpg
Which Offers to select only existing IDs in database.
All i want to do is when user selects (change) existing combo box "Filmas_ID" value, in nearby input box with value "Unbound" shows up data related to this ID (You know - Just for informative reasons).
(SELECT Filmas.Nosaukums FROM Filmas WHERE Filmas_ID = combo box "Filmas_ID" value; )
It looks very simple, but i had no success yet to get it work.
Thanks for your help! =)
There are a few handy ways to do this, one involves including the data in the combo. Let us say the combo is set up like so:
RowSource : SELECT Filmas_ID, Nosaukums FROM Filmas
ColumnCount : 2
You now have a choice of setting the column widths so that filmas_id is selected, but Nosaukums is shown in the combo. Alternatively, you can set the textbox to reference the combo column, like so:
= mycombo.column(1)
Where columns count from zero.
Another method is to use DlookUp in the control source of the textbox:
= DLookUp("Nosaukums","Filmas", "Filmas_ID = " & mycombo)
Where Filmas_ID is numeric and the required value is in the bound column of the combo.

Resources