简体   繁体   English

从文本框中输入文本到组合框vb.net

[英]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. 我已经使用组合框设置为DropDownDropDownList对其进行了测试,结果是相同的。 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.

相关问题 在vb.net的组合框中检索记录 - Retrieving record to a combo box in vb.net 我如何从 SQL Server 表列 Vb.NET 中进行文本框即时搜索 - How I Do Text Box Instant Search From SQL Server Table Column Vb.NET 根据从databse中选择的另一个组合框,在vb.net中填充组合框 - Populate a combobox with in vb.net based on a selection of another combo box from databse 使用VB.net将组合框中的ID插入SQL Server - Inserting ID from combo box into SQL Server using VB.net 具有格式的富文本框文本不会保存在VB.NET的SQL中 - Rich text box text with formatting does not save in SQL for VB.NET 如何在SQL中使用富文本框保存图像和带格式文本并在Crystal Report VB.NET上显示 - How to save images and formated text with rich text box in SQL and show on crystal report VB.NET 使用组合框作为过滤器,使用sql在vb.net中按时间显示图表 - Using combo-box as filter to show chart by time in vb.net using sql 在 C# 从组合框中选择乌尔都语文本不更新文本框或 Label 形式但与具有相同代码的英文文本配合使用 - in C# Selecting Urdu text from Combo box not updating Text box or Label in form but works well with English text with same code 从executenonquery到文本文件vb.net的sql查询结果 - sql query result from executenonquery to text file vb.net 如何根据 VB.Net 中的复选框显示 SQL Server 数据库中的数据? - How do I display data from a SQL Server database based on checked box in VB.Net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM