如何在Combobox的下拉框内显示两例数据?

abcz 2003-10-21 12:38:23
我要把一个Combobox绑定到一个数据表上,但我要在它的下拉框里显示这个表的两列数据,请高手指教!谢谢!
...全文
139 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqwqq 2003-10-24
  • 打赏
  • 举报
回复
restr=myrow("class_id")tostring+myrow("class_name").tostring
combobox.item.add(restr)
良朋 2003-10-21
  • 打赏
  • 举报
回复
似乎有个属性column设为2就行了。
gzfzxf 2003-10-21
  • 打赏
  • 举报
回复
是呀,这是一个问题,像这样非得自已做控件了.
另外,在这里接个火.
combobox这个控件有什么办法让他显示为平面??????
njuhuangmy 2003-10-21
  • 打赏
  • 举报
回复
用.net 的 vc++ 的时候, 是在 combobox 的data 项里 写上你要放置的内容

用 分号 隔开
abcz 2003-10-21
  • 打赏
  • 举报
回复
谢谢大家,虽然我都不太懂!我本来现在给大家分的,可我想再看看别人还有别的高见吗,所以等等再给大家分吧!谢谢!
liq1979 2003-10-21
  • 打赏
  • 举报
回复
ArrayList list = new ArrayList()
SqlDataReader dr = new SqlDataReader(sql,conn);

while(dr.Read())
{
list.Add(dr[0].ToString() + " " + dr[1].ToString());
}
DropDownList1.DataSource = list;
DropDownList1.DataBind();
Montaque 2003-10-21
  • 打赏
  • 举报
回复
或者你在 SQL 语句中修改。

Select Field1 & '-' &Field2 as NewField1 ...From Table1
NoReady 2003-10-21
  • 打赏
  • 举报
回复
0001|中国
0002|英国
0003|美国
Surpass 2003-10-21
  • 打赏
  • 举报
回复
'显示的项数
Public Property MaxDropDownItems() As Integer
Get
MaxDropDownItems = Me.CBox.MaxDropDownItems
End Get
Set(ByVal Value As Integer)
Me.CBox.MaxDropDownItems = Value
End Set
End Property
'列数
Public Property ColumnCount() As Integer
Get
ColumnCount = CCount
End Get
Set(ByVal Value As Integer)
If Value < 1 Then
MsgBox("属性值无效!", 16, "错误")
Else
CCount = Value
End If
End Set
End Property

'绑定列
Public Property BoundColumn() As Integer
Get
BoundColumn = BColumn
End Get
Set(ByVal Value As Integer)
If Value < 1 Then
MsgBox("属性值无效!", 16, "错误")
Else
BColumn = Value
End If
End Set
End Property
'下拉按钮
Public Property DownButton() As Boolean
Get
DownButton = DButton
End Get
Set(ByVal Value As Boolean)
DButton = Value
If Value = True Then
Me.CBox.Width = Me.PBox.Width + 4
Else
Me.CBox.Width = Me.PBox.Width + 20
End If
End Set
End Property
'限于列表
Public Property LimitList() As Boolean
Get
LimitList = LList
End Get
Set(ByVal Value As Boolean)
LList = Value
End Set
End Property
Function ComboBoxItem() As Boolean
If LimitList = False Then
ComboBoxItem = True
Exit Function
End If

If Len(Trim(Me.CBox.Text)) = 0 Then
ComboBoxItem = True
Exit Function
End If
NewText()
Dim i As Integer
For i = 0 To CBox.Items.Count - 1
If UCase(Trim(Me.CBox.Text)) = UCase(Trim(GetString(CBox.Items.Item(i).ToString))) Then
ComboBoxItem = True
Exit For
Exit Function
End If
Next
If i = CBox.Items.Count Then
MsgBox("输入的值不在列表中。", , "提示")
'CBox.DroppedDown = True
ComboBoxItem = False
End If
End Function

Function GetString(ByVal str As String) As String
If Len(Trim(str)) = 0 Then Exit Function
If InStr(str, "|", CompareMethod.Text) = 0 Then
GetString = Trim(str)
Exit Function
End If
Dim a As Integer
For i = 1 To Len(str)
If GetChar(str, i) = "|" Then
a = a + 1
If a = BoundColumn - 1 Then
str = Mid(str, i + 1)
Exit For
End If
End If
Next
GetString = Mid(str, 1, InStr(str, "|", CompareMethod.Text) - 1)
End Function
Sub NewText()
If InStr(Me.CBox.Text, "|", CompareMethod.Text) > 0 Then
Me.CBox.Text = Trim(GetString(Me.CBox.Text))
End If
End Sub
Private Sub CBox_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBox.SelectedValueChanged
Dim T1 As Thread = New Thread(AddressOf NewText)
T1.Start()
End Sub

