当前位置:文档之家› VB自定义ListBox控件

VB自定义ListBox控件

VB自定义ListBox控件
VB自定义ListBox控件

VB6自定义ListBox控件

从测试图中可以看到自定义控件比系统自带的控件速度快58倍

Form 代码

Private Declare Function GetTickCount Lib "kernel32" () As Long

Dim Tk As Long

Dim Mtk As Long

'自定义控件添加数据

Private Sub Command1_Click()

Tk = GetTickCount

For i = 1 To 100000

xList1.AddItem "项目:" & i

Next i

xList1.DrawListBox

Mtk = GetTickCount

Label2.Caption = "添加10W行数据用时:" & Mtk - Tk & "毫秒"

End Sub

Private Sub Command2_Click()

xList1.RemoveItem xList1.ListIndex

xList1.DrawListBox

End Sub

Private Sub Command3_Click()

xList1.ItemFontColor(3) = 255

xList1.DrawListBox

End Sub

'自带列表添加数据

Private Sub Command4_Click()

Tk = GetTickCount

For i = 1 To 100000

List1.AddItem "项目:" & i

Next i

Mtk = GetTickCount

Label3.Caption = "添加10W行数据用时:" & Mtk - Tk & "毫秒" End Sub

Usercontrol自定义代码

Option Explicit

'VB绘制简单的列表控件

'作者扣:六五九三五四九五三来水美树

'添加工程组件Timer ,PictureBox (命名:SollBar),各属性设置如下'timer.Enabled 设为False

' .InterVal 设为1

'Sollbar.AutoRedraw = True

'Sollbar.BorderStyle = 0

'Sollbar.Appearance = 0

'Sollbar.Visible = False

'UserControl.AutoRedraw = True

'粘贴以下代码即可运行

Private Type POINTAPI

x As Long

y As Long

End Type

Private Type RECT

Left As Long

Top As Long

Right As Long

Bottom As Long

End Type

Private Type ListItems

Text As String

FontColor As Long

' Check As Boolean '

' Icon As StdPicture 此处可为每行添加图标,

End Type

Dim m_ListCount As Long '总行

Dim m_ListIndex As Long '当前选中行

Dim m_Grid As Boolean '线段

Dim m_Page As Integer '

Dim m_CurIndex As Long '当前置顶的行号

Dim m_ItemHeight As Integer '行高

Dim m_Stretch As Boolean '决定图片与窗口一样大小

Dim m_BorderColor As Long

Dim m_SelBackColor As Long

Dim m_pic As StdPicture

Dim m_SollbarValue As Long

Dim m_SollbarValueMax As Long

Dim sReg As Long '滑动区域

Dim m_Slid As Long '滑块

Dim m_List() As ListItems

Dim Tk As Long

Dim Mtk As Long

Dim ret As Long

Public Event MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) Public Event MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single) Public Event MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) Public Event Click()

Public Event DblClick()

Private Const ButHeight = 18

Private Const SLIDERMINHEIGHT = 10 '滑块最小高度

Private Const SOLLCOMMANDHEIGHT = &HFF '滚动条上下按钮的高度Private Const SLIDHEIGHTMIN = &HFF '滚动条最小滑块高度

Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long

Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long

Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpPoint As Long) As Long

Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long

Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long

Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long

Private Const ALTERNA TE = 1

Private Const WINDING = 2

Private Declare Function SetPolyFillMode Lib "gdi32" (ByVal hdc As Long, ByVal nPolyFillMode As Long) As Long

