none
想请教有关代一些代码的问题 RRS feed

  • 问题

  • 我篇写了有关add 2 张 picture的program。

    每当user每次更新,新的picture 时都会delete旧的backup picture。

    我想请教的是如果用户只更新其中一个picture的话,program也不会删除没有被更新的backup 照片。

    以下是我的代码

    Private Sub Picture_Path()
         Dim FileToDelete As String

            FileToDelete = txtPL1.Text

            If System.IO.File.Exists(FileToDelete) = True Then

                System.IO.File.Delete(FileToDelete)

            End If

            FileToDelete = txtPL2.Text

            If System.IO.File.Exists(FileToDelete) = True Then

                System.IO.File.Delete(FileToDelete)

            End If

        End Sub

    每当user 有browse picture txtPictureName1.text 和 txtPictureName2.text 都会pop picture 的path

         Dim opf As New OpenFileDialog

            opf.Filter = "Choose Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif"

            If opf.ShowDialog = DialogResult.OK Then

                PictureBox1.Image = Image.FromFile(opf.FileName)

                txtPictureName1.Text = opf.FileName
            End If

       Dim opf As New OpenFileDialog
            opf.Filter = "Choose Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif"
            If opf.ShowDialog = DialogResult.OK Then
                If opf.FileName <> "" Then
                    picPath = opf.FileName
                    txtPictureName2.Text = opf.FileName
                    Frm_Picture.PictureBox2.Image = Image.FromFile(opf.FileName)
                End If
            End If



    2020年7月29日 8:14

答案

  • Hi christing,

    你需要修改 update 方法中的代码,参考以下代码进行修改:

    If PictureBox1.Image IsNot Nothing Or picPath IsNot Nothing Then If PictureBox1.Image Is Nothing Then If System.IO.File.Exists(txtPL1.Text) = True Then System.IO.File.Delete(txtPL1.Text)

    '只更新第二张图片 End If controltextbox1() '... ElseIf picPath Is Nothing Then If System.IO.File.Exists(txtPL2.Text) = True Then System.IO.File.Delete(txtPL2.Text)

    ‘只更新第一张图片 End If controltextbox1() '... Else Picture_Path() controltextbox1()

    '同时更新两张图片 '... End If Else cmd.CommandText = "UPDATE DATA STRING" End If cmd.Parameters.AddWithValue("@PRODUCT_ID", Me.Product.Text)

    希望可以帮助你解决问题。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2020年8月4日 6:14
    版主