Private Sub CBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBox.Leave
Dim T1 As Thread = New Thread(AddressOf NewText)
T1.Start()
If ComboBoxItem() = False Then
Me.CBox.Focus()
Else
RaiseEvent LostFocus(sender, e)
End If
End Sub
'回车自动跳出
Private Sub CBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CBox.KeyPress
If e.KeyChar.GetHashCode = 851981 Then
If EnterAsTab = True Then SendKeys.SendWait("{TAB}")
Else
If BoundColumn = 1 Then
If Me.CBox.Items.Count > 0 Then Me.CBox.DroppedDown = True
End If
End If
End Sub

Private Sub CBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBox.Enter
If Len(Trim(CBox.Text)) = 0 Or (BoundColumn = 1 And Len(Trim(CBox.Text)) = 0) Then
If Me.CBox.Items.Count > 0 Then Me.CBox.DroppedDown = True
End If
End Sub

Private Sub CBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBox.Click
If Me.CBox.Items.Count > 0 Then Me.CBox.DroppedDown = True
NewText()
End Sub

Private Sub CBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBox.TextChanged
RaiseEvent TextChanged(sender, e)
End Sub

Private Sub CBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBox.LostFocus
If LimitList = False Then RaiseEvent LostFocus(sender, e)
End Sub

Private Sub CBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles CBox.GotFocus
RaiseEvent GotFocus(sender, e)
End Sub
End Class

我的msn: zhongnian_wei%msn.com
Surpass 2003-10-21
  • 打赏
  • 举报
回复
Private Sub TextBox_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
Me.Height = Me.CBox.Height - 3
End Sub

Public Overrides Property Text() As String
Get
Text = Me.CBox.Text
End Get
Set(ByVal Value As String)
Me.CBox.Text = Value
End Set
End Property
'背景颜色
Public Property CombBoxBackColor() As Color
Get
CombBoxBackColor = Me.CBox.BackColor
End Get
Set(ByVal Value As Color)
Me.CBox.BackColor = Value
End Set
End Property
'前景颜色
Public Property CombBoxForeColor() As Color
Get
CombBoxForeColor = Me.CBox.ForeColor
End Get
Set(ByVal Value As Color)
Me.CBox.ForeColor = Value
End Set
End Property

'是否可用
Public Property CombBoxEnabled() As Boolean
Get
CombBoxEnabled = Me.PBox.Enabled
End Get
Set(ByVal Value As Boolean)
If Value = False Then
Me.BackColor = System.Drawing.Color.LightSteelBlue
Else
Me.BackColor = System.Drawing.Color.CornflowerBlue
End If
Me.PBox.Enabled = Value
End Set
End Property
'回车 as Tab
Public Property EnterAsTab() As Boolean
Get
EnterAsTab = EnterTab
End Get
Set(ByVal Value As Boolean)
EnterTab = Value
End Set
End Property

'下拉列表行来源
Public Property RowSource() As String
Get
RowSource = RSource
End Get
Set(ByVal Value As String)
RSource = Trim(Value)
RS()
End Set
End Property

Sub RS()
On Error Resume Next
Me.CBox.Items.Clear()
If Len(RSource) = 0 Then Exit Sub

If Microsoft.VisualBasic.Right(RSource, 1) <> ";" Then RSource = RSource & ";"

Dim ColumnW() As String
ReDim ColumnW(StrCount(ColumnWidth, ",") - 1)
ColumnW = Split(ColumnWidth, ",")

Dim RValue() As String
ReDim RValue(StrCount(RSource, ";") - 1)
RValue = Split(RSource, ";")

For i = 0 To UBound(RValue) - 1
Dim CValue() As String
ReDim CValue(StrCount(RValue(i), "'") - 1)
CValue = Split(RValue(i), "'")

Dim txt As String = ""
For x = 0 To ColumnCount - 1
Dim tmpS As Integer
If x > UBound(ColumnW) - 1 Then
tmpS = 10
Else
tmpS = CInt(Mid(ColumnW(x), 1, Len(ColumnW(x)) - 2))
End If
If LenChar(CValue(0)) = 0 Then CValue(0) = "." 'StrDup(tmpS, Chr(32)) ' Space(tmpS)
Dim Ctxt As String = IIf(x > UBound(RValue), "", CValue(x))