Private Declare Function CreateRectRgn Lib "gdi32" (ByVal x1 As Long, ByVal y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long

Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long Private Declare Function FrameRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long

Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long

Private Declare Function FloodFill Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long

Private Declare Function GetTickCount Lib "kernel32" () As Long

Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long Private Const DT_LEFT = &H0

Private Const DT_CENTER = &H1

Private Const DT_RIGHT = &H2

Private Const DT_SINGLELINE = &H20

Private Const DT_VCENTER = &H4

Private Const DT_WORDBREAK = &H10

Private Const DT_CALCRECT = &H400

Private Const DT_EDITCONTROL = &H2000

'添加行

Public Sub AddItem(ByVal Text As String)

If m_ListCount = 0 Then

ReDim Preserve m_List(0) As ListItems

Else

ReDim Preserve m_List(UBound(m_List) + 1) As ListItems

End If

m_ListCount = UBound(m_List) + 1

m_List(UBound(m_List)).Text = Text

m_List(UBound(m_List)).FontColor = UserControl.ForeColor

End Sub

'绘制矩形

Private Sub DrawRectEx(sRect As RECT, ByVal cColor As Long)

UserControl.Line (sRect.Left, sRect.Top)-(sRect.Right, sRect.Top), cColor

UserControl.Line (sRect.Right, sRect.Top)-(sRect.Right, sRect.Bottom + 15), cColor

UserControl.Line (sRect.Left, sRect.Bottom)-(sRect.Right, sRect.Bottom), cColor

UserControl.Line (sRect.Left, sRect.Top)-(sRect.Left, sRect.Bottom), cColor

End Sub

'取渐变色中间的值

Private Function BlendColors(ByVal lngColor1 As Long, _

ByVal lngColor2 As Long, _

ByVal bStyle As Boolean, _

Optional addValue As Long = 0) As Single

If bStyle = True Then

BlendColors = RGB(((lngColor1 And &HFF) + (lngColor2 And &HFF)) / 2 + addValue, _ (((lngColor1 \ &H100) And &HFF) + ((lngColor2 \ &H100) And &HFF)) / 2 + addValue, _

(((lngColor1 \ &H10000) And &HFF) + ((lngColor2 \ &H10000) And &HFF)) / 2 + addValue)

Else

BlendColors = RGB(((lngColor1 And &HFF) + (lngColor2 And &HFF)) / 2 - addValue, _ (((lngColor1 \ &H100) And &HFF) + ((lngColor2 \ &H100) And &HFF)) / 2 - addValue, _

(((lngColor1 \ &H10000) And &HFF) + ((lngColor2 \ &H10000) And &HFF)) / 2 - addValue)

End If

End Function

Private Sub DrawGradientColor(ByVal sColor As Long, _

ByVal eColor As Long, _

ByRef sRect As RECT, _

Optional Variable As Boolean = True, _

Optional sStyle As Integer = 0)

Dim sR As Single

Dim sG As Single

Dim sB As Single

Dim eR As Single

Dim eG As Single

Dim eB As Single

Dim cR As Single

Dim cG As Single

Dim cB As Single

Dim Z As Long

On Error GoTo Err_Net

sR = sColor Mod 256

sG = sColor \ 256 Mod 256

sB = sColor \ 256 \ 256

eR = eColor Mod 256

eG = eColor \ 256 Mod 256

eB = eColor \ 256 \ 256

If Variable = True Then '变亮

sR = sR * 1.2: sG = sG * 1.2: sB = sB * 1.2

eR = eR * 1.2: eG = eG * 1.2: eB = eB * 1.2

End If

If sStyle = 0 Then '用垂直方式填充

cR = (sR - eR) / sRect.Bottom

cG = (sG - eG) / sRect.Bottom

cB = (sB - eB) / sRect.Bottom

For Z = sRect.Top To sRect.Bottom - 1

UserControl.Line (sRect.Left, Z)-(sRect.Right, Z), RGB(eR + (Z * cR), eG + (Z * cG), eB + (Z * cB))

Next Z

Else

cR = (sR - eR) / sRect.Right

cG = (sG - eG) / sRect.Right

cB = (sB - eB) / sRect.Right

For Z = sRect.Left To sRect.Right - 1

UserControl.Line (Z, sRect.Top)-(Z, sRect.Bottom), RGB(eR + (Z * cR), eG + (Z * cG), eB + (Z * cB))

Next Z

End If

Exit Sub

Err_Net:

End Sub

Private Sub DrawGradientColorX(ByVal sColor As Long, _

ByVal eColor As Long, _

ByRef sRect As RECT, _

Optional Variable As Boolean = True, _

Optional cValue As Long = 0)

Dim sR As Single

Dim sG As Single

Dim sB As Single

Dim eR As Single

Dim eG As Single

Dim eB As Single

Dim cR As Single

Dim cG As Single

Dim cB As Single

Dim Z As Long

Dim tz As Long

sR = sColor Mod 256

sG = sColor \ 256 Mod 256

sB = sColor \ 256 \ 256

eR = eColor Mod 256

eG = eColor \ 256 Mod 256

eB = eColor \ 256 \ 256

If Variable = True Then '变亮

sR = sR * 1.2: sG = sG * 1.2: sB = sB * 1.2

eR = eR * 1.2: eG = eG * 1.2: eB = eB * 1.2

End If

cR = (sR - eR) / sRect.Bottom

cG = (sG - eG) / sRect.Bottom

cB = (sB - eB) / sRect.Bottom

If cValue = 1 Then

For Z = sRect.Left To sRect.Right - 1

If Z >= (sRect.Right / 2) Then Exit Sub

Sollbar.Line (Z, sRect.Top)-(Z, sRect.Bottom), RGB(eR + (Z * cR), eG + (Z * cG), eB + (Z * cB))

Next Z

Else

For Z = (sRect.Right / 2) To sRect.Left Step -1

tz = sRect.Right / 2

tz = tz + tz / 2

Sollbar.Line (sRect.Right / 2 + (sRect.Right / 2 - Z), sRect.Top)-(sRect.Right / 2 + (sRect.Right / 2 - Z), sRect.Bottom), RGB(eR + (tz * cR), eG + (tz * cG), eB + (tz * cB)) Next Z

End If

End Sub

'输出文字

Private Sub sDrawText(ByRef sRect As RECT, ByVal Text As String, Align As AlignmentConstants)

Select Case Align

Case vbLeftJustify

sRect.Left = sRect.Left + 1

sRect.Top = sRect.Top + 4

DrawText hdc, Text, -1, sRect, DT_LEFT Or DT_EDITCONTROL Or DT_WORDBREAK

Case vbRightJustify

sRect.Left = sRect.Left + 1

DrawText hdc, Text, -1, sRect, DT_LEFT Or DT_VCENTER Or DT_SINGLELINE

Case vbCenter

DrawText hdc, Text, -1, sRect, DT_CENTER Or DT_VCENTER Or DT_WORDBREAK

End Select

End Sub

'绘制滚动条按钮

Private Sub DrawPoly()

Dim Rgn As Long

Dim FillMode As Long

Dim Brush As Long

Dim Bom As Long

Dim Wid, Hei As Long

Wid = Sollbar.ScaleWidth / Screen.TwipsPerPixelX

Hei = Sollbar.ScaleHeight / Screen.TwipsPerPixelY

Dim Poyl1(2) As POINTAPI

Dim Poyl2(2) As POINTAPI

Poyl1(0).x = Wid / 2

Poyl1(0).y = ButHeight / 2 - 1

Poyl1(1).x = Wid / 2 + 5

Poyl1(1).y = ButHeight / 2 + 4

Poyl1(2).x = Wid / 2 - 5

Poyl1(2).y = ButHeight / 2 + 4

Poyl2(0).x = Wid / 2 - 4

Poyl2(0).y = (Hei - ButHeight) + ButHeight / 2 - 3

Poyl2(1).x = Wid / 2 + 4

Poyl2(1).y = (Hei - ButHeight) + ButHeight / 2 - 3

Poyl2(2).x = Wid / 2

Poyl2(2).y = (Hei - ButHeight) + ButHeight / 2 + 1

FillMode = SetPolyFillMode(Sollbar.hdc, ALTERNATE) Rgn = CreatePolygonRgn(Poyl2(0), 3, FillMode) Brush = CreateSolidBrush(255)

FillRgn Sollbar.hdc, Rgn, Brush

DeleteObject Rgn

DeleteObject Brush

Rgn = CreatePolygonRgn(Poyl1(0), 3, FillMode) Brush = CreateSolidBrush(255)

FillRgn Sollbar.hdc, Rgn, Brush

DeleteObject Rgn

DeleteObject Brush

End Sub

Private Sub DrawSollbar(ByVal SollBarMaxValue As Long, ByVal nv As Long)

Dim x As Single

Dim y As Single

Dim sRe As RECT

Sollbar.Cls

DrawPoly

sReg = Sollbar.ScaleHeight - SOLLCOMMANDHEIGHT * 2

m_Slid = sReg / (SollBarMaxValue + 1)

If m_Slid < SOLLCOMMANDHEIGHT Then

m_Slid = SOLLCOMMANDHEIGHT

End If

y = SOLLCOMMANDHEIGHT + (sReg - m_Slid) / SollBarMaxV alue * nv

sRe.Left = 0

sRe.Top = y

sRe.Right = 255

sRe.Bottom = y + m_Slid

DrawGradientColorX &H80FF80, &H80FF80, sRe, True, 1

DrawGradientColorX &H80FF80, &H80FF80, sRe, True, 2

Sollbar.Line (sRe.Left, sRe.Top)-(sRe.Right - 15, sRe.Top), m_BorderColor

Sollbar.Line (sRe.Left, sRe.Top)-(sRe.Left, sRe.Bottom), m_BorderColor

Sollbar.Line (sRe.Left, sRe.Bottom)-(sRe.Right - 15, sRe.Bottom), m_BorderColor

Sollbar.Line (sRe.Right - 15, sRe.Top)-(sRe.Right - 15, sRe.Bottom + 10), m_BorderColor

'For X = 1 To 255

' Sollbar.Line (X, Y)-(X, Y + m_Slid), 255

'Next X

End Sub

Private Function GetSollBarValue(ByVal y As Single, ByVal SlidHeight As Long, ByVal SollBarMaxValue As Long, ByRef SlidRect As RECT) As Long

On Error GoTo Err_Net

If y <= SlidRect.Top Then: GetSollBarValue = -1: Exit Function ' 向上滚动按钮点击If y >= (SlidRect.Bottom + SlidRect.Top) Then: GetSollBarValue = -2: Exit Function ' 向下

滚动按钮点击

GetSollBarValue = (y - SlidHeight) / (SlidRect.Bottom / SollBarMaxValue)

Exit Function

Err_Net:

GetSollBarValue = -1

End Function

Private Sub Sollbar_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

If Button = 1 Then

Tk = GetTickCount

Dim sRect As RECT

Dim nv As Integer

With sRect

.Top = 255

.Left = 0

.Right = 255

.Bottom = sReg

End With

ret = GetSollBarValue(y, m_Slid, m_SollbarValueMax, sRect)

If ret = -1 Then

Timer1.Enabled = True

ElseIf ret = -2 Then

Timer1.Enabled = True

Else

m_SollbarValue = ret

End If

DrawSollbar m_SollbarValueMax, m_SollbarValue

m_CurIndex = m_SollbarV alue

DrawListBox

End If

End Sub

Private Sub Sollbar_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single) If Button = 1 Then

