【问题标题】:Read and write below a precise cell in a CSV在 CSV 中的精确单元格下方读取和写入
【发布时间】:2021-05-13 05:00:25
【问题描述】:

我遇到了一个基本问题。我想要的是通过 CSV 进行解析,以便比较一些字符串,如果找到的话,写在下面。

确切地说,我有一个程序,我可以在其中拖放一些按钮,当我放下这个按钮时,我想将它的新位置保存在相应列下方的第一个空单元格上。 这是我的 CSV 示例:

所以我从我的 CSV 中对 .x/.y 进行子串化,并在 textFieldParser 的帮助下将下拉按钮中的名称与每个单元格进行比较。当它找到一个相等的表达式时,我的循环似乎停止工作了。

但这是我不知道该怎么说的问题,我的程序写在它下面。我能弄清楚的第一个原因是因为我的解析器一直运行到 endOfData 并且我希望它一直运行到 endOfDat + 一行。 第二个是因为我不知道是否可以在 textFieldParser 中使用 fieldwriter,我的意思是我尝试使用 row+1 创建一个变量并写在下面,但是当我使用 fileWriter 时没有任何反应。

现在是我的代码示例:

    Private Sub manageCsv(ByVal sender As Button)

        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("..\..\Pic\csvPic.csv")

            MyReader.TextFieldType = FileIO.FieldType.Delimited
            MyReader.SetDelimiters(",")

            Dim currentRow As String()
            Dim rowPlusOne As String()
            While Not MyReader.EndOfData
                Try
                    
                    currentRow = MyReader.ReadFields()
                    rowPlusUn = MyReader.ReadFields()

                    Dim currentField As String

                    
                    Dim str As String = btnSender.Name.Substring(3)
                    Dim nameDelimited As String
                    Dim x As Integer


                    For Each currentField In currentRow

                        ''Search the corresponding field''
                        x = InStr(currentField, ".")
                        If Not (currentField.Equals("imagefile")) Then ''imagefile is the first index of my csv''
                            nameDelimited = currentField.Substring(0, x) ''substr the extension''
                            If nameDelimited.Equals(str) Then
                                writeCsv("..\..\Image\csvPic.csv", nameDelimited, ",")
                                ''Ofc the "+1" does not work but that was the idea''
                                currentRow(+1) = lblImgName.Text
                                currentRow(+1) = btnSender.Location.ToString 
                                Exit For
                            End If
                        End If
                    Next
                Catch ex As _
        Microsoft.VisualBasic.FileIO.MalformedLineException
                    MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
                End Try
            End While
        End Using
    End Sub

我希望它足够清楚,如果不是,我会尝试详细说明。感谢您的帮助

【问题讨论】:

  • 为什么是 csv 文件?有更简单的方法可以做到这一点。
  • 这是我老师的指示,我不知道他为什么要那样精确....但是很痛苦。
  • 列标题是按钮 .Name(或 .Text)加上 X 和 Y 坐标吗?目的是在表单关闭后重新创建位置吗?
  • 是的,就是这样。是的,它应该是一个迷你 faceApp,所以我们从几个 img 保存和加载位置

标签: vb.net csv


【解决方案1】:

向您的老师展示使用简单的文本文件有更好的方法来做到这一点。仅当之前在应用程序中移动过按钮时,该文件才会存在。见行内 cmets。

Private ButtonLocation As New Dictionary(Of Button, Point)
Private MouseIsDown As Boolean
Private ptX, ptY As Integer 'Starting point of mouse relative to the button
Private btn As Button 'The button being moved
Private ButtonPath As String = "C:\Users\maryo\Desktop\Code\DroppedButtons.txt"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Reposition the buttons to where they were dropped in the previous session.
    If File.Exists(ButtonPath) Then
        Dim lines = File.ReadAllLines(ButtonPath) 'returns an array of strings (each line)
        For Each line In lines 'loop though each line in the file
            Dim fields = line.Split(","c) 'The three values on the line are separated by a comma
            Dim b = DirectCast(Controls(fields(0)), Button) 'Change the string Button.Name
            'to an actual Button object by finding it in the controls collection
            'Set the location with the next 2 values on the line
            b.Location = New Point(CInt(fields(1)), CInt(fields(2)))
            'Add the Button and Location to the list
            ButtonLocation.Add(b, b.Location)
        Next
    End If
End Sub
'These three Event procedures are the normal code to Drag and Drop a control
Private Sub Button_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown, Button2.MouseDown
    btn = DirectCast(sender, Button)
    ptX = e.Location.X
    ptY = e.Location.Y
    MouseIsDown = True
End Sub

Private Sub Button_MouseMove(sender As Object, e As MouseEventArgs) Handles Button1.MouseMove, Button2.MouseMove
    If MouseIsDown Then
        'e.X and e.Y are the coordinates of the Mouse relative to the control (the Button)
        'not the Form or the Screen. 
        btn.Location = New Point(btn.Location.X + e.X - ptX, btn.Location.Y + e.Y - ptY)
    End If
End Sub

Private Sub Button_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp, Button2.MouseUp
    MouseIsDown = False
    'When we drop the button with the MouseUp event we record the new location in the list
    RecordButtonLocation(btn, btn.Location)
    btn = Nothing
End Sub

Private Sub RecordButtonLocation(Sender As Button, Location As Point)
    'Check if the Button is already in the list
    If ButtonLocation.ContainsKey(Sender) Then
        'Record its new location
        ButtonLocation.Item(Sender) = Location
    Else
        'If it is not in the list add it.
        ButtonLocation.Add(Sender, Location)
    End If
End Sub

Private Sub Form1_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
    SaveDictionary()
End Sub

Private Sub SaveDictionary()
    If ButtonLocation.Count > 0 Then
        'If there is anything in the list we will create or overwrite the file
        Dim sb As New StringBuilder
        For Each kv As KeyValuePair(Of Button, Point) In ButtonLocation
            sb.AppendLine($"{kv.Key.Name},{kv.Value.X},{kv.Value.Y}")
        Next
        File.WriteAllText(ButtonPath, sb.ToString)
    End If
End Sub

【讨论】:

  • 非常感谢!这比我之前使用 3 textFieldParser 和 2 switch 的尝试要好得多......现在看来我可以只保存一堆位置,因为 writeAllText 所以我正在尝试修改你的代码以从名称中保存几个位置那里的形象。无论如何,你给我一些更清洁的东西,谢谢!
  • @Alex 每次移动按钮时,新位置都会在 MouseUp 事件发生时保存到 ButtonList。整个列表在表单关闭时保存。您确定每个鼠标事件的 Handles 子句都包含所有涉及的按钮吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多