Dim S As String
If LenChar(Ctxt) >= tmpS Then
S = ""
Else
S = Space(tmpS - LenChar(Ctxt))
End If
txt = txt & IIf(LenChar(Ctxt) <= tmpS, Ctxt, Mid(Ctxt, 1, tmpS)) & S & "|"
Next

Me.CBox.Items.Add(Trim(Mid(txt, 1, Len(txt) - 1)))
Next
End Sub
'下拉框宽度
Public Property DropDownWidth() As Integer
Get
DropDownWidth = Me.CBox.DropDownWidth
End Get
Set(ByVal Value As Integer)
Me.CBox.DropDownWidth = Value
End Set
End Property
'列宽度
Public Property ColumnWidth() As String
Get
ColumnWidth = CWidth
End Get
Set(ByVal Value As String)
Value = Replace(Value, " ", "")
For i = 1 To Len(Value)
Dim v As String = GetChar(Value, i)
If (v = "0" Or v = "1" Or v = "2" Or v = "3" Or v = "4" Or v = "5" Or v = "6" Or v = "7" Or v = "8" Or v = "9" Or v = "字" Or v = "节" Or v = ",") = False Then
MsgBox("属性值无效!", 16, "错误")
Exit Property
End If
Next
Dim ColumnW() As String
ReDim ColumnW(StrCount(Value, ",") - 1)
ColumnW = Split(Value, ",")
For i = 0 To UBound(ColumnW) - 1
If Len(ColumnW(i)) >= 3 Then
If Mid(ColumnW(i), Len(ColumnW(i)) - 1) <> "字节" Then
ColumnW(i) = ColumnW(i) & "字节"
End If
Else
ColumnW(i) = ColumnW(i) & "字节"
End If
Next
Value = ""
For i = 0 To UBound(ColumnW) - 1
Value = Value & ColumnW(i) & ","
Next
CWidth = Value
RS()
End Set
End Property
Surpass 2003-10-21
  • 打赏
  • 举报
回复
新建一个控件,清空全部代码,把我的代码Copy进去,再改一下。

Imports System.Threading
Public Class ComboBox
Inherits System.Windows.Forms.UserControl
Shadows Event TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Shadows Event LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
Shadows Event GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
Dim i As Integer, x As Integer
Dim EnterTab As Boolean = True
Dim RSource As String
Dim CCount As Integer = 1
Dim BColumn As Integer = 1
Dim CWidth As String
Dim LList As Boolean
Dim DButton As Boolean

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'UserControl 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents CBox As System.Windows.Forms.ComboBox
Friend WithEvents PBox As System.Windows.Forms.Panel
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.CBox = New System.Windows.Forms.ComboBox
Me.PBox = New System.Windows.Forms.Panel
Me.PBox.SuspendLayout()
Me.SuspendLayout()
'
'CBox
'
Me.CBox.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.CBox.BackColor = System.Drawing.Color.White
Me.CBox.ForeColor = System.Drawing.Color.Navy
Me.CBox.ItemHeight = 12
Me.CBox.Location = New System.Drawing.Point(-2, -2)
Me.CBox.Name = "CBox"
Me.CBox.Size = New System.Drawing.Size(106, 20)
Me.CBox.TabIndex = 1
'
'PBox
'
Me.PBox.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.PBox.Controls.Add(Me.CBox)
Me.PBox.Location = New System.Drawing.Point(1, 1)
Me.PBox.Name = "PBox"
Me.PBox.Size = New System.Drawing.Size(86, 15)
Me.PBox.TabIndex = 2
'
'ComboBox
'
Me.BackColor = System.Drawing.Color.CornflowerBlue
Me.Controls.Add(Me.PBox)
Me.Name = "ComboBox"
Me.Size = New System.Drawing.Size(88, 17)
Me.PBox.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub TextBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox_Resize(sender, e)
RS()
'addhandler CBox.SizeChanged
End Sub

NoReady 2003-10-21
  • 打赏
  • 举报
回复
那何时隐藏lsitview呢
2002pine 2003-10-21
  • 打赏
  • 举报
回复
只要加一个listview,注意它的位置,要把以前的listbox履盖。
Angelnet 2003-10-21
  • 打赏
  • 举报
回复
用第三方控件不就行了。
myvbnet 2003-10-21
  • 打赏
  • 举报
回复

TextBox + Button + ListView
meteorlg 2003-10-21
  • 打赏
  • 举报
回复
字符串相加

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