Mtk = GetTickCount

If (Mtk - Tk) < 20 Then Exit Sub

Tk = GetTickCount

Sollbar_MouseDown Button, Shift, x, y

End If

End Sub

Private Sub Sollbar_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) Timer1.Enabled = False

End Sub

'通过鼠标X,Y获取当前选中的行与列

Private Function GetMousedwValue(ByVal y As Single, ByVal x As Single) As Long

Dim y1 As Long

Dim ix As Integer

Dim x1 As Long

On Error GoTo Err_Net

If m_ListCount = 0 Then: GetMousedwValue = -1: Exit Function

y1 = Int(y / m_ItemHeight) '

GetMousedwValue = y1 + m_CurIndex

Err_Net:

End Function

Private Sub UserControl_Click()

RaiseEvent Click

End Sub

Private Sub UserControl_DblClick()

RaiseEvent DblClick

End Sub

Private Sub UserControl_KeyPress(KeyAscii As Integer)

'Select Case KeyAscii

' Case 72

' If m_SollbarValue <= 0 Then Exit Sub

' m_SollbarValue = m_SollbarValue - 1

' Case 82

' If m_SollbarValue >= m_SollbarValueMax Then Exit Sub

' m_SollbarValue = m_SollbarValue + 1

'End Select

' DrawSollbar m_SollbarValueMax, m_SollbarValue

' m_CurIndex = m_SollbarV alue

' DrawListBox

End Sub

Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

If Button = 1 Then

m_ListIndex = GetMousedwValue(y, x)

If m_ListIndex > (m_ListCount - 1) Then

m_ListIndex = m_ListCount - 1

End If

DrawListBox

End If

RaiseEvent MouseDown(Button, Shift, x, y)

End Sub

Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)

RaiseEvent MouseMove(Button, Shift, x, y)

End Sub

Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) RaiseEvent MouseUp(Button, Shift, x, y)

End Sub

'绘制ListBox 核心代码

Public Sub DrawListBox()

Dim sBack As RECT

Dim selRect As RECT

Dim i As Integer

Dim cut As Long

Dim m_Count As Long

m_Page = UserControl.ScaleHeight / m_ItemHeight

UserControl.Height = m_Page * m_ItemHeight

UserControl.Cls

'有背景就画背景

If Not m_pic Is Nothing Then

If m_Stretch = False Then

Set UserControl.Picture = m_pic

Else

UserControl.PaintPicture m_pic, 0, 0, UserControl.ScaleWidth, UserControl.ScaleHeight

End If

End If

'绘制网格’

If m_Grid = True Then

For i = 1 To m_Page

UserControl.Line (16, i * m_ItemHeight)-(UserControl.Width - 16, i * m_ItemHeight), m_BorderColor

'BlendColors(m_BorderColor, m_BorderColor, True, 1)

Next i

End If

'绘制边框

With sBack

.Top = 0

.Left = 0

.Right = UserControl.ScaleWidth - Screen.TwipsPerPixelX

.Bottom = UserControl.ScaleHeight - Screen.TwipsPerPixelY

End With

DrawRectEx sBack, m_BorderColor 'm_BorColor

If m_ListCount = 0 Then Exit Sub

'绘制列表

If m_ListCount <= m_Page Then

For cut = 0 To m_ListCount - 1

If cut = m_ListIndex Then '绘制选中行

selRect.Left = 16

selRect.Top = cut * m_ItemHeight - m_CurIndex * m_ItemHeight

selRect.Right = UserControl.ScaleWidth - 32

selRect.Bottom = selRect.Top + m_ItemHeight

DrawGradientColor m_SelBackColor, m_SelBackColor, selRect, False, 1 End If

sBack.Left = 1 '绘制行文字

sBack.Top = (cut * m_ItemHeight) / Screen.TwipsPerPixelY

sBack.Right = UserControl.ScaleWidth - 16

sBack.Bottom = sBack.Top + m_ItemHeight / Screen.TwipsPerPixelY

UserControl.ForeColor = m_List(cut).FontColor

sDrawText sBack, m_List(cut).Text, vbLeftJustify

Next cut

Else

Sollbar.Visible = True ' 加载滚动条

Sollbar.BackColor = &HE0E0E0

Sollbar.Top = 16

Sollbar.Height = UserControl.ScaleHeight - 32

Sollbar.Left = UserControl.ScaleWidth - Sollbar.Width - 16

m_SollbarValueMax = m_ListCount - m_Page

DrawSollbar m_SollbarValueMax, m_SollbarValue

If (m_CurIndex + m_Page) > m_ListCount Then

m_Count = m_ListCount

Else

m_Count = m_CurIndex + m_Page

End If

For cut = m_CurIndex To m_Count - 1

If cut = m_ListIndex Then

selRect.Left = 16

selRect.Top = cut * m_ItemHeight - m_CurIndex * m_ItemHeight

selRect.Right = UserControl.ScaleWidth - 32

selRect.Bottom = selRect.Top + m_ItemHeight

DrawGradientColor m_SelBackColor, m_SelBackColor, selRect, False, 1

End If

sBack.Left = 1

sBack.Top = (cut * m_ItemHeight) / Screen.TwipsPerPixelY - m_CurIndex * (m_ItemHeight / Screen.TwipsPerPixelY)

sBack.Right = UserControl.ScaleWidth - 16

sBack.Bottom = sBack.Top + m_ItemHeight / Screen.TwipsPerPixelY

UserControl.ForeColor = m_List(cut).FontColor

sDrawText sBack, m_List(cut).Text, vbLeftJustify

Next cut

End If

End Sub

'行高

Public Property Get ItemHeight() As Integer

ItemHeight = m_ItemHeight

End Property

Public Property Let ItemHeight(ByVal vNewValue As Integer) If vNewValue < 255 Then

vNewValue = 255

End If

m_ItemHeight = vNewValue

PropertyChanged "ItemHeight"

DrawListBox

