更改按钮的名称

时间:2016-06-07 09:07:58

标签: vb.net button

所以我试图进行验证,例如

如果用户将一个文件放入按钮并且它已经存在于数据库中,它将显示一条消息("此文件已经存在")并且它应该从文件名重命名按钮名称到它的实际名称。

这是我在将文件放入按钮时调用的方法,它将更改并显示我想要的内容。

Private Sub UpdateControls()
        SQLCon = New SqlConnection
        SQLCon.ConnectionString = "........."

        Dim query As String
        query = "SELECT Filename 
                FROM infofile
                WHERE Filename=@Filename"

        If cmdEntrar.Text = query Then
            cmdEntrar.Text = "Open file"
        Else
            cmdEntrar.Text = MeuFicheiro.Name
            cmdEntrar.Width = GetButtonSize(MeuFicheiro.Name)

            lblArraste.Left = cmdEntrar.Left + cmdEntrar.Width + 15
        End If
    End Sub

因为我有一个Public Sub,我称之为AddFile,在那里我将从文件(名称,扩展名等)中添加一些东西到我的数据库中。但是,如果我添加一个已存在的文件,它将清除布局中的所有标签,并将按钮的名称更改为原始文件。但那部分不起作用。

这是进行验证的代码段

 Public Sub AddFile(Filename As String, Filetype As String, Filesize As String, Created As Date, Modified As Date, Access As Date, PcName As String)
    SQLCon = New SqlConnection
    SQLCon.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\MIGG-PC\Documents\Visual Studio 2015\Projects\TrashCollection\TrashCollection\TrashDB.mdf;Integrated Security=True;Connect Timeout=30"
    Try
        Dim strInsert As String = "INSERT INTO INFOFILE (Filename,Filetype,Filesize,Created,Modified,Access,PcName) " &
                                  "VALUES (" &
                                  "'" & Filename & "'," &
                                  "'" & Filetype & "'," &
                                  "'" & Filesize & "'," &
                                  "'" & Access & "'," &
                                  "'" & Created & "'," &
                                  "'" & Modified & "'," &
                                  "'" & PcName & "')"
        SQLCon.Open()
        SqlCmd = New SqlCommand(strInsert, SQLCon)

        SqlCmd.ExecuteNonQuery()

        SQLCon.Close()
    Catch ex As Exception
        MsgBox("Ficheiro existente!")
        pathTextBox.Clear()
        lblName.Text = String.Empty
        lblType.Text = String.Empty
        lblSize.Text = String.Empty
        lblCreated.Text = String.Empty
        lblModify.Text = String.Empty
        lblAccess.Text = String.Empty
        lblKB.Text = String.Empty
        nomePcLbl.Text = String.Empty
        pbIcon.Image = Nothing
        pbGreenDot.Image = Nothing
        pbRedDot.Image = Nothing
    End Try
End Sub

我想在此例外中添加将按钮文本更改为原始文本的可能性

任何解决方案?在此先感谢..

1 个答案:

答案 0 :(得分:1)

您应该操纵清除控件文本的page load

  

如果您共享代码会更有用。