生成带有文本框/标签的过程图像框,以显示其下的文本

时间:2017-04-22 15:10:20

标签: .net vb.net winforms procedural

嘿我有一些代码从xml文件中提取信息并将其作为字符串返回。我试图弄清楚如何获取xml文件中的所有块名,并为每个生成的框生成带有预选图像的图像框。这是我的代码。

Imports System.Xml
Imports System.Xml.Linq
Imports System.IO
Public Class Form1
    Public FILENAME As String = String.Empty
    Public models As Model
    Public productionBlocks As New Panel
    Public pictureBlocks As New List(Of PictureBox)
    Public gridsinfo As CubeGrid

    Public dictCount As New Dictionary(Of String, Integer)
    Public dictionay As New Dictionary(Of String, String)
    Public fileloader As New OpenFileDialog

    Dim NUMBER_OF_PANELS = 0

    Const MAIN_PANEL_WIDTH As Integer = 1000
    Const MAIN_PANEL_HEIGHT As Integer = 1000
    Const MAIN_PANEL_TOP As Integer = 50
    Const MAIN_PANEL_LEFT As Integer = 50

    Const PANEL_COLUMNS As Integer = 5
    Const PANEL_WIDTH_MARGIN As Integer = 10
    Const PANEL_HEIGHT_MARGIN As Integer = 10

    Const PANEL_WIDTH As Integer = (MAIN_PANEL_WIDTH / PANEL_COLUMNS) - PANEL_WIDTH_MARGIN

    Dim PANEL_ROWS As Integer = 0
    Dim PANEL_HEIGHT As Integer = 0

    Dim PICTURE_BOX_MARGIN As Integer = 0

    Dim PICTURE_BOX_WIDTH As Integer = PANEL_WIDTH
    Dim PICTURE_BOX_HEIGHT As Integer = 0
    Const PICTURE_BOX_TOP As Integer = 0
    Const PICTURE_BOX_LEFT As Integer = 0

    Dim LABEL_WIDTH As Integer = PANEL_WIDTH
    Dim LABEL_HEIGHT As Integer = 0
    Dim LABEL_TOP As Integer = 0
    Dim LABEL_LEFT As Int16 = 0

    Dim IMAGE_PATH As String = "E:\VS Projects\Resources\SE\Cubes\ArmorCenter.jpg"

    Public _model As Model
    Public mainPanel As New CubeGrid
    Public subPanels As New List(Of CubeBlock)


    Sub New()
        ' This call is required by the designer.
        InitializeComponent()
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load
    End Sub

    Sub CalulateControlSizes(numberOfPanels As Integer)
        PANEL_ROWS = Math.Ceiling(NUMBER_OF_PANELS / PANEL_COLUMNS)
        'PANEL_HEIGHT = (MAIN_PANEL_HEIGHT / PANEL_ROWS) - PANEL_HEIGHT_MARGIN
        PANEL_HEIGHT = 200

        PICTURE_BOX_MARGIN = 0.1 * PANEL_HEIGHT

        PICTURE_BOX_HEIGHT = 0.8 * PANEL_HEIGHT

        LABEL_HEIGHT = 0.1 * PANEL_HEIGHT
        LABEL_TOP = PICTURE_BOX_HEIGHT + PANEL_HEIGHT_MARGIN

    End Sub




    'FindWords Function to get count of items
    Private Function FindWords(ByVal TextSearched As String, ByVal Paragraph As String) As Integer
        Dim location As Integer = 0
        Dim occurances As Integer = 0

        Do
            location = TextSearched.IndexOf(Paragraph, location)
            If location <> -1 Then
                occurances += 1
                location += Paragraph.Length
            End If
        Loop Until location = -1
        Return occurances
    End Function


    'Open Button
    Private Sub PictureBox1_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox1.MouseEnter
        PictureBox1.BorderStyle = BorderStyle.Fixed3D
    End Sub

    Private Sub PictureBox1_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox1.MouseLeave
        PictureBox1.BorderStyle = BorderStyle.FixedSingle
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        'Resetting the list box when new file is loaded
        Try
            For Each item In ListBox1.Items
                ListBox1.Items.Remove(item)
            Next
        Catch ex As Exception
        End Try

        'pre-load initialization
        fileloader.ShowDialog()
        FILENAME = fileloader.FileName
        models = New Model
        models.Load(FILENAME)

        'Grabbing information from XML file
        Dim blockNames As List(Of String) = models.print.Select(Function(x) x.cubes.Select(Function(y) y.cubeBlocks.Select(Function(z) z.SubtypeName)).SelectMany(Function(y) y).ToList()).FirstOrDefault()
        dictCount = blockNames.GroupBy(Function(x) x).ToDictionary(Function(x) x.Key, Function(y) y.Count)
        Dim display As String = models.print2.Select(Function(x) x.cubes.Select(Function(y) y.displayname)).SelectMany(Function(y) y).FirstOrDefault.ToString
        Dim gridsizeenum As String = models.print3.Select(Function(x) x.cubes.Select(Function(y) y.enumerator)).SelectMany(Function(y) y).FirstOrDefault.ToString
        'Dim ownername As String = models.print4.Select(Function(x) x.cubes.Select(Function(y) y.ownername)).SelectMany(Function(y) y).FirstOrDefault.ToString

        'Load items into the listbox
        For Each item In dictCount
            ListBox1.Items.Add(item.ToString)
        Next item

        'a useless dictionary method [UN-USED]
        For Each key As String In dictCount.Keys.AsEnumerable()
            Dim keylist As String = "subTypeName = '{0}', count = '{1}'" + key.ToString + dictCount(key).ToString
        Next key

        'Load Text from XML to the read only boxes
        InfluenceTextBox2.Text = "Grid Name: " + display.ToString
        InfluenceTextBox3.Text = "Grid Type: " + gridsizeenum.ToString + " Ship"
        'InfluenceTextBox1.Text = "Owner Name: " + ownername.ToString



        'Gui Displaying
        Dim reader As New StreamReader(FILENAME)
        Dim doc As XDocument = XDocument.Load(reader)
        Dim xmlstring As String = doc.ToString

        LightArmorBlockText.Text = "Qty: " + FindWords(xmlstring, "LargeBlockArmorBlock").ToString
        LightArmorSlopeText.Text = "Qty: " + FindWords(xmlstring, "LargeBlockArmorSlope").ToString
        LightArmorCornerText.Text = "Qty: " + FindWords(xmlstring, "LargeBlockArmorCorner").ToString
        LightArmorCornerInvertedText.Text = "Qty: " + FindWords(xmlstring, "LargeBlockArmorInvCorner").ToString



        'Function to calculate oxygentanks and hydrogen tanks
        Dim tanks As Integer = FindWords(xmlstring, "MyObjectBuilder_OxygenTank").ToString
        Dim oxygentanks As Integer = tanks - FindWords(xmlstring, "LargeHydrogenTank").ToString
        OxygenTanksText.Text = "Qty: " + oxygentanks.ToString
        HydrogenTanksText.Text = "Qty: " + FindWords(xmlstring, "LargeHydrogenTank").ToString





        dictionay = blockNames.GroupBy(Function(x) x).ToDictionary(Function(x) x.Key, Function(y) y.Key)
        'Procedural Control Generation

        _model = New Model()
        _model.Load(FILENAME)

        NUMBER_OF_PANELS = _model.print.cubes(0).cubeBlocks.Count
        CalulateControlSizes(NUMBER_OF_PANELS)

        mainPanel = _model.print.cubes(0)
        mainPanel.Height = MAIN_PANEL_HEIGHT
        mainPanel.Width = MAIN_PANEL_WIDTH
        mainPanel.Top = MAIN_PANEL_TOP
        mainPanel.Left = MAIN_PANEL_LEFT
        mainPanel.BackColor = Color.FromArgb(24, 24, 24)
        mainPanel.ForeColor = Color.White

        mainPanel.AutoScroll = True
        mainPanel.VerticalScroll.Visible = True
        mainPanel.HorizontalScroll.Visible = True
        Me.Controls.Add(mainPanel)

        For panelNumber As Integer = 0 To (NUMBER_OF_PANELS - 1)
            Dim row As Integer = Math.Floor(panelNumber / PANEL_COLUMNS)
            Dim col As Integer = panelNumber Mod PANEL_COLUMNS

            Dim newPanel As CubeBlock = _model.print.cubes(0).cubeBlocks(panelNumber)
            newPanel.Top = row * (PANEL_HEIGHT + PANEL_HEIGHT_MARGIN)
            newPanel.Left = col * (PANEL_WIDTH + PANEL_WIDTH_MARGIN)
            newPanel.Width = PANEL_WIDTH
            newPanel.Height = PANEL_HEIGHT
            newPanel.BackColor = Color.FromArgb(24, 24, 24)
            newPanel.ForeColor = Color.White
            mainPanel.Controls.Add(newPanel)

            subPanels.Add(newPanel)

            Dim newPicture As New PictureBox()
            newPicture.Height = PICTURE_BOX_HEIGHT
            newPicture.Width = PICTURE_BOX_WIDTH
            newPicture.Top = PICTURE_BOX_TOP
            newPicture.Left = PICTURE_BOX_LEFT
            newPicture.Image = Image.FromFile(IMAGE_PATH)
            newPicture.BackColor = Color.FromArgb(24, 24, 24)
            newPanel.Controls.Add(newPicture)

            Dim newLabel As New Label
            newLabel.Height = LABEL_HEIGHT
            newLabel.Width = LABEL_WIDTH
            newLabel.Top = LABEL_TOP
            newLabel.Left = LABEL_LEFT
            newLabel.BackColor = Color.Transparent
            newLabel.ForeColor = Color.White
            newLabel.Text = newPanel.SubtypeName & "(" & newPanel.count & ")"
            newLabel.BackColor = SystemColors.Window
            newLabel.ForeColor = SystemColors.WindowText

            newPanel.Controls.Add(newLabel)

        Next panelNumber
    End Sub
    'END OPEN BUTTON


    'Contributors Section
    Private Sub InfluenceTopButton1_Click(sender As Object, e As EventArgs) Handles InfluenceTopButton1.Click
        Form2.Show()
    End Sub


    'XML INPUT LOADING FUNCTION