End Property

Public Property Get Grid() As Boolean

Grid = m_Grid

End Property

Public Property Let Grid(ByVal vNewValue As Boolean)

m_Grid = vNewValue

PropertyChanged "Grid"

DrawListBox

End Property

Public Property Get ListIndex() As Long

ListIndex = m_ListIndex

End Property

Public Property Let ListIndex(ByVal vNewValue As Long)

m_ListIndex = vNewValue

PropertyChanged "ListIndex"

DrawListBox

End Property

Public Property Get ListCount() As Long

ListCount = m_ListCount

End Property

Public Property Get CurIndex() As Long

CurIndex = m_CurIndex

End Property

'边框颜色

Public Property Get BorderColor() As OLE_COLOR

BorderColor = m_BorderColor

End Property

Public Property Let BorderColor(ByVal vNewValue As OLE_COLOR)

m_BorderColor = vNewValue

PropertyChanged "BorderColor"

DrawListBox

End Property

'背景

Public Property Get BackColor() As OLE_COLOR

BackColor = UserControl.BackColor

End Property

Public Property Let BackColor(ByVal vNewValue As OLE_COLOR) UserControl.BackColor = vNewValue

PropertyChanged "BackColor"

DrawListBox

End Property

'List数据

Public Property Get List(ByVal Index As Long) As String

List = m_List(Index).Text

End Property

Public Property Let List(ByVal Index As Long, ByVal vNewValue As String) m_List(Index).Text = vNewValue

PropertyChanged "List"

'DrawListBox 为了提高速度,这段不建议在里面使用,End Property

Public Property Get ItemFontColor(ByVal Index As Long) As Long ItemFontColor = m_List(Index).FontColor

End Property

Public Property Let ItemFontColor(ByVal Index As Long, ByVal vNewValue As Long) m_List(Index).FontColor = vNewValue

PropertyChanged "ItemFontColor"

'DrawListBox 为了提高速度,这段不建议在里面使用,End Property

Public Property Get ForeColor() As OLE_COLOR

ForeColor = UserControl.ForeColor

End Property

Public Property Let ForeColor(ByVal vNewValue As OLE_COLOR) UserControl.ForeColor = vNewValue

PropertyChanged "ForeColor"

End Property

Public Property Get SelColor() As OLE_COLOR

SelColor = m_SelBackColor

End Property

Public Property Let SelColor(ByVal vNewValue As OLE_COLOR)

m_SelBackColor = vNewValue

PropertyChanged "SelColor"

End Property

Public Property Get Stretch() As Boolean

Stretch = m_Stretch

End Property

Public Property Let Stretch(ByVal vNewValue As Boolean)

m_Stretch = vNewValue

PropertyChanged "Stretch"

DrawListBox

End Property

'图片

Public Property Get Picture() As StdPicture

Set Picture = m_pic

End Property

Public Property Let Picture(ByVal vNewValue As StdPicture)

'

End Property

Public Property Set Picture(ByVal vNewValue As StdPicture)

Set m_pic = vNewValue

PropertyChanged "Picture"

DrawListBox

End Property

Private Sub Timer1_Timer()

If ret = -1 Then

If m_SollbarValue <= 0 Then Exit Sub

m_SollbarV alue = m_SollbarValue - 1

ElseIf ret = -2 Then

If m_SollbarValue >= m_SollbarValueMax Then Exit Sub m_SollbarV alue = m_SollbarValue + 1

End If

DrawSollbar m_SollbarValueMax, m_SollbarValue

m_CurIndex = m_SollbarV alue

DrawListBox

End Sub

Private Sub UserControl_Initialize()

m_BorderColor = &H8000000A

UserControl.BackColor = vbWhite

UserControl.ForeColor = 0

m_SelBackColor = &HFFC0C0

m_Stretch = False

Set m_pic = Nothing

m_ItemHeight = 255

m_ListCount = 0

m_ListIndex = -1

m_SollbarValue = 0

m_SollbarValueMax = 0

m_CurIndex = 0

m_Grid = True

Erase m_List

End Sub

Public Sub Clear()

m_ListCount = 0

m_ListIndex = -1

m_SollbarValue = 0

m_SollbarValueMax = 0

m_CurIndex = 0

Sollbar.Visible = False

Erase m_List

End Sub

Public Sub RemoveItem(ByVal Index As Long)

Dim cut As Long

If Index = -1 Then Exit Sub

If m_ListCount = 1 Then: Clear: Exit Sub

If Index = UBound(m_List) Then

ReDim Preserve m_List(UBound(m_List) - 1) As ListItems

m_ListCount = UBound(m_List) + 1

m_ListIndex = -1

Exit Sub

End If

For cut = 0 To UBound(m_List)

If cut > Index Then

m_List(cut - 1) = m_List(cut)

Else

m_List(cut) = m_List(cut)

End If

Next cut

ReDim Preserve m_List(UBound(m_List) - 1) As ListItems

m_ListCount = UBound(m_List) + 1

m_ListIndex = -1

End Sub

Private Sub UserControl_Resize()

DrawListBox

End Sub

Private Sub UserControl_Show()

DrawListBox

End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)

m_ItemHeight = PropBag.ReadProperty("ItemHeight", m_ItemHeight)

m_Grid = PropBag.ReadProperty("Grid", m_Grid)

m_ListIndex = PropBag.ReadProperty("ListIndex", m_ListIndex)

m_BorderColor = PropBag.ReadProperty("BorderColor", m_BorderColor)

VFP中的列表框控件(listbox)

VFP中的列表框控件(listbox) 一.列表框(listbox):主要用于选择一组指定的数据,用户从列表中选取选项,然后执行所需的操作. 二.列表框常用属性:见下表 注1(rowsourcetype属性可指定的值): 0-无,运行时使用列表框的确良additem和addlistitem方法加入 1-值,将列表框的内容在设计时直接写在该属性中 2-表别名:由columncount确定表中选择的字段.当用户选择列表框时,记录指针将自动移到该记录上 3-sql语句:见sql部分,由执行的结果产生. 4-查询文件名:见查询部分,由查询结果产生 5-数组名 6-字段名表:可用表别名作为字段前缀.当用户选择列表项时,记录指针将自动移到该记录上7-文件名描述框架,可包含"*"和"?"来描述在列表框中显示的文件名 8-结构

9-弹出式菜单,提供向后兼容. 二.列表框常用的方法:见下表 三.列表框常用事件:列表框的常用事件为click(单击)事件和dbclick(双击)事件. 四.例:列表框练习 1.新建表单,添加一个文本框text1,三个命令按钮command1~command3,三个命令按钮的caption属性依次设为"加入","移出"和"全部移出",一个列表框list1.界面如图25 2.设置属性:将表单的caption属性设为"列表框练习",autocenter属性设为.T.;将列表框list1的moverbars属性设为.T.,multiselect属性设为.T. 3.编写代码: ●"加入"命令按钮command1的click事件: qm=thisform.text1.value IF !empty(qm) no=.t. FOR i=1 to thisform.list1.listcount IF thisform.list1.list(i)=qm &&如果文本框中输入的内容和列表框中已存在的内容相同,则不添加 no=.f. ENDIF NEXT i IF no thisform.list1.additem(qm) thisform.refresh

