2017-07-28 93 views
0

我有一個列表框,我希望一些項目是不同的顏色,我明白要做到這一點,我必須將繪圖模式設置爲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 ...,但我認爲您可以獲得想法

+0

如果您需要代碼幫助,您必須向我們顯示代碼。我們不是通靈者,我們不住在你的電腦裏面。請閱讀[問]並參加[導覽] – Plutonix

+0

對不起....這裏是基本代碼....用於一個新項目來演示問題。 –

+0

它不會讓我粘貼代碼現在它說它太長 –

回答

0

我已經想通了這個問題在LstAll.SelectedIndexCh anged

私人小組LstAll_SelectedIndexChanged(發件人爲System.Object的,例如作爲System.EventArgs)把手LstAll.SelectedIndexChanged

Dim i As New Item 
    i = LstAll.SelectedItem 
    TextBox1.Text = i.Txt 

結束子

這個工作需要。