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

编程入门 行业动态 更新时间:2024-10-23 05:38:18
本文介绍了在文本框中输入文本到组合框vb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个vb winform项目,其中包含一个带有国家/地区代码列表的组合框。在一种形式上,从该组合框中选择国家代码,并且可以将其保存到sql server数据库。在另一种形式上,文本框显示以第一种形式从组合框保存到数据库的国家/地区代码。如果错误(即更新/编辑信息),我想以第二种形式更改此国家/地区代码。我要做的是,在第二种形式上,当选择编辑时,隐藏带有国家代码的文本框,并显示包含所有国家/地区代码的组合框。我想在现在可见的组合框中的隐藏文本框中显示国家/地区代码,但此时显示组合框时它是空白的。我已经尝试将信息直接放入数据库中的文本框中,例如:

cbCountryCode.Text = CStr (dataTable.Rows( 0 )。项目( 2 ))

但这不起作用。我还需要将国家/地区代码保存在组合框中,因为如果错误,用户需要更改国家/地区代码。有办法做到这一点吗?我试过在谷歌搜索解决方案,但我找不到解决方案。我有一个解决方法,如果组合框是空白的,我不会让用户更新信息,但我希望国家代码已经存在,这样如果没有错误,用户就不必再次选择国家代码。任何有关此问题的帮助将不胜感激。 编辑: datatable.Rows(0) .Item(2) 持有国家代码,例如爱尔兰(+353),英国(+44)或美国(1)。这是我从数据库中调用信息的代码:

sqlVisitorDetails = SELECT * FROM visitorDetails WHERE idNumber = @ idNumber sqlCon.Open() sqlCmd = 新 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 )。项目( 3 )) txtCountryCode.Text = CStr (dtVisitorDetails.Rows( 0 )。项目( 4 )) txtAddress.Text = CStr (dtVisitorDetails.Rows( 0 )。Item( 5 ))

国家代码(例如'Ireland(+353)')存储在dtVisitorDetails.Rows(0).Item(4)中,并将其放入文本框txtCountryCode中。当在表单上单击编辑时,文本框txtCountryCode被隐藏,组合框cbCountryCode可见(在单击编辑之前显示txtCountryCode并隐藏cbCountryCode)。然后我想要国家代码(在这种情况下'爱尔兰(+353)')显示在cbCountryCode组合框中。在显示组合框时,它是空白的,用户必须再次选择国家代码,即使它是正确的。我希望这会让事情变得更清楚。

解决方案

请使用以下代码将项目添加到您的组合框

ComboBox1.Items.Add(TextBox1.Text)

不要设置组合框的文本属性,而是将项目添加到组合框中。

在我将组合框DropDownStyle设置为DropDownList的属性中,我将其更改为DropDown并使用

cbCountryCode.Text = CStr (dataTable) .Rows( 0 )。项目( 2 ))

并且有效。

I am developing a vb winform project that includes a combobox with a list of country codes. On one form a country code is selected from this combobox and can be saved to a sql server database. On another form, a textbox shows the country code that was saved to the database from the combobox in the first form. I want to change this country code in the second form if it is wrong (i.e. update/edit the info). What I am trying to do is this, on the second form, when 'edit' is selected, the textbox with the country code is hidden and a combobox with all the country codes is shown. I want to show the country code in the hidden textbox in the now visible combobox, but at the moment it is blank when the combobox is shown. I have tried putting the info into the combobox like the textboxes straight from the database, 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 tried searching for a solution on google, but I cannot find a solution to 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.

解决方案

Please use the below code to add item to your combo box

ComboBox1.Items.Add(TextBox1.Text)

Instead of setting the text property of the combo box kindly add the items to it.

In properties I had set the combobox DropDownStyle to DropDownList, I changed this to DropDown and used

cbCountryCode.Text = CStr(dataTable.Rows(0).Item(2))

and that worked.

更多推荐

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

本文发布于:2023-11-03 20:29:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   组合   框中输入   net   vb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!