关于WPF的listbox控件绑定问题

关于WPF的listbox控件 首先关于listbox以及datagrid 人们比较喜欢用datagrid来绑定数据,应为它的文本生成出来比listbox要整齐,相对来说listbox生成的文本可以用四个字来形容----不堪入目!下面我用2个控件做出的效果: 发现没?Listbox也能使之对齐。。详解如下: 前台绑定:XAML 其中Name;Gends;Age;Hobby 分别为实例类的属性(也是表的属性); 再看后台很简洁;

ListBox控件

ListBox控件 1.功能 ListBox控件显示较长的选项列表,用户可从中选择一项或多项。如果项总数超出可以显示的项数,则自动向ListBox控件添加滚动条。ListBox控件列表中的每个元素称为项。图1所示为ListBox控件。 图1 ListBox控件 2.属性 ListBox控件常用属性及说明如表1所示。 表1 ListBox控件常用属性及说明 下面对比较重要的属性进行详细介绍。 (1)Items属性。该属性用于查看列表框中的项。 语法: public ObjectCollection Items { get; } 属性值:ListBox.ObjectCollection表示ListBox中的项。 说明: ① 该属性使用户可以获取对当前存储在 ListBox 中的项列表的引用。通过此引用,可以在集合中添加项、移除项和获得项的计数。

② 可以使用DataSource属性来操控ListBox的项。如果使用DataSource属性向ListBox 添加项,则可以使用Items属性查看ListBox中的项,但不能使用 ListBox.ObjectCollection的方法向该列表添加项或从中移除项。 (2)SelectedItem属性。该属性表示当前选中的项。 语法: public Object SelectedItem { get; set; } 属性值:表示控件中当前选定内容的对象。 说明:对于标准 ListBox,可以使用此属性确定在ListBox中选定了哪个项。如果 ListBox 的SelectionMode属性设置为SelectionMode.MultiSimple或 SelectionMode.MultiExtended(它指示多重选择ListBox),并在该列表中选定了多个项,则此属性可返回任何选定的项。 示例 把左边的文本框中的内容移到右边的文本框中 本示例主要使用Items属性向ListBox1控件添加项,然后使用SelectedItem属性将ListBox1控件中的选中项添加到ListBox2控件中。示例运行结果如图2和图3所示。 图2 ListBox1中项移动前 图3 ListBox1中项移动后 程序主要代码如下: SqlConnection con = new SqlConnection("server=ZHY\\zhy;uid=sa;pwd=;database=student"); con.Open(); SqlCommand com = new SqlCommand("select * from student",con); SqlDataReader dr = com.ExecuteReader();

VB中ListBox的应用

https://www.doczj.com/doc/cf5629117.html,入门——ListBox控件的使用 【字体大小:小中大】2008-02-18 19:56 来源:作者: ListBox(列表框)控件可以显示一组项目的列表,用户可以根据需要从中选择一个或多个选项。列表框可以为用户提供所有选项的列表。虽然也可设置列表框为多列列表的形式,但在缺省时列表框单列垂直显示所有的选项,如果项目数目超过了列表框可显示的数目,控件上将自动出现滚动条。这时用户可在列表中上、下、左、右滚动。ListBox控件在工具箱中的图标如图所示: 一、ListBox常用属性 1、BackColor 属性:用于显示ListBox控件中的文本和图形的背景颜色,默认为白色(Window) 2、BorderStyle 属性:控制在列表框ListBox周围绘制的边框的类型,其枚举值为下面三个: BorderStyle.None——无边框 BorderStyle.FixedSingle——单行边框 BorderStyle.Fixed3D——三维边框 默认值为BorderStyle.Fixed3D。 3、Font、ForeColor 属性:前者用于调整列表框中文本的字体,后者用于调整文本框中文本或者图形的前景色。 4、MultiColumn 属性:指示列表框中的项是否以水平的方式在列表框中显示,默认为False,此时所有的项都只显示为一列,当列表框无法显示全部的项的时候,将会出现一个垂直的滚动条;如果MultiColumn属性为True,则列表框以多列的形式来显示所有的项,如果一列无法全部显示完,则在水平位置重新显示一列,直到显示完毕为止,此时将会出现一个水平滚动条,如下图一所示:

DropDownList,ListBox控件

5.7 列表控件(DropDownList,ListBox) 在Web开发中,经常会需要使用列表控件,让用户的输入更加简单。例如在用户注册时,用户的所在地是有限的集合,而且用户不喜欢经常键入,这样就可以使用列表控件。同样列表控件还能够简化用户输入并且防止用户输入在实际中不存在的数据,如性别的选择等。 5.7.1 DropDownList列表控件 列表控件能在一个控件中为用户提供多个选项,同时又能够避免用户输入错误的选项。例如,在用121 户注册时,可以选择性别是男,或者女,就可以使用DropDownList列表控件,同时又避免了用户输入其他的信息。因为性别除了男就是女,输入其他的信息说明这个信息是错误或者是无效的。下列语句声明了一个DropDownList列表控件,示例代码如下所示。 1 2 3 4 5 6 7 上述代码创建了一个DropDownList列表控件,并手动增加了列表项。同时DropDownList列表控件也可以绑定数据源控件。DropDownList列表控件最常用的事件是SelectedIndexChanged,当DropDownList列表控件选择项发生变化时,则会触发该事件,示例代码如下所示。 protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e) { Label1.Text = "你选择了第" + DropDownList1.Text + "项"; } 上述代码中,当选择的项目发生变化时则会触发该事件,如图5-14所示。当用户再次进行选择时,系统会将更改标签1中的文本,如图5-15所示。 当用户选择相应的项目时,就会触发SelectedIndexChanged事件,开发人员可以通过捕捉相应的用户选中的控件进行编程处理,这里就捕捉了用户选择的数字进行字体大小的更改。 5.7.2 ListBox列表控件

https://www.doczj.com/doc/cf5629117.html,入门分组控件:checkedlistbox控件的使用

https://www.doczj.com/doc/cf5629117.html,入门——分组控件:CheckedListBox控件的使用https://www.doczj.com/doc/cf5629117.html,入门——分组控件:CheckedListBox控件的使用 在https://www.doczj.com/doc/cf5629117.html,中,CheckBox、CheckedListBox、RadioButton 和TrackBar这四个控件的功能都是设置和获取值的,我们 称之为设置值的控件。 前面我们了解了CheckBox控件和RadioButton控件的使用,这里我们来看CheckedListBox控件的使用,CheckedListBox控件在工具箱中的图标如图所示: Windows窗体的CheckedListBox控件,扩展了ListBox控件(https://www.doczj.com/doc/cf5629117.html,入门——ListBox控件的使用)。它几乎可以完成列表框控件(ListBox)可以完成的所有任务,并且还可以在列表中的项旁边显示复选的标记。这两种控件的其他差异是:CheckedListBox控件(也称复选列表框)只支持DrawMode.Normal,并且复选列表框只能有一项选定或者没有任何选定,选定的项在窗体上突出显示,与以选中的项区别。 一、常用属性和方法 CheckedListBox控件的属性和方法基本上都可以在ListBox

