PDA

View Full Version : سوال: داشتن ComboBox و ListBox با گزینه های غیر فعال



baran_mehr
دوشنبه 22 مهر 1387, 14:09 عصر
سلام دوستان .:لبخندساده:
آیا میشه ما بعضی از سدیف های کمبوباکس یا لیست باکس رو غیر فعال کنیم .
مثلا تو لیست باکسمون ردیف اولش رو نشه انتخاب کرد و یا تو کمبوباکس و قتی رو ردیف دوم کلیک میکنیم انتخاب نشه. به عکسی که گذاشتم نگاه کنید منظورم رو متوجه میشید.

baran_mehr
دوشنبه 22 مهر 1387, 18:55 عصر
کسی در این ضمینه نمیتونه منو راهنمایی کنه؟؟
ببخشید یادم رفته بود عکس رو بزارم

rooshan2008
دوشنبه 22 مهر 1387, 20:45 عصر
سلام
برای اینکه کارت راحت تر بشه و برای هر کنترول لیست باکس نخوایی این کار رو بکنی من این راه رو پیشنهاد میکنم:
اول یه پروژه باز کن و به پروژت یک classاضافه کن و این کد که می نویسم رو کلاً توش کپی کن:





Public Class RoshanList : Inherits ListBox
Dim LockIndex_1() As Integer


Public Function GetLocKItems(ByVal Index() As Integer)
Return LockIndex_1
End Function


Public Sub LocKItems(ByVal Index() As Integer)
LockIndex_1 = Index
End Sub


Private Sub RoshanList_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
UnSelect()
End Sub


Private Sub RoshanList_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
UnSelect()
End Sub


Private Sub RoshanList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SelectedIndexChanged
UnSelect()
End Sub


Public Sub ClearLock()
ReDim LockIndex_1(0)
LockIndex_1(0) = -1
End Sub


Private Sub UnSelect()
If Not IsNothing(LockIndex_1) Then

For i = 0 To LockIndex_1.Count - 1
If Me.SelectedIndex = LockIndex_1(i) Then

Me.SelectedIndex = -1
Exit Sub

End If

Next

End If

End Sub

End Class



1:حالا از برنامه build بگیر
2:;کلاس رو ببند و یه سری به ToolBoxبزن یه کنترول جدید اضافه شده به نام RoshanList

من فقط تو کلاس بالا Listboxرو سفارشی کردم
دوتا تابع بهش اضافه کردم :
1:(LocKItems)آرایه ای از integer می گیره و اونها رو قفل می کنه
2:(ClearLock) تمام قفل ها رو پاک می کنه

این هم یه نمونه کد کار با این کنترول جدید:




Dim J(3) As Integer

J(0) = 0
J(1) = 1
J(2) = 5
RoshanList1.LocKItems(J)



من فقط لیست باکس رو درست کردم برای کامبوباکس خودت زحمت بکش همین رو شبیه سای کن

البته این راه حل من خیلی ابتکاری هست و نمیدونم خوب کار می کنه یا نه شاید راه حل بهتری وجود داشته باشه

baran_mehr
دوشنبه 22 مهر 1387, 21:26 عصر
گلم به Count ایراد میگیره:

For i = 0 To LockIndex_1.Count - 1
و آیا راهی جز کلاس وجود نداره؟؟ یعنی خود دات نت امکانی رو قرار داده باشه.

baran_mehr
چهارشنبه 24 مهر 1387, 13:00 عصر
rooshan2008 میشه منو راهنمایی کنید.

rooshan2008
چهارشنبه 24 مهر 1387, 16:05 عصر
سلام
درباره کدوم قسمت؟؟؟؟

mostafaaa
چهارشنبه 24 مهر 1387, 17:34 عصر
فکر کنم این مشکلت رو حل کنه.

Public Class Form1
Dim DL As New List(Of Integer) 'Disabled List
Public Property DisabledList() As List(Of Integer)
Get
Return DL
End Get
Set(ByVal value As List(Of Integer))
DL = value
End Set
End Property
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.ListBox1.DrawMode = DrawMode.OwnerDrawFixed
DisabledList.Add(2)
DisabledList.Add(1)
End Sub
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
Dim myBrush As Brush
Dim myBackColor As Color = Color.White
Dim myForeColor As Color = Color.Black
Dim myFont As Font = Me.Font
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
myBackColor = Color.Blue
myForeColor = Color.White
End If
'Checking the property of item in DataSet
If isDisabled(e.Index) Then
myFont = New Font(Me.Font, FontStyle.Strikeout)
myForeColor = Color.Gray
myBackColor = Color.WhiteSmoke
End If

