[英]Enter text from text box into combo box vb.net
I have two form, A and B. 我有两种形式,A和B。
On FORM A
user will select a country code from combobox
and it will then be saved to DB. 在
FORM A
用户将从combobox
选择国家/地区代码,然后将其保存到DB。
On FORM B
a textbox
shows the country code that was saved to the database earlier. 在
FORM B
一个textbox
显示以前保存到数据库中的国家代码。
I want to change the country code in FORM B
when edit
is selected. 选择
edit
我想在FORM B
更改国家/地区代码。
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. 更改方法:1.首先将隐藏文本框2.将显示带有所有国家/地区代码的组合框,其选定值等于隐藏的文本框值。
I have tried putting the info into the combobox like the textboxes straight from the database when it is blank, eg: 我试过将信息像文本框一样直接从数据库放入空白时放入组合框,例如:
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 USA (1)
. datatable.Rows(0).Item(2)保留国家/地区代码,例如
Ireland (+353), United Kingdom (+44) or USA (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 (eg 'Ireland (+353)') is stored in dtVisitorDetails.Rows(0).Item(4) and this is put into the text box txtCountryCode. 国家/地区代码(例如“爱尔兰(+353)”)存储在dtVisitorDetails.Rows(0).Item(4)中,并将其放入文本框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).
在窗体上单击编辑时,将隐藏文本框txtCountryCode,并且组合框cbCountryCode是可见的(单击编辑之前,将显示txtCountryCode并隐藏cbCountryCode)。 I then want the country code (in this case 'Ireland (+353)') to be shown in the cbCountryCode combo box.
然后,我希望在cbCountryCode组合框中显示国家代码(在这种情况下为“爱尔兰(+353)”)。 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.
我希望这可以使事情变得更清楚。
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. .Text
属性是组合中当前选择的文本,而不是当前选择的项目。
EDIT: (version 3!) 编辑:(第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;
使用新的WinForms项目和Form1等创建一个新项目。添加一个按钮(btnTest)和一个组合框(cboTest)并将此代码粘贴到其中;
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. 我已经使用组合框设置为
DropDown
和DropDownList
对其进行了测试,结果是相同的。 Please check you have the right data going into and out of your routines from the dataTable/Row/Item you are using!! 请从正在使用的dataTable / Row / Item中检查您是否有正确的数据进入和退出例程!!
How about that? 那个怎么样?
cbCountryCode.ClearSelection()
cbCountryCode.SelectedIndex = cbCountryCode.Items.IndexOf(cbCountryCode.Items.FindByText(datatable.Rows(0).Item(2).ToString))
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). 如果属性中的
DropDownStyle
设置为DropDownList
,则将其更改为DropDown
(如果不是),则将不起作用。
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.
对此:
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. 您必须将数据绑定到ComboBox才能起作用。
EDIT: If possible, use ColumnName
instead of Index
for getting a data from datatable. 编辑:如果可能,请使用
ColumnName
而不是Index
来从数据表中获取数据。 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"))
简单尝试:
comboBox.Items.Add("Item 1");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.