控件中找到,可以参看这里:https://www.doczj.com/doc/cf5629117.html,入门——ListBox控件的使用。值得我们重视的是,CheckedListBox控件的CheckedItems属性,它是CheckedListBox控件中复选框选中的项的集合,我们可以通过检索该属性来获得选中的项。CheckOnClick 指示是否只要一选择项即切换复选框。默认行为是在首次单击时更改选定内容,然后让用户再次单击以应用选中标记。但在某些情况下,您可能愿意一单击项就选中它。 下面我们就直接以实例来了解和掌握CheckedListBox控件的方法和属性。 二、在CheckedListBox控件中添加、移除项 因为CheckedListBox控件控件的使用和ListBox控件相似,所以我们这里就只针对用编程的方法来对CheckedListBox 控件的操作。 1、添加项 添加一个名为“新建文件”的项,且复选框为选中状态 CheckedListBox1.Items.Add("新建文件", True) 添加一个名为“拷贝文件”的项,且复选框为未选中状态 CheckedListBox1.Items.Add("拷贝文件", False) 2、删除项

MFC ListBox控件用法介绍

列表控件可以看作是功能增强的ListBox,它提供了四种风格,而且可以同时显示一列的多中属性值。MFC中使用CListCtrl类来封装列表控件的各种操作。通过调用?BOOLCreate(DWORDdwStyle,constRECT&rect,CWnd*pParentWnd,UINTnID);创建一个窗口,dwStyle中可以使用以下一些列表控件的专用风格: ?LVS_ICONLVS_SMALLICONLVS_LISTLVS_REPORT这四种风格决定控件的外观,同时只可以选择其中一种,分别对应:大图标显示,小图标显示,列表显示,详细报表显示 ?LVS_EDITLABELS结点的显示字符可以被编辑,对于报表风格来讲可编辑的只为第一列。 ?LVS_SHOWSELALWAYS在失去焦点时也显示当前选中的结点 ?LVS_SINGLESEL同时只能选中列表中一项 首先你需要设置列表控件所使用的ImageList,如果你使用大图标显示风格,你就需要以如下形式调用:? CImageList*SetImageList(CImageList*pImageList,LVSIL_NORMAL);? 如果使用其它三种风格显示而不想显示图标你可以不进行任何设置,否则需要以如下形式调用:? CImageList*SetImageList(CImageList*pImageList,LVSIL_SMALL); 通过调用intInsertItem(intnItem,LPCTSTRlpszItem);可以在列表控件中nItem指明位置插入一项,lpszItem为显示字符。除LVS_REPORT风格外其他三种风格都只需

CSharp对ListBox重写

WinForms中重写ListBox控件 /* **使用时 simListBox1.ItemCollection.Add(1, "百里屠苏"); simListBox1.ItemCollection.Add(2, "风晴雪"); simListBox1.ItemCollection.Add(3, "方兰生"); simListBox1.ItemCollection.Add(4, "欧阳少恭"); simListBox1.ItemCollection.Add(5, "工长君"); simListBox1.ItemCollection.Add(6, "古剑奇谭"); simListBox1.SelectionMode = SelectionMode.MultiExtended;//默认值是:只能选择一项在按钮的单击事件中: private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < simListBox1.SelectedListBoxItems.Count; i++) { //查看选择的所有项的显示内容 MessageBox.Show(simListBox1.SelectedListBoxItems[i].Key.ToString()); MessageBox.Show(simListBox1.SelectedListBoxItems[i].Item.ToString()); } } */ SimListBox控件源代码如下: using System; using System.Collections.Generic; using https://www.doczj.com/doc/cf5629117.html,ponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; namespace DoorControl { ///

/// 专门用于填充ListBox信息方法 /// public partial class SimListBox : ListBox { //AutoScaleMode AutoScaleMode;请在SimListBox.designer.cs中注释掉 //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; public SimListBox()

VC++中 ListBox(列表框)的使用

VC++ ListBox(列表框)的使用 文档制作:@柳絮飘诗 ListBox的操作比较简单 1添加数据 声明控件变量的类别为Control,变量类型为CListBox,变量名为m_ListBox_Content. m_ListBox_Content.AddString(_T("123")); m_ListBox_Content.AddString(_T("汉字")); m_ListBox_Content.AddString(_T("English")); m_ListBox_Content.AddString(_T("!@#$%^&*()")); 2获取数据 CString s; m_ListBox_Content.GetText(1,s); MessageBox(s,_T("取得第2行数据"),MB_OK); s.ReleaseBuffer(); 将会得到"汉字"这个字符串,如果没有得到"汉字"这个字符串,是因为ListBox的Sort属性设为True了.设为False之后就按照你编写顺序写入(原来是这样 之前都没注意过) 3获取选择的数据 首先要将ListBox的Selection属性设置为Multiple; int nSel; nSel=m_ListBox_Content.GetCurSel(); CString s; m_ListBox_Content.GetText(nSel,s); MessageBox(s,_T("您选择的是"),MB_OK); s.ReleaseBuffer(); 4获取选择ListBox项的多个数据 首先要将ListBox的Selection的属性设置为Multiple int nSel = m_ListBox_Content.GetSelCount(); CArray< int,int& > arrayListSel; arrayListSel.SetSize(nSel); m_ListBox_Content.GetSelItems(nSel,arrayListSel.GetData()); CString s = _T(""); for( int i=0; i< nSel; i++ ) { m_ListBox_Content.GetText( arrayListSel[i], s); MessageBox(s,_T("您选择的是"),MB_OK); } 5双击删除所选项 添加一个ListBox的双击事件 m_ListBox_Content.DeleteString(m_ListBox_Content.GetCurSel()); //例子: CListBox *List; List=(CListBox*)GetDlgItem(IDC_LIST1); List -> AddString("AAA"); List -> SetCurSel(0);

https://www.doczj.com/doc/cf5629117.html,中 ListBox列表框控件的使用方法

ListBox 控件允许用户从预定义的列表中选择一个或多个项。它与DropDownList 控件的不同之处在于,它不但可以一次显示多个项,而且(可选)还允许用户选择多个项。 一、属性属性值作用SelectionModeSingle|Multiple列表选择模式:单选|多选SelectedFalse|true是否为选中状态 二、示例 ListBox.aspx 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListBox.aspx.cs" Inherits="WebControls_ListBox" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.doczj.com/doc/cf5629117.html,/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="https://www.doczj.com/doc/cf5629117.html,/1999/xhtml"> <head runat="server">     <title></title>     <style type="text/css">         .style1         {             width: 293px;         }         .style2         {             width: 233px;         }     </style> </head> <body>     <form id="form1" runat="server">     <div>         <h5>1,单选列表框</h5>         省份:<asp:ListBox ID="lstProv"  runat="server">             <asp:ListItem>山东</asp:ListItem>             <asp:ListItem Selected="True">河北</asp:ListItem>             <asp:ListItem>内蒙</asp:ListItem>             <asp:ListItem>安徽</asp:ListItem>         </asp:ListBox>         <br />         <asp:Button ID="btnSubmit" runat="server" Text="提交"