myBrush = New SolidBrush(myBackColor)
e.Graphics.FillRectangle(myBrush, e.Bounds)
myBrush = New SolidBrush(myForeColor)
e.Graphics.DrawString(ListBox1.Items(e.Index), myFont, myBrush, New RectangleF(e.Bounds.X, _
e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.DrawFocusRectangle()
End Sub
Private Function isDisabled(ByVal Index As Integer) As Boolean
For Each item As Integer In DisabledList
If item = Index Then Return True
Next
End Function
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If isDisabled(Me.ListBox1.SelectedIndex) Then Me.ListBox1.SelectedIndex += 1
End Sub
End Class

baran_mehr
پنج شنبه 25 مهر 1387, 12:41 عصر
سلام دوستان ببخشید من یک کم تاخیر داشتم.
داداش mostafaa کدت حرف نداشت(خیلی حال کردم).
دستت درد نکنه داداشی.برای combobox هم میشه از همین شیوه استفاده کرد؟

baran_mehr
پنج شنبه 25 مهر 1387, 12:41 عصر
داداشی میشه درباره کدای زیر یه کوچولو توضیح بدی:

If isDisabled(Me.ListBox1.SelectedIndex) Then Me.ListBox1.SelectedIndex += 1

myBrush = New SolidBrush(myBackColor)
e.Graphics.FillRectangle(myBrush, e.Bounds)
myBrush = New SolidBrush(myForeColor)
e.Graphics.DrawString(ListBox1.Items(e.Index), myFont, myBrush, New RectangleF(e.Bounds.X, _
e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.DrawFocusRectangle()

Public Property DisabledList() As List(Of Integer)
Get
Return DL
End Get
Set(ByVal value As List(Of Integer))
DL = value
End Set
End Property

mostafaaa
پنج شنبه 25 مهر 1387, 20:57 عصر
بری ComboBox تست نکردم ، ولی به احتمال قریب به یقین امکانپذیره.
در مورد کدها هم.
قسمت اولی که مشخص کردید چک میکنه ببینه که آیتم انتخاب شده تو لیست آیتمهای Disable هست یا نه، که اگه بود آیتم انتخاب شده رو یه دونه جلو میبره.
قسمت دوم هم که مشخص کردید مربوط به رویداد Draw هر آیتم از ListBox هست . که از اشیا GDI برای Draw کردن آیتمهای لیست باکس استفاده میکنی.
قسمت سوم هم یه Property هست برای Set/Get کردن آیتم به داخل لیست آیتمهایی که باید Disable بشن.
اگه بد توضیح دادم ببخشید چون یه خورده عجله داشتم موقع نوشتن.

baran_mehr
پنج شنبه 25 مهر 1387, 22:03 عصر
سلام مصطفی جان.
نه خوب بود.ممنون از این که کمک کردی.
از روشن جان هم ممنون.

gilsoft
یک شنبه 28 آبان 1391, 06:04 صبح
سلام دوستان

برای ComboBox باید از کد زیر استفاده کرد :


Private Items() As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Items = New String() {"Item0", "Unknown", "Item2", "Unknown", "Unknown", "Item5", "Item6"}
With ComboBox1
.DataSource = Items
.DrawMode = DrawMode.OwnerDrawVariable
.DropDownStyle = ComboBoxStyle.DropDownList
For i As Byte = 0 To ComboBox1.Items.Count - 1
If .Items.Item(i) = "Unknown" Then DisabledList.Add(i)
Next
.SelectedIndex = 0
End With

End Sub

Dim DL As New List(Of Integer) 'Disabled List

Public Property DisabledList() As List(Of Integer)
Get
Return DL
End Get
Set(ByVal value As List(Of Integer))
DL = value
End Set
End Property

Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
e.DrawBackground()
Dim myBrush As Brush
Dim myBackColor As Color = Color.White
Dim myForeColor As Color = Color.Black
Dim myFont As Font = Me.Font

If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
myBackColor = Color.RoyalBlue : myForeColor = Color.White
End If

If isDisabled(e.Index) Then
myFont = New Font(Me.Font, FontStyle.Strikeout + FontStyle.Italic)
myForeColor = Color.Gray : myBackColor = Color.WhiteSmoke
End If
myBrush = New SolidBrush(myBackColor)
e.Graphics.FillRectangle(myBrush, e.Bounds)

myBrush = New SolidBrush(myForeColor)
e.Graphics.DrawString(ComboBox1.Items(e.Index), myFont, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.DrawFocusRectangle()
End Sub

Private Function isDisabled(ByVal Index As Integer) As Boolean
For Each item As Integer In DisabledList
If item = Index Then Return True
Next
Return False
End Function

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If isDisabled(Me.ComboBox1.SelectedIndex) Then Me.ComboBox1.SelectedIndex += 1
End Sub


موفق باشید