End Class
Public Class Model
    Public print As New List(Of Model)
    Public print2 As New List(Of Model)
    Public print3 As New List(Of Model)
    Public print4 As New List(Of Model)
    Public _type As String
    Public _id As ID
    Public _display As String
    Public _display1 As String
    Public displayname As String
    Public enumerator As String
    Public cubes As List(Of CubeGrid)
    Public info As String
    Public Sub Load(filename As String)
        Dim reader As New StreamReader(filename)
        Dim doc As XDocument = XDocument.Load(reader)
        Dim firstNode As XElement = doc.FirstNode
        Dim xsiNs = firstNode.GetNamespaceOfPrefix("xsi")
        Dim drawingTypes = firstNode.Elements.FirstOrDefault()
        Dim drawingType = drawingTypes.Elements.FirstOrDefault()
        Dim drawingStr = drawingType.Name.LocalName
        print = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
           ._type = x.Attribute(xsiNs + "type"), ._id = x.Elements("Id").Select(Function(y) New ID() With {
                                              .type = y.Attribute("Type"),
                                              .subtype = y.Attribute("Subtype")
                                          }).FirstOrDefault(),
           .cubes = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
                                                      .id = y.Element("EntityId"),
                                                      .persistentFlags = y.Element("PersistentFlags"),
                                                      .position = y.Descendants("Position").Select(Function(z) New location() With {
                                                        .x = CType(z.Attribute("x"), Double),
                                                        .y = CType(z.Attribute("y"), Double),
                                                        .z = CType(z.Attribute("z"), Double)
                                                      }).FirstOrDefault(),
                                                      .forward = y.Descendants("Forward").Select(Function(z) New location() With {
                                                        .x = CType(z.Attribute("x"), Double),
                                                        .y = CType(z.Attribute("y"), Double),
                                                        .z = CType(z.Attribute("z"), Double)
                                                      }).FirstOrDefault(),
                                                      .up = y.Descendants("Up").Select(Function(z) New location() With {
                                                        .x = CType(z.Attribute("x"), Double),
                                                        .y = CType(z.Attribute("y"), Double),
                                                        .z = CType(z.Attribute("z"), Double)
                                                      }).FirstOrDefault(),
                                                      .orientation = y.Descendants("Orientation").Select(Function(z) New location() With {
                                                        .w = CType(z.Element("W"), Double),
                                                        .x = CType(z.Element("X"), Double),
                                                        .y = CType(z.Element("Y"), Double),
                                                        .z = CType(z.Element("Z"), Double)
                                                      }).FirstOrDefault(),
                                                      .cubeBlocks = y.Descendants("MyObjectBuilder_CubeBlock").GroupBy(Function(z) CType(z.Element("SubtypeName"), String)).Select(Function(z) New CubeBlock() With {
                                                        .SubtypeName = z.Key,
                                                        .count = z.Count
                                                      }).OrderBy(Function(z) z.SubtypeName).ToList()
                                                  }).ToList()
        }).ToList()



        print2 = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
            .cubes = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
                .displayname = y.Element("DisplayName")
            }).ToList()
        }).ToList()

        print3 = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
        .cubes = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
            .enumerator = y.Element("GridSizeEnum")
            }).ToList()
        }).ToList()

        'Not Working - Gets owner name
        print4 = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
            .cubes = x.Descendants("MyObjectBuilder_ShipBlueprintDefinition").Select(Function(y) New CubeGrid() With {
                .ownername = y.Element("DisplayName")
            }).ToList()
        }).ToList()
    End Sub
End Class
Public Class ID
    Public type As String
    Public subtype As String
End Class
Public Class CubeGrid
    Inherits Panel

    Public id As String
    Public persistentFlags As String
    Public position As location
    Public forward As location
    Public up As location
    Public orientation As location
    Public cubeBlocks As List(Of CubeBlock)

    Public displayname As String
    Public ownername As String
    Public enumerator As String
End Class
Public Class location
    Public w As Double
    Public x As Double
    Public y As Double
    Public z As Double
End Class
Public Class CubeBlock
    Inherits Panel

    Public SubtypeName As String
    Public username As String
    Public count As Integer
End Class

使用块名称中的每个项目在其下创建一个图片框和一个文本框&lt; http://imgur.com/a/XLzbd&gt;供参考。

使用这两个生成的项目作为子控件创建自定义控件:

    'Procedural Control Generation
    For Each item In dictCount
        For panelNumber As Integer = 0 To (NUMBER_OF_PANELS - 1)

            Dim row As Integer = Math.Floor(panelNumber / PANEL_COLUMNS)
            Dim col As Integer = panelNumber Mod PANEL_COLUMNS

            Dim newPanel As New Panel
            newPanel.Top = row * (PANEL_HEIGHT + PANEL_HEIGHT_MARGIN)
            newPanel.Left = col * (PANEL_WIDTH + PANEL_WIDTH_MARGIN)
            newPanel.Width = PANEL_WIDTH
            newPanel.Height = PANEL_HEIGHT
            mainPanel.Controls.Add(newPanel)
            subPanels.Add(newPanel)

            Dim newTextBox As New TextImagePanelLibrary.TextImagePanel
            newTextBox.BackgroundImage = My.Resources.heavy_armor_cube
            newTextBox.BackgroundImageLayout = ImageLayout.Stretch
            newTextBox.Height = 100
            newTextBox.Width = 100
            newTextBox.Top = TEXT_BOX_TOP
            newTextBox.Left = TEXT_BOX_LEFT
            newTextBox.Text = "Qty: " + item.ToString



            FlowLayoutPanel1.Controls.Add(newTextBox)
        Next panelNumber
    Next item

使用每个项目函数中的信息生成每个项目项目是包含块名称和计数的信息字符串

4 个答案:

答案 0 :(得分:0)

尝试以下作为开始