ListBox控件的使用

CListBox是一种很常见的操作控件,用来从列表中选择相应的内容,这个控件以前没有用过,而且在一些书或教程上面讲解的很简单,无法从中看出如何来使用该控件,为了方便以后使用,这里先从MSDN上面的说明开始介绍起,把该控件的有关内容翻译一下,然后做一个程序来综合测试一下相关的功能 CListBox是从CWnd类继承下来的,对该类的总体介绍如下: 一、总体介绍: 一个列表框显示了一系列的项目,比如说文件名等,用户可以查看和选择操作。 在单选列表框中,用户只能选择一个项目,而对于多选列表框,一系列的项目都可以被选择,当用户选择项目后,该项目会被高亮显示,同时列表框将向其父窗口发送一个消息。 你可以用两种方法来创建一个列表框:从对话框个工具栏中拖动或者是直接用代码来创建。但用代码来直接创建时,先构造一个单选框的对象,然后调用Create方法创建一个列表框控件并将它和一个列表框对象关联起来。如果使用对话框工具来生成列表框,那么在对话框类中建立一个列表框的变量,然后使用DoDataExchange函数中的DDX_Control宏来连接该变量和控件(这一点在你用向导创建变量的时候会自动完成) 构造函数可以是一个一步过程,仅仅调用Create方法就可以了 如果你想处理列表框向其父窗口发送的消息(常常是CDialog类),那么你可以添加一个消息循环的入口和一个消息相应函数,格式如下: ON_Notification( id, memberFxn ) 这里id表示的是列表框子窗口的ID,而memberFxn则是父窗口中用来处理这个消息的消息响应函数,其原型如下: afx_msg void memberFxn( ); 下面的表格列举了有哪些消息能够发送给父窗口: 当你使用对话框工具箱来创建一个列表框时,列表框对象会在对话框关闭时自动销毁。 当你在一个窗口(WINDOW)中创建列表框时,你需要自行销毁其对象。如果你是在栈中创建列表框对象,那么它会自动销毁,而如果你在堆中创建列表框对象,比如采用new运算符,那么你必须调用delete方法来销毁该对象 如果你在列表框对象中申请了内存空间,你需要重载列表框的解构函数来释放内存 二、列表框的属性: 从上面的介绍中可以看到,列表框有不同的类型,这些都是通过设置属性来完成的,在VS2008上至少是这么来设置的,下面介绍一下几个关键的属性: 1)Selection属性:该属性用来指定列表框是否允许用户同时选择多个项,前面讲的多选列

MFC ListBox控件用法介绍

MFC CListCtrl 使用介绍 列表控件可以看作是功能增强的ListBox,它提供了四种风格,而且可以同时显示一列的多中属性值。MFC中使用CListCtrl类来封装列表控件的各种操作。通过调用 BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );创建一个窗口,dwStyle中可以使用以下一些列表控件的专用风格: ?LVS_ICON LVS_SMALLICON LVS_LIST LVS_REPORT 这四种风格决定控件的外观,同时只可以选择其中一种,分别对应:大图标显示,小图标显示,列表显示,详细报表显示 ?LVS_EDITLABELS 结点的显示字符可以被编辑,对于报表风格来讲可编辑的只为第一列。 ?LVS_SHOWSELALWAYS 在失去焦点时也显示当前选中的结点 ?LVS_SINGLESEL 同时只能选中列表中一项 首先你需要设置列表控件所使用的ImageList,如果你使用大图标显示风格,你就需要以如下形式调用: CImageList* SetImageList( CImageList* pImageList, LVSIL_NORMAL); 如果使用其它三种风格显示而不想显示图标你可以不进行任何设置,否则需要以如下形式调用: CImageList* SetImageList( CImageList* pImageList, LVSIL_SMALL); 通过调用int InsertItem( int nItem, LPCTSTR lpszItem );可以在列表控件中nItem指明位置插入一项,lpszItem为显示字符。除LVS_REPORT风格外其他三种风格都只需要直接调用InsertItem就可以了,但如果使用报表风格就必须先设置列表控件中的列信息。 通过调用int InsertColumn( int nCol, LPCTSTR lpszColumnHeading, int nFormat , int nWidth, int nSubItem);可以插入列。iCol为列的位置,从零开始,lpszColumnHeading为显示的列名,nFormat为显示对齐方式,nWidth为显示宽度,nSubItem为分配给该列的列索引。 在有多列的列表控件中就需要为每一项指明其在每一列中的显示字符,通过调用 BOOL SetItemText( int nItem, int nSubItem, LPTSTR lpszText );可以设置每列的显示字符。nItem为设置的项的位置,nSubItem为列位置,lpszText为显示字符。下面的代码演示了如何设置多列并插入数据: m_list.SetImageList(&m_listSmall,LVSIL_SMALL);//设置ImageList m_list.InsertColumn(0,"Col 1",LVCFMT_LEFT,300,0);//设置列 m_list.InsertColumn(1,"Col 2",LVCFMT_LEFT,300,1); m_list.InsertColumn(2,"Col 3",LVCFMT_LEFT,300,2); m_list.InsertItem(0,"Item 1_1");//插入行 m_list.SetItemText(0,1,"Item 1_2");//设置该行的不同列的显示字符 m_list.SetItemText(0,2,"Item 1_3"); 此外CListCtrl还提供了一些函数用于得到/修改控件的状态。 COLORREF GetTextColor( )/BOOL SetTextColor( COLORREF cr );用于得到/设置显示的

ListBox控件使用示例

1.按图所示的界面和下表向窗体上添加控件并设置属性 2.代码如下 using System; using System.Collections.Generic; using https://www.doczj.com/doc/cf5629117.html,ponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace lianxiwinform { public partial class Form2 : Form { public Form2() { InitializeComponent(); listBox1.Items.AddRange(new object[] { "武汉", "北京", "广州", "上海", "天津", "重庆", "成都", "深圳" }); } private void button1_Click(object sender, EventArgs e) {

