【发布时间】:2016-08-12 08:44:43
【问题描述】:
我为 DataTable 中的组合框创建了多列下拉列表,现在我也想在其中显示两列。到目前为止,仅显示 1 列(带有 DisplayMember 属性)。所以基本上我想要自动完成,两列都显示在组合框中。 I would be satisfied with displaying 2nd column in Textbox next to combobox too, but It must work as Autocomplete (when selected index changes, display value changes too).我需要这个,因为两个 Datable 值(姓名和姓氏)将一起添加到另一个数据库表中,并且用户可以在同一个位置查看这两个值。
已编辑:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim SQL As String = "SELECT ID,Name ||' ' || Surname as FullName from MyTable"
Dim dtb As New DataTable()
dtb.Columns.Add("Name", System.Type.GetType("System.String"))
dtb.Columns.Add("Surname", System.Type.GetType("System.String"))
Using con As OracleConnection = New OracleConnection("Data Source=MyDB;User Id=Lucky;Password=MyPassword;")
Try
con.Open()
Using dad As New OracleDataAdapter(SQL, con)
dad.Fill(dtb)
End Using
Combobox1.DataSource = dtb
Combobox1.DisplayMember = "FullName"
Combobox1.ValueMember= "ID"
con.Close()
Catch ex As Exception
'MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try
End Using
End Sub
并在组合框列之间画线:
Private Sub Combobox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles Combobox1.DrawItem
e.DrawBackground()
Dim drv As DataRowView = CType(Combobox1.Items(e.Index), DataRowView)
Dim id As String = drv("Name").ToString()
Dim name As String = drv("Surname").ToString()
Dim r1 As Rectangle = e.Bounds
r1.Width = r1.Width / 2
Using sb As SolidBrush = New SolidBrush(e.ForeColor)
e.Graphics.DrawString(id, e.Font, sb, r1)
End Using
Using p As Pen = New Pen(Color.AliceBlue)
e.Graphics.DrawLine(p, r1.Right, 0, r1.Right, r1.Bottom)
End Using
Dim r2 As Rectangle = e.Bounds
r2.X = e.Bounds.Width / 2
r2.Width = r2.Width / 2
Using sb As SolidBrush = New SolidBrush(e.ForeColor)
e.Graphics.DrawString(name, e.Font, sb, r2)
End Using
End Sub
有什么建议吗?提前致谢!!
【问题讨论】:
-
我没用过,但昨天看到有人提到过codeproject.com/Articles/8619/…
-
我已经测试过了,它不能正常工作。除此之外,我的解决方案在这里运行良好,我只想为两列自动完成。我之前的帖子:stackoverflow.com/questions/38868261/…
-
哈哈,是的,是你发的。您是否尝试过查看他们如何尝试在他们的库中进行自动完成?
-
不,我没看过。我只是想为我的表单找到某种解决方案 - 用户需要输入已经在不同表中的姓名和姓氏(所以只需选择它们,然后用它自动填充字段或组合框),并将它们一起保存在不同的表中字段为字符串。
-
对我来说听起来不像是自动完成。所有正常的自动完成功能都是为您的数据源中匹配的内容提出建议,如果您点击选项卡,那么该建议将成为组合框的选定项。如果我提到的是你所追求的,你可以试试:stackoverflow.com/questions/11780558/…。它可能会给你一些东西。