0
votes

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.

4
You can't have it both ways @chinz! Half the time the questions are short and meaningless, and now you're complaining there's too much information! It is a bit long.. but still.. :))Grim
@Grim, I dont mind reading a long question. but not all in one line perhaps? =]HengChin
Can you suggest a better way of asking this question with all the information in it @chinzCoder92
How are you filling the combobox?Pradeep Kumar
In the properties of the combobox in 'Items' I have copied and pasted a list of country codes.Coder92

4 Answers

0
votes

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"))
0
votes

How about that?

cbCountryCode.ClearSelection()

cbCountryCode.SelectedIndex = cbCountryCode.Items.IndexOf(cbCountryCode.Items.FindByText(datatable.Rows(0).Item(2).ToString))
0
votes

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!!

-1
votes

Try simple:

comboBox.Items.Add("Item 1");