if (listBox1.SelectedIndex >= 0) { listBox2.Items.Add(listBox1.Text); listBox1.Items.RemoveAt(listBox1.SelectedIndex); } int l = listBox1.SelectedItems.Count; for (int i = 0; i < l; i++) { listBox2.Items.Add(listBox1.SelectedItems[0]); listBox1.Items.Remove(listBox1.SelectedItems[0]); } } private void button2_Click(object sender, EventArgs e) { int l = listBox1.Items.Count; for (int i = 0; i < l; i++) { listBox2.Items.Add(listBox1.Items[0]); listBox1.Items.Remove(listBox1.Items[0]); } } private void button3_Click(object sender, EventArgs e) { if (listBox2.SelectedIndex >= 0) { listBox1.Items.Add(listBox2.Text); listBox2.Items.RemoveAt(listBox2.SelectedIndex); } int r = listBox2.SelectedItems.Count; for (int i = 0; i < r; i++) { listBox1.Items.Add(listBox2.SelectedItems[0]); listBox2.Items.Remove(listBox2.SelectedItems[0]); } } private void button4_Click(object sender, EventArgs e) { int r = listBox2.Items.Count; for (int i = 0; i < r; i++) { listBox1.Items.Add(listBox2.Items[0]); listBox2.Items.Remove(listBox2.Items[0]); } }

listBox控件item属性

列表控件ListBox(lst) Item属性是一个集合,可以获取对当前存储在ListBox中的项列表的引用。通过这个引用,可以在集合中访问项,添加项,移除项和获得项的技术等等。 1 访问项 可以像访问数组元素一样访问Items集合中的项,例如: //将列表中的第二项显示在文本框中 textBox1.Text=listBox1. Items[1].ToString(); 2 添加项 有多种方法可以向集合中添加项。Add方法提供向集合中添加单个对象的能力。若要向集合中添加多个对象,可创建一个项的数组,并将其分配给AddRange方法。如果要在集合内的特定位置插入某个对象,可以使用Instert方法。例如: //在列表中体添加一个新项 listBox1. Items.Add(“清华大学”);

//将listBox1中的所有项添加到listBox2中 for(int i=0;i< listBox1. Items.Count;i++) { listBox2. Items.Add(listBox1. Items[i]); } //在列表中添加三个新项 listBox1. Items.AddRange(new object[]{“北京大学”,”复旦大学”,”南京大学”}); //在列表的第二个项之后插入一个新项 listBox1. Items.Insert(2,”武汉大学”); 3 移除项 如果要移除列表中的项,可以使用Remove方法,或者如果知道项在集合中的位置,也可以使用RemoveAt方法。Clear方法可以一次从集合中移除所有项,而不必使用Remove方法一次移除一个项。//移除列表中的第三个项 listBox1. Items. Remove(listBox1. Items[2]); //或者 listBox1. Items. RemoveAt(2); //移除列表中的所有项 listBox1. Items.Clear(); 4 获取选定项 通过SelectedIndex属性可以获得选定项的索引,如果没有选定任何

MFC控件之ListBox

mfc控件之ListBox 1、增加listbox控件,去掉style中的sort项的勾,表示显示不用排序 2、插入、删除记录 m_listbox.AddString(LPCTSTR str); //增加一条记录,如果没勾选sort项的话加到列表框的最后m_listbox.DeleteString(UINT n); //删除第n条记录 m_listbox.InsertString(UINT n, LPCTSTR str); //在第n行插入记录 3、删除所有记录m_listbox.ResetContent(); 4、获取列表中行数m_listbox.GetCount(); 5、获取当前选中记录行数m_listbox.GetCursel(); 6、设置指定行数为选定m_listbox.SetCursel(int n); MFC中listbox控件是为了显示一系列的文本,每个文本占一行。 Listbox控件可以设置属性为: LBS_CHILD :(默认)子窗口 LBS_Visible :(默认)可视 LBS_Multiplesel :可选择多行 LBS_Extendedsel :可以使用shift或ctrl选择多行 LBS_SORT:所有行按字母顺序进行排序 对Listbox进行操作: Int listbox.AddString (LPCTSTR Str) :对listbox的首行添加一个str的文本行, 即此时nIndex为0; Int listbox.DeleteString (uint nIndex) :删除listbox中第nIndex行的文本行,注意nIndex是从0起始的;int listbox.InsertString (uint xIndex, LPCTSTR Str):在listbox中第nIndex行插入一个str的文本行; Int listbox.GetCount ( ):获取listbox的行的总数目; Int listbox.GetCurSel ( ):得到listbox选中的文本行的行数。 Void listbox.ResetContent ( ):清除listbox中的所有数据; Int listbox.GetCurCount ( ):获取listbox被选中的行的数目; Int listbox.FindString ( int nstart , LPCTSTR Str ):从第nstart行开始起进行查找,查找Str的文本行;Int listbox.SelectString ( int nstart, LPCTSTR Str ) :从第nstart行开始,选择包含Str字符串的那一行; 对Listbox中某一行进行双击操作,要产生一个响应函数的步骤: 在 BEGIN_MESSAGE_MAP() END_MESSAGE_MAP()

VB中ListBox的应用演示教学

V B中L i s t B o x的应 用

https://www.doczj.com/doc/cf5629117.html,入门——ListBox控件的使用 【字体大小:小中大】2008-02-18 19:56 来源:作者: ListBox(列表框)控件可以显示一组项目的列表,用户可以根据需要从中选择一个或多个选项。列表框可以为用户提供所有选项的列表。虽然也可设置列表框为多列列表的形式,但在缺省时列表框单列垂直显示所有的选项,如果项目数目超过了列表框可显示的数目,控件上将自动出现滚动条。这时用户可在列表中上、下、左、右滚动。ListBox控件在工具箱中的图标如图所示: 一、ListBox常用属性 1、BackColor 属性:用于显示ListBox控件中的文本和图形的背景颜色,默认为白色(Window) 2、BorderStyle 属性:控制在列表框ListBox周围绘制的边框的类型,其枚举值为下面三个: BorderStyle.None——无边框 BorderStyle.FixedSingle——单行边框 BorderStyle.Fixed3D——三维边框 默认值为BorderStyle.Fixed3D。

3、Font、ForeColor 属性:前者用于调整列表框中文本的字体,后者用于调整文本框中文本或者图形的前景色。 4、MultiColumn 属性:指示列表框中的项是否以水平的方式在列表框中显示,默认为False,此时所有的项都只显示为一列,当列表框无法显示全部的项的时候,将会出现一个垂直的滚动条;如果MultiColumn属性为True,则列表框 以多列的形式来显示所有的项,如果一列无法全部显示完,则在水平位置重新显示一列,直到显示完毕为止,此时将会出现一个水平滚动条,如下图一所示: 5、ColumnWidth 属性:指示“多列列表框”中各列的宽度。当MultiColumn 属性为True时才起作用,其默认值为0,即将默认宽度分配给每列。可以使用此属性确保多列 ListBox 中的每列都可正确显示其项,我们可以通过如下代码来自己设置列表框ColumnWidth 属性的值,以确保能以最优的宽度来显示列 表: Private Sub SetupMyMultiColumn() Dim x As Integer Dim width As Integer = CInt(ListBox1.CreateGraphics().MeasureString(ListBox1.Items(ListBox1. Items.Count - 1).ToString(), ListBox1.Font).Width) ListBox1.ColumnWidth = width

相关主题
文本预览
相关文档 最新文档