Imports System.Drawing
Public Class Form1
    Const NUMBER_OF_PANELS = 22

    Const MAIN_PANEL_WIDTH As Integer = 1000
    Const MAIN_PANEL_HEIGHT As Integer = 400
    Const MAIN_PANEL_TOP As Integer = 50
    Const MAIN_PANEL_LEFT As Integer = 50

    Const PANEL_COLUMNS As Integer = 5
    Const PANEL_WIDTH_MARGIN As Integer = 10
    Const PANEL_HEIGHT_MARGIN As Integer = 10

    Const PANEL_WIDTH As Integer = (MAIN_PANEL_WIDTH / PANEL_COLUMNS) - PANEL_WIDTH_MARGIN

    Dim PANEL_ROWS As Integer = Math.Ceiling(NUMBER_OF_PANELS / PANEL_COLUMNS)
    Dim PANEL_HEIGHT As Integer = (MAIN_PANEL_HEIGHT / PANEL_ROWS) - PANEL_HEIGHT_MARGIN

    Dim PICTURE_BOX_MARGIN As Integer = 0.1 * PANEL_HEIGHT

    Dim PICTURE_BOX_WIDTH As Integer = PANEL_WIDTH
    Dim PICTURE_BOX_HEIGHT As Integer = 0.5 * PANEL_HEIGHT
    Const PICTURE_BOX_TOP As Integer = 0
    Const PICTURE_BOX_LEFT As Integer = 0

    Dim TEXT_BOX_WIDTH As Integer = PANEL_WIDTH
    Dim TEXT_BOX_HEIGHT As Integer = 0.4 * PANEL_HEIGHT
    Dim TEXT_BOX_TOP As Integer = PICTURE_BOX_HEIGHT + PANEL_HEIGHT_MARGIN
    Dim TEXT_BOX_LEFT As Int16 = 0

    Dim IMAGE_PATH As String = "c:\temp\image1.jpg"

    Public mainPanel As New Panel
    Public subPanels As New List(Of Panel)
    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        AddHandler Me.Load, AddressOf Form1_Load

    End Sub
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load
        CalulateControlSizes(NUMBER_OF_PANELS)

        mainPanel = New Panel
        mainPanel.Height = MAIN_PANEL_HEIGHT
        mainPanel.Width = MAIN_PANEL_WIDTH
        mainPanel.Top = MAIN_PANEL_TOP
        mainPanel.Left = MAIN_PANEL_LEFT
        Me.Controls.Add(mainPanel)

        For panelNumber As Integer = 0 To (NUMBER_OF_PANELS - 1)
            Dim row As Integer = Math.Floor(panelNumber / PANEL_COLUMNS)
            Dim col As Integer = panelNumber Mod PANEL_COLUMNS

            Dim newPanel As New Panel
            newPanel.Top = row * (PANEL_HEIGHT + PANEL_HEIGHT_MARGIN)
            newPanel.Left = col * (PANEL_WIDTH + PANEL_WIDTH_MARGIN)
            newPanel.Width = PANEL_WIDTH
            newPanel.Height = PANEL_HEIGHT
            mainPanel.Controls.Add(newPanel)
            subPanels.Add(newPanel)

            Dim newPicture As New PictureBox()
            newPicture.Height = PICTURE_BOX_HEIGHT
            newPicture.Width = PICTURE_BOX_WIDTH
            newPicture.Top = PICTURE_BOX_TOP
            newPicture.Left = PICTURE_BOX_LEFT
            newPicture.Image = Image.FromFile(IMAGE_PATH)
            newPanel.Controls.Add(newPicture)

            Dim newTextBox As New TextBox
            newTextBox.Height = TEXT_BOX_HEIGHT
            newTextBox.Width = TEXT_BOX_WIDTH
            newTextBox.Top = TEXT_BOX_TOP
            newTextBox.Left = TEXT_BOX_LEFT
            newTextBox.Text = "PANEL NUMBER" + panelNumber.ToString()
            newTextBox.BackColor = SystemColors.Window
            newTextBox.ForeColor = SystemColors.WindowText

            newPanel.Controls.Add(newTextBox)

        Next panelNumber


    End Sub
    Sub CalulateControlSizes(numberOfPanels As Integer)
        PANEL_ROWS = Math.Ceiling(NUMBER_OF_PANELS / PANEL_COLUMNS)
        PANEL_HEIGHT = (MAIN_PANEL_HEIGHT / PANEL_ROWS) - PANEL_HEIGHT_MARGIN

        PICTURE_BOX_MARGIN = 0.1 * PANEL_HEIGHT

        PICTURE_BOX_HEIGHT = 0.5 * PANEL_HEIGHT

        TEXT_BOX_HEIGHT = 0.4 * PANEL_HEIGHT
        TEXT_BOX_TOP = PICTURE_BOX_HEIGHT + PANEL_HEIGHT_MARGIN

    End Sub

End Class

答案 1 :(得分:0)

在等待它们之前,我等着你理解这2个项目。现在,乐趣从一些简单的步骤开始

1)首先阅读xml

2)主面板是CubeGrid

    Public mainPanel As New CubeGrid
    Public subPanels As New List(Of CubeBlock) [you can eliminate this since it is gridsinfo.cubeBlocks]
    mainPanel = gridsinfo.

让CubeGrid继承Panel。

Public Class CubeGrid
    Inherits Panel
    Public id As String
    Public persistentFlags As String
    Public position As location
    Public forward As location
    Public up As location
    Public orientation As location
    Public cubeBlocks As List(Of CubeBlock)

    Public displayname As String
    Public enumerator As String
End Class

3)Cube Block也是一个需要继承Panel的Panel。它也应该有一个计数

Public Class CubeBlock
    Inherits Panel
    Public SubtypeName As String
    Public username As String
    Public count As Integer
End Class

4)然后,面板数量为Dim NUMBER_OF_PANELS = gridsinfo.cubeBlocks.Count。您将收到错误,因为NUMBER_OF_Panels从Const更改为Dim。所以你必须将一些Const改为Dim。

5)现在for循环变了

        For panelNumber As Integer = 0 To (NUMBER_OF_PANELS - 1)

            Dim newPanel As CubeBlock = gridsinfo.cubeBlocks(panelNumber)
        Next panelNumber

答案 2 :(得分:0)