全部回复

  • Hi christing,

    能否提供你是如何 add 以及 update picture的代码,这样有助于我们重现你的问题并进行测试。

    期待你的更新。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年7月30日 1:45
    版主
  • Sub Picture_Path()代码是将txtPL1.Text、txtPL2.Text两个文件一起删除;

    下部代码 If opf.ShowDialog = DialogResult.OK  then情况处理方法,else情况没有处理方法。

    2020年7月30日 7:59
  • @xing yu zhao 

    感谢你的回复

    以下是我add data 的代码

      Private Sub controltextbox()
            Dim strtime As String
            Dim strdate As String

            strtime = Format(Now, "mm-ss")
            strdate = Format(Now, "dd-MM-yyyy")

            If PictureBox1.Image Is Nothing Then
                txtPL1.Text = ""
            ElseIf PictureBox1.Image IsNot Nothing Then
                txtPictureName1.Text = strtime + strdate
                txtPL1.Text = strCurrPath + "\Picture0\" + txtPictureName1.Text + ".jpg"

            End If

            If picPath Is Nothing Then
                txtPL2.Text = ""
            ElseIf picPath IsNot Nothing Then
                txtPictureName2.Text = strtime + strdate
                txtPL2.Text = strCurrPath + "\Picture1\" + txtPictureName2.Text + ".jpg"
                txtVOLTAGESPIKE.Text = txtPL2.Text

            End If
            Exit Sub
        End Sub

                Dim folderP As String = strCurrPath + "\Picture0"
                If Not Directory.Exists(folderP) Then
                    Directory.CreateDirectory(folderP)
                End If

                Dim folderPIC As String = strCurrPath + "\Picture1"
                If Not Directory.Exists(folderPIC) Then
                    Directory.CreateDirectory(folderPIC)
                End If

                controltextbox()

                If PictureBox1.Image IsNot Nothing Or picPath IsNot Nothing Then

                    If PictureBox1.Image Is Nothing Then
                        Frm_Picture.PictureBox2.Image.Save(strCurrPath + "\Picture1\" + txtPictureName2.Text)

                        cmd.CommandText = "INSERT INTO STRING"

                        Dim DataPic_Update As Byte() = ImageToBytes(Image.FromFile(picPath), ImageFormat.Png)
                        cmd.Parameters.AddWithValue("@VOLTAGE_SPIKE", DataPic_Update)

                    ElseIf picPath Is Nothing Then

                        PictureBox1.Image.Save(strCurrPath + "\Picture0\" + txtPictureName1.Text + ".jpg")

                        cmd.CommandText = "INSERT INTO STRING"

                        Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                        cmd.Parameters.AddWithValue("@PRODUCT_PICTURE", DataPic_Update)
                    Else
                        cmd.CommandText =  "INSERT INTO STRING"

      Frm_Picture.PictureBox2.Image.Save(strCurrPath + "\Picture1\" + txtPictureName2.Text + ".jpg")
                        PictureBox1.Image.Save(strCurrPath + "\Picture0\" + txtPictureName1.Text + ".jpg")

                        Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                        Dim DataPic_Update1 As Byte() = ImageToBytes(Image.FromFile(picPath), ImageFormat.Png)
                        cmd.Parameters.AddWithValue("@PRODUCT_PICTURE", DataPic_Update)
                        cmd.Parameters.AddWithValue("@VOLTAGE_SPIKE", DataPic_Update1)
                    End If

                Else
                    cmd.CommandText = "INSERT INTO STRING"

    END IF

      cmd.Parameters.AddWithValue("@PRODUCT_ID", Me.Product.Text)

    ETC

    UPDATE DATA

        If PictureBox1.Image IsNot Nothing Or picPath IsNot Nothing Then
                    If PictureBox1.Image Is Nothing Then

                        Picture_Path()
                        controltextbox1()

                        Frm_Picture.PictureBox2.Image.Save(strCurrPath + "\Picture1\" + txtPictureName2.Text + ".jpg")

                        cmd.CommandText = "UPDATE DATA STRING"

      Dim DataPic_Update As Byte() = ImageToBytes(Image.FromFile(picPath), ImageFormat.Png)
                        cmd.Parameters.AddWithValue("@VOLTAGE_SPIKE", DataPic_Update)

                    ElseIf picPath Is Nothing Then

                        Picture_Path()
                        controltextbox1()

                        PictureBox1.Image.Save(strCurrPath + "\Picture0\" + txtPictureName1.Text + ".jpg")

                        cmd.CommandText = "UPDATE DATA STRING"

       Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                        cmd.Parameters.AddWithValue("@PRODUCT_PICTURE", DataPic_Update)
                    Else
                        Picture_Path()
                        controltextbox1()


                        Frm_Picture.PictureBox2.Image.Save(strCurrPath + "\Picture1\" + txtPictureName2.Text + ".jpg")
                        PictureBox1.Image.Save(strCurrPath + "\Picture0\" + txtPictureName1.Text + ".jpg")

                        cmd.CommandText =  "UPDATE DATA STRING"

      Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                        Dim DataPic_Update1 As Byte() = ImageToBytes(Image.FromFile(picPath), ImageFormat.Png)

                        cmd.Parameters.AddWithValue("@PRODUCT_PICTURE", DataPic_Update)
                        cmd.Parameters.AddWithValue("@picture1", DataPic_Update1)
                    End If
                Else
                    cmd.CommandText =  "UPDATE DATA STRING"

     End If
                cmd.Parameters.AddWithValue("@PRODUCT_ID", Me.Product.Text)

    ETC

     Private Sub controltextbox1()
            Dim strtime As String
            Dim strdate As String

            strtime = Format(Now, "mm-ss")
            strdate = Format(Now, "dd-MM-yyyy")

            If PictureBox1.Image Is Nothing Then
                txtPL1.Text = ""
            ElseIf PictureBox1.Image IsNot Nothing Then
                txtPictureName1.Text = strtime + strdate
                txtPL1.Text = strCurrPath + "\Picture0\" + txtPictureName1.Text + ".jpg"


            End If

            If picPath Is Nothing Then
                txtPL2.Text = ""
            ElseIf picPath IsNot Nothing Then
                txtPictureName2.Text = strtime + strdate
                txtPL2.Text = strCurrPath + "\Picture1\" + txtPictureName2.Text + ".jpg"
                txtVOLTAGESPIKE.Text = txtPL2.Text

            End If
            Exit Sub
        End Sub
    2020年8月4日 1:24
  • Hi christing,

    你需要修改 update 方法中的代码,参考以下代码进行修改:

    If PictureBox1.Image IsNot Nothing Or picPath IsNot Nothing Then If PictureBox1.Image Is Nothing Then If System.IO.File.Exists(txtPL1.Text) = True Then System.IO.File.Delete(txtPL1.Text)

    '只更新第二张图片 End If controltextbox1() '... ElseIf picPath Is Nothing Then If System.IO.File.Exists(txtPL2.Text) = True Then System.IO.File.Delete(txtPL2.Text)

    ‘只更新第一张图片 End If controltextbox1() '... Else Picture_Path() controltextbox1()

    '同时更新两张图片 '... End If Else cmd.CommandText = "UPDATE DATA STRING" End If cmd.Parameters.AddWithValue("@PRODUCT_ID", Me.Product.Text)

    希望可以帮助你解决问题。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2020年8月4日 6:14
    版主
  • @xing yu zhao 感谢你的回复

    我的program能让user add 两张图,

    我发现代码会跳过

      ElseIf picPath Is Nothing Then 的part 想请帮忙看看有关代码

    (当user只update 第二张图时 ElseIf picPath Is Nothing Then 这段代码会跳过)

      Private Sub Update_Data()

            Dim strdate As String
            strdate = Format(Now, "dd-MM-yyyy")

            cmd.Parameters.Clear()

            If Not cnn.State = ConnectionState.Open Then
                cnn.Open()
            End If

            cmd.Connection = cnn

            textbox_control1()

            If valid12 = False Then
                MsgBox("Fail No1, No2")
            ElseIf valid34 = False Then
                MsgBox("Fail No3, No4")
            ElseIf valid56 = False Then
                MsgBox("Fail No5, No6")
            ElseIf valid78 = False Then
                MsgBox("Fail No7, No8")
            ElseIf valid910 = False Then
                MsgBox("Fail No9, No10")
            ElseIf valid1112 = False Then
                MsgBox("Fail No11, No12")
            ElseIf valid1314 = False Then
                MsgBox("Fail No13, No14")
            ElseIf valid1516 = False Then
                MsgBox("Fail No15, No16")
            End If

            Dim folderP As String = strCurrPath + "\Picture0"
            If Not Directory.Exists(folderP) Then
                Directory.CreateDirectory(folderP)
            End If

            Dim folderPIC As String = strCurrPath + "\Picture1"
            If Not Directory.Exists(folderPIC) Then
                Directory.CreateDirectory(folderPIC)
            End If

            If PictureBox1.Image IsNot Nothing Or picPath IsNot Nothing Then

                If PictureBox1.Image Is Nothing Then

                    If System.IO.File.Exists(txtPictureName2.Text) = True Then
                        System.IO.File.Delete(txtVOLTAGESPIKE.Text)
                    End If

                    controltextbox1()

                    Frm_Picture.PictureBox2.Image.Save(strCurrPath + "\Picture1\" + txtPictureName2.Text + ".jpg")

                    cmd.CommandText = "UPDATE DATA STRING"

                    Dim DataPic_Update As Byte() = ImageToBytes(Image.FromFile(picPath), ImageFormat.Png)
                    cmd.Parameters.AddWithValue("@VOLTAGE_SPIKE", DataPic_Update)

     ElseIf picPath Is Nothing Then

                    If System.IO.File.Exists(txtPictureName1.Text) = True Then
                        System.IO.File.Delete(txtPL1.Text)
                    End If

                    controltextbox1()

                    PictureBox1.Image.Save(strCurrPath + "\Picture0\" + txtPictureName1.Text + ".jpg")

                    cmd.CommandText = "UPDATE DATA STRING"
                    Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                    cmd.Parameters.AddWithValue("@PRODUCT_PICTURE", DataPic_Update)

                Else

      If System.IO.File.Exists(txtPictureName1.Text) = True Then
                        System.IO.File.Delete(txtPL1.Text)
                    End If

                    If System.IO.File.Exists(txtPictureName2.Text) = True Then
                        System.IO.File.Delete(txtVOLTAGESPIKE.Text)
                    End If

                    controltextbox1()

                    PictureBox1.Image.Save(strCurrPath + "\Picture0\" + txtPictureName1.Text + ".jpg")
                    Frm_Picture.PictureBox2.Image.Save(strCurrPath + "\Picture1\" + txtPictureName2.Text + ".jpg")

       Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                    Dim DataPic_Update1 As Byte() = ImageToBytes(Image.FromFile(picPath), ImageFormat.Png)
                    cmd.Parameters.AddWithValue("@PRODUCT_PICTURE", DataPic_Update)
                    cmd.Parameters.AddWithValue("@VOLTAGE_SPIKE", DataPic_Update1)
                End If

            Else
                cmd.CommandText =  "UPDATE DATA STRING"

            End If
            cmd.Parameters.AddWithValue("@PRODUCT_ID", Me.Product.Text)

        cmd.ExecuteNonQuery()
            RefreshData()
            MsgBox("Update Data Successful", MsgBoxStyle.OkOnly, "Message")


        End Sub

    感恩


    2020年8月5日 3:09
  • Hi christing,

    当你设置断点进行调试时,如果程序能够满足 ‘PictureBox1.Image IsNot Nothing Or picPath IsNot Nothing’ 的条件,并且不满足 'picPath Is Nothing'的条件,证明在从文件夹选择图片时,你有选择第二张图片,因此 picPath具有值。

    我更新了之前的代码,也许可以帮助你更直观的理解。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年8月5日 7:25
    版主