在列表框中选择绘图模式设置为ownerdrawfixed的项目
问题描述:
我有一个列表框,我希望一些项目是不同的颜色,我明白要做到这一点,我必须将绘图模式设置为ownerdrawfixed。这工作正常,但是,现在我无法检索选定的项目。当绘图模式设置为正常时,当我点击列表框中的一个项目时,我将它放入一个文本框中。将drawmode设置为ownerdrawfixed,当我点击一个项目时,出现一个错误,“从类型'item'转换为类型'string'无效。此外,listbox不再被排序,即使排序的属性是设置为true(当ownerdrawfixed模式)。在列表框中选择绘图模式设置为ownerdrawfixed的项目
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim ac As Integer
LstAll.DrawMode = DrawMode.OwnerDrawFixed
MaxRec = 708
ChkShow = True
FileOpen(1, "C:\MyMov3\MovData.mdt", OpenMode.Random, , , Len(Mv3Rec))
For x = 1 To MaxRec
FileGet(1, Mv3Rec, x + 1)
'This If loop for the colored text
If Mv3Rec.Rc3Mlti = True And ChkShwMlti.Checked = True Then
ac = Asc(Trim(Mv3Rec.Rc3MTitle))
If ac > 0 Then
Dim i As New Item()
i.ItmColor = Color.Red
i.Txt = Trim(Mv3Rec.Rc3MTitle)
LstAll.Items.Add(i)
End If
End If
If ChkShow = True Then
Dim i As New Item() 'Needed for the black text when in ownerdrawfixed mode
i.ItmColor = Color.Black 'Needed for the black text when in ownerdrawfixed mode
i.Txt = Trim(Mv3Rec.Rc3Title) 'Needed for the black text when in ownerdrawfixed mode
LstAll.Items.Add(i) 'Needed for the black text when in ownerdrawfixed mode
'LstAll.Items.Add(Trim(Mv3Rec.Rc3Title)) 'This line adds the text when in normal mode
End If
Next
FileClose(1)
End Sub
Private Sub LstAll_DrawItem(sender As Object, e As System.Windows.Forms.DrawItemEventArgs) Handles LstAll.DrawItem
If e.Index < 0 Then Return
Dim i As Item
i = TryCast(LstAll.Items(e.Index), Item)
If i IsNot Nothing Then
e.Graphics.DrawString(i.Txt, e.Font, New SolidBrush(i.ItmColor), e.Bounds)
End If
End Sub
Private Sub LstAll_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles LstAll.SelectedIndexChanged
TextBox1.Text = LstAll.SelectedItem
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
End
End Sub
End Class
Public Class Item
Public Txt As String
Public ItmColor As Color
End Class
此代码是唯一的关键部位....并在空白表格上一个新的项目进行了测试。有了一个列表框(重命名为LstAll)的文本框,一个复选框(重命名为ChkShwMlti),这给出了与我需要它工作的程序相同的错误。但它使用的文件不包含Structure for ...,但我认为您可以获得想法
答
我已经想通了这个问题在LstAll.SelectedIndexCh anged
私人小组LstAll_SelectedIndexChanged(发件人为System.Object的,例如作为System.EventArgs)把手LstAll.SelectedIndexChanged
Dim i As New Item
i = LstAll.SelectedItem
TextBox1.Text = i.Txt
结束子
这个工作需要。
如果您需要代码帮助,您必须向我们显示代码。我们不是通灵者,我们不住在你的电脑里面。请阅读[问]并参加[导览] – Plutonix
对不起....这里是基本代码....用于一个新项目来演示问题。 –
它不会让我粘贴代码现在它说它太长 –