从所选文本文件填充特定文本并获取与特定文本关联的颜色

时间:2014-11-26 15:41:38

标签: vb.net

我正在开发一个实用程序,其中Textobiles列表填充在Combobox中。然后根据Combobox的Selected项目,我想获得单词" material"旁边的特定文本。 现在每个文本文件都可以有多个"材料"值。   这些文本文件包含以下格式的某些文本,

MV_PL--0

material Aluminium
 color 0.752941 0.752941 0.752941
 specular_color 0.87451 0.87451 0.87451
 end

material Brass_Frosted
 color 0.811765 0.713726 0.478431
 specular_color 0.87451 0.803922 0.635294
 end

我设法获得"材料"旁边的唯一第一个值。在列表框中。如何获得"材料"旁边列出的所有值?在每个Textfile?

[CODE]

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim strPath As String = "C:\Users\Test\"
    Dim dirInfo As New IO.DirectoryInfo(strPath)
    For Each file As FileInfo In dirInfo.GetFiles("*.txt", SearchOption.TopDirectoryOnly)
        FileComboBox.Items.Add(file.Name)
    Next
End Sub

Private Sub FileComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileComboBox.SelectionChangeCommitted

    MatlListBox.Items.Clear()
    Dim strPath As String = "C:\Users\Test\"
    Dim strRead As String = IO.File.ReadAllText(strPath + FileComboBox.SelectedItem.ToString)

   Dim Material As String = strRead.Split(New String() {"material "}, StringSplitOptions.None)(1).Split(" ")(0)
   MatlListBox.Items.Add(Material)

End Sub

[/代码]

最后,一旦我获得"材料"旁边的特定值我需要转换下一行中可用的值"颜色0.752941 0.752941 0.752941"到可以分配给文本框背景颜色的颜色。 请建议......

1 个答案:

答案 0 :(得分:2)

我确定你可以用一些花哨的RegEx来解析文件,但我很糟糕。这是一个普通的旧行处理例程,它从所选文本文件中提取名称和颜色。如果文件格式错误或其中一行中的值无效,我还没有添加任何其他条件。这里的实现使用了自定义"材料"包含名称和颜色值的类。材质实例被添加到ComboBox。重写的ToString()函数确定ComboBox如何显示Material的实例。当ComboBox的SelectedIndex发生更改时,我们将SelectedItem转换回Material并使用这些值来更改TextBox的颜色:

Private Sub FileComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileComboBox.SelectionChangeCommitted
    Dim materials As New List(Of Material)
    Dim strPath As String = "C:\Users\Test\"
    Dim lines As New List(Of String)(File.ReadAllLines(strPath + FileComboBox.SelectedItem.ToString))
    For i As Integer = 0 To lines.Count - 1
        If lines(i).StartsWith("material") AndAlso i < lines.Count - 1 Then
            Dim mat As New Material

            Dim values() As String
            values = lines(i).Split(" ")
            If values.Length >= 2 Then
                mat.Name = values(1)

                If lines(i + 1).StartsWith(" color") Then
                    values = lines(i + 1).Split(" ")
                    If values.Length >= 5 Then
                        Dim value As Double
                        Dim R, G, B As Byte
                        If Double.TryParse(values(2), value) Then
                            If value >= 0 And value <= 1 Then
                                R = value * 255

                                If Double.TryParse(values(3), value) Then
                                    If value >= 0 And value <= 1 Then
                                        G = value * 255

                                        If Double.TryParse(values(4), value) Then
                                            If value >= 0 And value <= 1 Then
                                                B = value * 255

                                                mat.Color = Color.FromArgb(R, G, B)
                                                materials.Add(mat)
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    Next
    materials.Sort()
    MatlListBox.DataSource = materials
End Sub

Private Class Material
    Implements IComparable(Of Material)

    Public Name As String
    Public Color As Color

    Public Overrides Function ToString() As String
        Return Name
    End Function

    Public Function CompareTo(other As Material) As Integer Implements IComparable(Of Material).CompareTo
        Return Me.Name.CompareTo(other.Name)
    End Function

End Class

Private Sub MatlListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles MatlListBox.SelectedIndexChanged
    If MatlListBox.SelectedIndex <> -1 Then
        Dim mat As Material = DirectCast(MatlListBox.SelectedItem, Material)
        TextBox1.BackColor = mat.Color
    End If
End Sub