这是完整的代码

    Imports System.Drawing
    Imports System.IO
    Imports System.Xml
    Imports System.Xml.Linq
    Public Class Form1
        Const FILENAME As String = "c:\temp\test.xml"

        Dim NUMBER_OF_PANELS = 0

        Const MAIN_PANEL_WIDTH As Integer = 1000
        Const MAIN_PANEL_HEIGHT As Integer = 1000
        Const MAIN_PANEL_TOP As Integer = 50
        Const MAIN_PANEL_LEFT As Integer = 50

        Const PANEL_COLUMNS As Integer = 5
        Const PANEL_WIDTH_MARGIN As Integer = 10
        Const PANEL_HEIGHT_MARGIN As Integer = 10

        Const PANEL_WIDTH As Integer = (MAIN_PANEL_WIDTH / PANEL_COLUMNS) - PANEL_WIDTH_MARGIN

        Dim PANEL_ROWS As Integer = 0
        Dim PANEL_HEIGHT As Integer = 0

        Dim PICTURE_BOX_MARGIN As Integer = 0

        Dim PICTURE_BOX_WIDTH As Integer = PANEL_WIDTH
        Dim PICTURE_BOX_HEIGHT As Integer = 0
        Const PICTURE_BOX_TOP As Integer = 0
        Const PICTURE_BOX_LEFT As Integer = 0

        Dim LABEL_WIDTH As Integer = PANEL_WIDTH
        Dim LABEL_HEIGHT As Integer = 0
        Dim LABEL_TOP As Integer = 0
        Dim LABEL_LEFT As Int16 = 0

        Dim IMAGE_PATH As String = "c:\temp\image1.jpg"

        Public _model As Model
        Public mainPanel As New CubeGrid
        Public subPanels As New List(Of CubeBlock)
        Sub New()

            ' This call is required by the designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.
            AddHandler Me.Load, AddressOf Form1_Load

        End Sub
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load

            _model = New Model()
            _model.Load(FILENAME)
            NUMBER_OF_PANELS = _model.print._cubeGrid.cubeBlocks.Count
            CalulateControlSizes(NUMBER_OF_PANELS)

            mainPanel = _model.print._cubeGrid
            mainPanel.Height = MAIN_PANEL_HEIGHT
            mainPanel.Width = MAIN_PANEL_WIDTH
            mainPanel.Top = MAIN_PANEL_TOP
            mainPanel.Left = MAIN_PANEL_LEFT

            mainPanel.AutoScroll = True
            mainPanel.VerticalScroll.Visible = True
            mainPanel.HorizontalScroll.Visible = True
            Me.Controls.Add(mainPanel)

            For panelNumber As Integer = 0 To (NUMBER_OF_PANELS - 1)
                Dim row As Integer = Math.Floor(panelNumber / PANEL_COLUMNS)
                Dim col As Integer = panelNumber Mod PANEL_COLUMNS

                Dim newPanel As CubeBlock = _model.print._cubeGrid.cubeBlocks(panelNumber)
                newPanel.Top = row * (PANEL_HEIGHT + PANEL_HEIGHT_MARGIN)
                newPanel.Left = col * (PANEL_WIDTH + PANEL_WIDTH_MARGIN)
                newPanel.Width = PANEL_WIDTH
                newPanel.Height = PANEL_HEIGHT
                mainPanel.Controls.Add(newPanel)
                subPanels.Add(newPanel)

                Dim newPicture As New PictureBox()
                newPicture.Height = PICTURE_BOX_HEIGHT
                newPicture.Width = PICTURE_BOX_WIDTH
                newPicture.Top = PICTURE_BOX_TOP
                newPicture.Left = PICTURE_BOX_LEFT
                newPicture.Image = Image.FromFile(IMAGE_PATH)
                newPanel.Controls.Add(newPicture)

                Dim newLabel As New Label
                newLabel.Height = LABEL_HEIGHT
                newLabel.Width = LABEL_WIDTH
                newLabel.Top = LABEL_TOP
                newLabel.Left = LABEL_LEFT
                newLabel.Text = newPanel.SubtypeName & "(" & newPanel.count & ")"
                newLabel.BackColor = SystemColors.Window
                newLabel.ForeColor = SystemColors.WindowText

                newPanel.Controls.Add(newLabel)

            Next panelNumber


        End Sub
        Sub CalulateControlSizes(numberOfPanels As Integer)
            PANEL_ROWS = Math.Ceiling(NUMBER_OF_PANELS / PANEL_COLUMNS)
            'PANEL_HEIGHT = (MAIN_PANEL_HEIGHT / PANEL_ROWS) - PANEL_HEIGHT_MARGIN
            PANEL_HEIGHT = 200

            PICTURE_BOX_MARGIN = 0.1 * PANEL_HEIGHT

            PICTURE_BOX_HEIGHT = 0.8 * PANEL_HEIGHT

            LABEL_HEIGHT = 0.1 * PANEL_HEIGHT
            LABEL_TOP = PICTURE_BOX_HEIGHT + PANEL_HEIGHT_MARGIN

        End Sub

    End Class
    Public Class Model
        Public print As Model
        Public print2 As Model
        Public print3 As Model

        Public _type As String
        Public _id As ID
        Public _display As String
        Public _display1 As String
        Public displayname As String
        Public enumerator As String
        Public _cubeGrid As CubeGrid
        Public info As String

        Public Sub Load(filename As String)

            Dim reader As New StreamReader(filename)

            Dim doc As XDocument = XDocument.Load(reader)
            Dim firstNode As XElement = doc.FirstNode
            Dim xsiNs = firstNode.GetNamespaceOfPrefix("xsi")
            Dim drawingTypes = firstNode.Elements.FirstOrDefault()
            Dim drawingType = drawingTypes.Elements.FirstOrDefault()
            Dim drawingStr = drawingType.Name.LocalName

            print = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
               ._type = x.Attribute(xsiNs + "type"),
               ._id = x.Elements("Id").Select(Function(y) New ID() With {
                                                  .type = y.Attribute("Type"),
                                                  .subtype = y.Attribute("Subtype")
                                              }).FirstOrDefault(),
               ._cubeGrid = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
                                                          .id = y.Element("EntityId"),
                                                          .persistentFlags = y.Element("PersistentFlags"),
                                                          .position = y.Descendants("Position").Select(Function(z) New location() With {
                                                            .x = CType(z.Attribute("x"), Double),
                                                            .y = CType(z.Attribute("y"), Double),
                                                            .z = CType(z.Attribute("z"), Double)
                                                          }).FirstOrDefault(),
                                                          .forward = y.Descendants("Forward").Select(Function(z) New location() With {
                                                            .x = CType(z.Attribute("x"), Double),
                                                            .y = CType(z.Attribute("y"), Double),
                                                            .z = CType(z.Attribute("z"), Double)
                                                          }).FirstOrDefault(),
                                                          .up = y.Descendants("Up").Select(Function(z) New location() With {
                                                            .x = CType(z.Attribute("x"), Double),
                                                            .y = CType(z.Attribute("y"), Double),
                                                            .z = CType(z.Attribute("z"), Double)
                                                          }).FirstOrDefault(),
                                                          .orientation = y.Descendants("Orientation").Select(Function(z) New location() With {
                                                            .w = CType(z.Element("W"), Double),
                                                            .x = CType(z.Element("X"), Double),
                                                            .y = CType(z.Element("Y"), Double),
                                                            .z = CType(z.Element("Z"), Double)
                                                          }).FirstOrDefault(),
                                                      .cubeBlocks = y.Descendants("MyObjectBuilder_CubeBlock").GroupBy(Function(z) New With {Key .subName = CType(z.Element("SubtypeName"), String), Key .userName = CType(z.Attribute(xsiNs + "type"), String)}).Select(Function(z) New CubeBlock() With {
                                                        .SubtypeName = CType(z.FirstOrDefault().Element("SubtypeName"), String),
                                                         .username = z.FirstOrDefault().Attribute(xsiNs + "type"),
                                                        .count = z.Count
                                                      }).OrderBy(Function(z) z.SubtypeName).ToList()
                                                      }).FirstOrDefault
            }).FirstOrDefault()


            print2 = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
                ._cubeGrid = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
                    .displayname = y.Element("DisplayName")
                }).FirstOrDefault()
            }).FirstOrDefault()

            print3 = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
            ._cubeGrid = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
                .enumerator = y.Element("GridSizeEnum")
                }).FirstOrDefault
            }).FirstOrDefault()
        End Sub
    End Class
    Public Class ID
        Public type As String
        Public subtype As String
    End Class
    Public Class CubeGrid
        Inherits Panel

        Public id As String
        Public persistentFlags As String
        Public gridSizeEnum As String
        Public position As location
        Public forward As location
        Public up As location
        Public orientation As location
        Public cubeBlocks As List(Of CubeBlock)

        Public displayname As String
        Public enumerator As String
    End Class
    Public Class location
        Public w As Double
        Public x As Double
        Public y As Double
        Public z As Double
    End Class
    Public Class CubeBlock
        Inherits Panel
        Public SubtypeName As String
        Public count As Integer
    End Class

答案 3 :(得分:0)

我从您的代码中删除了语法错误,并简化了查询的简化。

Imports System.Xml
Imports System.Xml.Linq
Imports System.IO
Public Class Form1
    Public FILENAME As String = String.Empty
    Public models As Model
    Public productionBlocks As New Panel
    Public pictureBlocks As New List(Of PictureBox)
    Public gridsinfo As CubeGrid

    Public dictCount As New Dictionary(Of String, Integer)
    Public dictionay As New Dictionary(Of String, String)
    Public fileloader As New OpenFileDialog

    Dim NUMBER_OF_PANELS = 0

    Const MAIN_PANEL_WIDTH As Integer = 1000
    Const MAIN_PANEL_HEIGHT As Integer = 1000
    Const MAIN_PANEL_TOP As Integer = 50
    Const MAIN_PANEL_LEFT As Integer = 50

    Const PANEL_COLUMNS As Integer = 5
    Const PANEL_WIDTH_MARGIN As Integer = 10
    Const PANEL_HEIGHT_MARGIN As Integer = 10

    Const PANEL_WIDTH As Integer = (MAIN_PANEL_WIDTH / PANEL_COLUMNS) - PANEL_WIDTH_MARGIN

    Dim PANEL_ROWS As Integer = 0
    Dim PANEL_HEIGHT As Integer = 0

    Dim PICTURE_BOX_MARGIN As Integer = 0

    Dim PICTURE_BOX_WIDTH As Integer = PANEL_WIDTH
    Dim PICTURE_BOX_HEIGHT As Integer = 0
    Const PICTURE_BOX_TOP As Integer = 0
    Const PICTURE_BOX_LEFT As Integer = 0

    Dim LABEL_WIDTH As Integer = PANEL_WIDTH
    Dim LABEL_HEIGHT As Integer = 0
    Dim LABEL_TOP As Integer = 0
    Dim LABEL_LEFT As Int16 = 0

    Dim IMAGE_PATH As String = "c:\temp\image1.jpg"

    Public _model As Model
    Public mainPanel As New CubeGrid
    Public subPanels As New List(Of CubeBlock)


    Sub New()
        ' This call is required by the designer.
        InitializeComponent()
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load
    End Sub

    Sub CalulateControlSizes(numberOfPanels As Integer)
        PANEL_ROWS = Math.Ceiling(NUMBER_OF_PANELS / PANEL_COLUMNS)
        'PANEL_HEIGHT = (MAIN_PANEL_HEIGHT / PANEL_ROWS) - PANEL_HEIGHT_MARGIN
        PANEL_HEIGHT = 200

        PICTURE_BOX_MARGIN = 0.1 * PANEL_HEIGHT

        PICTURE_BOX_HEIGHT = 0.8 * PANEL_HEIGHT

        LABEL_HEIGHT = 0.1 * PANEL_HEIGHT
        LABEL_TOP = PICTURE_BOX_HEIGHT + PANEL_HEIGHT_MARGIN

    End Sub




    'FindWords Function to get count of items
    Private Function FindWords(ByVal TextSearched As String, ByVal Paragraph As String) As Integer
        Dim location As Integer = 0
        Dim occurances As Integer = 0

        Do
            location = TextSearched.IndexOf(Paragraph, location)
            If location <> -1 Then
                occurances += 1
                location += Paragraph.Length
            End If
        Loop Until location = -1
        Return occurances
    End Function


    'Open Button
    Private Sub PictureBox1_MouseEnter(sender As System.Object, e As EventArgs) Handles PictureBox1.MouseEnter
        PictureBox1.BorderStyle = BorderStyle.Fixed3D
    End Sub

    Private Sub PictureBox1_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox1.MouseLeave
        PictureBox1.BorderStyle = BorderStyle.FixedSingle
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        'Resetting the list box when new file is loaded
        Try
            For Each item In ListBox1.Items
                ListBox1.Items.Remove(item)
            Next
        Catch ex As Exception
        End Try

        'pre-load initialization
        fileloader.ShowDialog()
        FILENAME = fileloader.FileName
        models = New Model
        models.Load(FILENAME)

        'Grabbing information from XML file
        Dim blockNames As List(Of String) = models.print.cubes.cubeBlocks.Select(Function(x) x.SubtypeName).ToList()
        dictCount = blockNames.GroupBy(Function(x) x).ToDictionary(Function(x) x.Key, Function(y) y.Count)
        Dim display As String = models.print2.cubes.displayname
        Dim gridsizeenum As String = models.print3.cubes.enumerator
        'Dim ownername As String = models.print4.Select(Function(x) x.cubes.Select(Function(y) y.ownername)).SelectMany(Function(y) y).FirstOrDefault.ToString

        'Load items into the listbox
        For Each item In dictCount
            ListBox1.Items.Add(item.ToString)
        Next item

        'a useless dictionary method [UN-USED]
        For Each key As String In dictCount.Keys.AsEnumerable()
            Dim keylist As String = "subTypeName = '{0}', count = '{1}'" + key.ToString + dictCount(key).ToString
        Next key

        'Load Text from XML to the read only boxes
        InfluenceTextBox2.Text = "Grid Name: " + display.ToString
        InfluenceTextBox3.Text = "Grid Type: " + gridsizeenum.ToString + " Ship"
        'InfluenceTextBox1.Text = "Owner Name: " + ownername.ToString



        'Gui Displaying
        Dim reader As New StreamReader(FILENAME)
        Dim doc As XDocument = XDocument.Load(reader)
        Dim xmlstring As String = doc.ToString

        LightArmorBlockText.Text = "Qty: " + FindWords(xmlstring, "LargeBlockArmorBlock").ToString
        LightArmorSlopeText.Text = "Qty: " + FindWords(xmlstring, "LargeBlockArmorSlope").ToString
        LightArmorCornerText.Text = "Qty: " + FindWords(xmlstring, "LargeBlockArmorCorner").ToString
        LightArmorCornerInvertedText.Text = "Qty: " + FindWords(xmlstring, "LargeBlockArmorInvCorner").ToString



        'Function to calculate oxygentanks and hydrogen tanks
        Dim tanks As Integer = FindWords(xmlstring, "MyObjectBuilder_OxygenTank").ToString
        Dim oxygentanks As Integer = tanks - FindWords(xmlstring, "LargeHydrogenTank").ToString
        OxygenTanksText.Text = "Qty: " + oxygentanks.ToString
        HydrogenTanksText.Text = "Qty: " + FindWords(xmlstring, "LargeHydrogenTank").ToString





        dictionay = blockNames.GroupBy(Function(x) x).ToDictionary(Function(x) x.Key, Function(y) y.Key)
        'Procedural Control Generation

        _model = New Model()
        _model.Load(FILENAME)

        NUMBER_OF_PANELS = _model.print.cubes.cubeBlocks.Count
        CalulateControlSizes(NUMBER_OF_PANELS)

        mainPanel = _model.print.cubes
        mainPanel.Height = MAIN_PANEL_HEIGHT
        mainPanel.Width = MAIN_PANEL_WIDTH
        mainPanel.Top = MAIN_PANEL_TOP
        mainPanel.Left = MAIN_PANEL_LEFT
        mainPanel.BackColor = Color.FromArgb(24, 24, 24)
        mainPanel.ForeColor = Color.White

        mainPanel.AutoScroll = True
        mainPanel.VerticalScroll.Visible = True
        mainPanel.HorizontalScroll.Visible = True
        Me.Controls.Add(mainPanel)

        For panelNumber As Integer = 0 To (NUMBER_OF_PANELS - 1)
            Dim row As Integer = Math.Floor(panelNumber / PANEL_COLUMNS)
            Dim col As Integer = panelNumber Mod PANEL_COLUMNS

            Dim newPanel As CubeBlock = _model.print.cubes.cubeBlocks(panelNumber)
            newPanel.Top = row * (PANEL_HEIGHT + PANEL_HEIGHT_MARGIN)
            newPanel.Left = col * (PANEL_WIDTH + PANEL_WIDTH_MARGIN)
            newPanel.Width = PANEL_WIDTH
            newPanel.Height = PANEL_HEIGHT
            newPanel.BackColor = Color.FromArgb(24, 24, 24)
            newPanel.ForeColor = Color.White
            mainPanel.Controls.Add(newPanel)

            subPanels.Add(newPanel)

            Dim newPicture As New PictureBox()
            newPicture.Height = PICTURE_BOX_HEIGHT
            newPicture.Width = PICTURE_BOX_WIDTH
            newPicture.Top = PICTURE_BOX_TOP
            newPicture.Left = PICTURE_BOX_LEFT
            newPicture.Image = Image.FromFile(IMAGE_PATH)
            newPicture.BackColor = Color.FromArgb(24, 24, 24)
            newPanel.Controls.Add(newPicture)

            Dim newLabel As New Label
            newLabel.Height = LABEL_HEIGHT
            newLabel.Width = LABEL_WIDTH
            newLabel.Top = LABEL_TOP
            newLabel.Left = LABEL_LEFT
            newLabel.BackColor = Color.Transparent
            newLabel.ForeColor = Color.White
            newLabel.Text = newPanel.SubtypeName & "(" & newPanel.count & ")"
            newLabel.BackColor = SystemColors.Window
            newLabel.ForeColor = SystemColors.WindowText

            newPanel.Controls.Add(newLabel)

        Next panelNumber
    End Sub
    'END OPEN BUTTON


    'Contributors Section
    Private Sub InfluenceTopButton1_Click(sender As Object, e As EventArgs) Handles InfluenceTopButton1.Click
        'Form2.Show()
    End Sub


    'XML INPUT LOADING FUNCTION
End Class
Public Class Model
    Public print As Model
    Public print2 As Model
    Public print3 As Model
    Public print4 As Model
    Public _type As String
    Public _id As ID
    Public _display As String
    Public _display1 As String
    Public displayname As String
    Public enumerator As String
    Public cubes As New CubeGrid
    Public info As String
    Public Sub Load(filename As String)
        Dim reader As New StreamReader(filename)
        Dim doc As XDocument = XDocument.Load(reader)
        Dim firstNode As XElement = doc.FirstNode
        Dim xsiNs = firstNode.GetNamespaceOfPrefix("xsi")
        Dim drawingTypes = firstNode.Elements.FirstOrDefault()
        Dim drawingType = drawingTypes.Elements.FirstOrDefault()
        Dim drawingStr = drawingType.Name.LocalName
        print = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
           ._type = x.Attribute(xsiNs + "type"), ._id = x.Elements("Id").Select(Function(y) New ID() With {
                                              .type = y.Attribute("Type"),
                                              .subtype = y.Attribute("Subtype")
                                          }).FirstOrDefault(),
           .cubes = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
                                                      .id = y.Element("EntityId"),
                                                      .persistentFlags = y.Element("PersistentFlags"),
                                                      .position = y.Descendants("Position").Select(Function(z) New location() With {
                                                        .x = CType(z.Attribute("x"), Double),
                                                        .y = CType(z.Attribute("y"), Double),
                                                        .z = CType(z.Attribute("z"), Double)
                                                      }).FirstOrDefault(),
                                                      .forward = y.Descendants("Forward").Select(Function(z) New location() With {
                                                        .x = CType(z.Attribute("x"), Double),
                                                        .y = CType(z.Attribute("y"), Double),
                                                        .z = CType(z.Attribute("z"), Double)
                                                      }).FirstOrDefault(),
                                                      .up = y.Descendants("Up").Select(Function(z) New location() With {
                                                        .x = CType(z.Attribute("x"), Double),
                                                        .y = CType(z.Attribute("y"), Double),
                                                        .z = CType(z.Attribute("z"), Double)
                                                      }).FirstOrDefault(),
                                                      .orientation = y.Descendants("Orientation").Select(Function(z) New location() With {
                                                        .w = CType(z.Element("W"), Double),
                                                        .x = CType(z.Element("X"), Double),
                                                        .y = CType(z.Element("Y"), Double),
                                                        .z = CType(z.Element("Z"), Double)
                                                      }).FirstOrDefault(),
                                                      .cubeBlocks = y.Descendants("MyObjectBuilder_CubeBlock").GroupBy(Function(z) CType(z.Element("SubtypeName"), String)).Select(Function(z) New CubeBlock() With {
                                                        .SubtypeName = z.Key,
                                                        .count = z.Count
                                                      }).OrderBy(Function(z) z.SubtypeName).ToList()
                                                  }).FirstOrDefault()
        }).FirstOrDefault



        print2 = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
            .cubes = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
                .displayname = y.Element("DisplayName")
            }).FirstOrDefault
        }).FirstOrDefault()

        print3 = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
        .cubes = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
            .enumerator = y.Element("GridSizeEnum")
            }).FirstOrDefault
        }).FirstOrDefault

        'Not Working - Gets owner name
        print4 = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
            .cubes = x.Descendants("MyObjectBuilder_ShipBlueprintDefinition").Select(Function(y) New CubeGrid() With {
                .ownername = y.Element("DisplayName")
            }).FirstOrDefault
        }).FirstOrDefault
    End Sub
End Class
Public Class ID
    Public type As String
    Public subtype As String
End Class
Public Class CubeGrid
    Inherits Panel

    Public id As String
    Public persistentFlags As String
    Public position As location
    Public forward As location
    Public up As location
    Public orientation As location
    Public cubeBlocks As List(Of CubeBlock)

    Public displayname As String
    Public ownername As String
    Public enumerator As String
End Class
Public Class location
    Public w As Double
    Public x As Double
    Public y As Double
    Public z As Double
End Class
Public Class CubeBlock
    Inherits Panel

    Public SubtypeName As String
    Public username As String
    Public count As Integer
End Class