Jump to content
andreycolli

[Resolvido] Adicionar função no botão criado por tempo de execução

Recommended Posts

andreycolli

Eu queria dar um comando pro botao para diminuir ou aumentar um valor dentro de um textbox, como eu faço isso??? Segue o código. Ele gerou os botoes os nomes dos complementos e o txt normal, a unica coisa que preciso eh fazer ele aumentar ou diminuir o valor do txt, grato

Private Sub carregadados()

        Dim dr As OleDbDataReader = Nothing
        Using con As OleDbConnection = getconnection()
            Try
                con.Open()
                Dim sql As String = "SELECT * FROM complemento"
                Dim cmd As OleDbCommand = New OleDbCommand(sql, con)
                dr = cmd.ExecuteReader
                i = 1
                While (dr.Read())
                    Dim labl As New Label
                    labl.Text = dr.Item("nome")
                    labl.AutoSize() = True
                    labl.Location = New System.Drawing.Point(100, (i * 20))
                    Me.Controls.Add(labl)

                    Dim txtqnt As New TextBox
                    txtqnt.Text = 0
                    txtqnt.Name = "txtid" & i
                    txtqnt.Size() = New System.Drawing.Size(20, 20)
                    txtqnt.Location = New System.Drawing.Point(200, (i * 20))
                    Me.Controls.Add(txtqnt)

                    Dim botaoAcres As New Button
                    botaoAcres.Text = "+"
                    botaoAcres.Name = "btnacrescid" & i
                    botaoAcres.Size() = New System.Drawing.Size(20, 20)
                    botaoAcres.Location = New System.Drawing.Point(220, (i * 20))
                    Me.Controls.Add(botaoAcres)

                    Dim botaosu As New Button
                    botaosu.Text = "-"
                    botaosu.Name = "btnsubid" & i
                    botaosu.Size() = New System.Drawing.Size(20, 20)
                    botaosu.Location = New System.Drawing.Point(180, (i * 20))
                    Me.Controls.Add(botaosu)                    
                    i = i + 1
                End While

            Catch ex As Exception
                MsgBox("Ocorreu um erro ao tentar carregar dados!", MsgBoxStyle.Information)
            Finally
                con.Close()
            End Try
        End Using

    End Sub

 

Share this post


Link to post
Share on other sites
Rechousa

Viva,

Pelo que percebi, o objetivo é: quando clicares nos botões com o texto "+" ou "-", que seja executada determinada ação, certo?

Vê o statement AddHandler:

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/addhandler-statement

Tens de criar um evento para o botão. Nesse evento, é que irás alterar o valor que precisas.

Espero ter ajudado,

Edited by Rechousa
  • Vote 1

Pedro Martins

Sharing is Knowledge!

http://www.linkedin.com/in/rechousa

Share this post


Link to post
Share on other sites
andreycolli
1 hora atrás, Rechousa disse:

Viva,

Pelo que percebi, o objetivo é: quando clicares nos botões com o texto "+" ou "-", que seja executada determinada ação, certo?

Vê o statement AddHandler:

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/addhandler-statement

Tens de criar um evento para o botão. Nesse evento, é que irás alterar o valor que precisas.

Espero ter ajudado,

Eu tentei aqui mais nao consegui, nao sou muito bom em programação, estou aprendendo ainda kkkkk, voce poderia me passar o código se nao for pedir muito, desde ja agradeço

Share this post


Link to post
Share on other sites
Rechousa

Eu não domino VB.Net mas será algo do género:

Dim botaoAcres As New Button
botaoAcres.Text = "+"
botaoAcres.Name = "btnacrescid" & i
botaoAcres.Size() = New System.Drawing.Size(20, 20)
botaoAcres.Location = New System.Drawing.Point(220, (i * 20))
' Acrescentar o AddHandler a um botão:
AddHandler botaoAcres.Click, AddressOf Me.botaoAcres_Click
Me.Controls.Add(botaoAcres)


Dim botaosu As New Button
botaosu.Text = "-"
botaosu.Name = "btnsubid" & i
botaosu.Size() = New System.Drawing.Size(20, 20)
botaosu.Location = New System.Drawing.Point(180, (i * 20))
' Acrescentar o AddHandler ao outro botão:
AddHandler botaosu.Click, AddressOf Me.botaosu_Click
Me.Controls.Add(botaosu)

Private Sub botaoAcres_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim selectedBtn As Button = sender
    MsgBox("you have clicked button " & selectedBtn.Name)
End Sub

Private Sub botaosu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim selectedBtn As Button = sender
    MsgBox("you have clicked button " & selectedBtn.Name)
End Sub

 


Pedro Martins

Sharing is Knowledge!

http://www.linkedin.com/in/rechousa

Share this post


Link to post
Share on other sites
andreycolli
1 hora atrás, Rechousa disse:

Eu não domino VB.Net mas será algo do género:


Dim botaoAcres As New Button
botaoAcres.Text = "+"
botaoAcres.Name = "btnacrescid" & i
botaoAcres.Size() = New System.Drawing.Size(20, 20)
botaoAcres.Location = New System.Drawing.Point(220, (i * 20))
' Acrescentar o AddHandler a um botão:
AddHandler botaoAcres.Click, AddressOf Me.botaoAcres_Click
Me.Controls.Add(botaoAcres)


Dim botaosu As New Button
botaosu.Text = "-"
botaosu.Name = "btnsubid" & i
botaosu.Size() = New System.Drawing.Size(20, 20)
botaosu.Location = New System.Drawing.Point(180, (i * 20))
' Acrescentar o AddHandler ao outro botão:
AddHandler botaosu.Click, AddressOf Me.botaosu_Click
Me.Controls.Add(botaosu)

Private Sub botaoAcres_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim selectedBtn As Button = sender
    MsgBox("you have clicked button " & selectedBtn.Name)
End Sub

Private Sub botaosu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim selectedBtn As Button = sender
    MsgBox("you have clicked button " & selectedBtn.Name)
End Sub

 

Eu tentei mais sem sucesso, é o seguinte, vai ter "n" botoes, labl e txt, vai depender da quantidade de complementos que estiverem cadastrados, eu quero que quando eu click no btn1 ele mude o valor dentro do txt1 que corresponde ao complemento, exemplo: complemento1 tem labl1,  bntacres1, txt1 e bntsub1, complemento2 tem lbl2 etc... Eu quero que quando eu click no bntacres1  acrescentar no txt1 +1, se click no btnacres2 acrescenta no txt2+1 assim por diante todos os txts vai vim com valor padrao 0 e quando acrescentar ele vai somando......

Edited by andreycolli

Share this post


Link to post
Share on other sites
Retsu9

Boas,

Penso que isto resolve o teu problema:

Private Sub carregadados()

        Dim dr As OleDbDataReader = Nothing
        Using con As OleDbConnection = getconnection()
            Try
                con.Open()
                Dim sql As String = "SELECT * FROM complemento"
                Dim cmd As OleDbCommand = New OleDbCommand(sql, con)
                dr = cmd.ExecuteReader
                i = 1
                While (dr.Read())

                    Dim labl As New Label
                    With labl
                        .Text = dr.Item("nome")
                        .AutoSize() = True
                        .Location = New System.Drawing.Point(100, (i * 20))
                    End With

                    Me.Controls.Add(labl)

                    Dim txtqnt As New TextBox

                    With txtqnt
                        .Text = 0
                        .Name = "txtid" & i
                        .Size() = New System.Drawing.Size(20, 20)
                        .Location = New System.Drawing.Point(200, (i * 20))
                    End With

                    Me.Controls.Add(txtqnt)

                    Dim botaoAcres As New Button
                    With botaoAcres
                        .Text = "+"
                        .Name = "btnacrescid" & i
                        .Size() = New System.Drawing.Size(20, 20)
                        .Location = New System.Drawing.Point(220, (i * 20))
                        .Tag = txtqnt.Name
                        AddHandler .Click, AddressOf botao_click
                    End With

                    Me.Controls.Add(botaoAcres)

                    Dim botaosu As New Button
                    botaosu.Text = "-"
                    botaosu.Name = "btnsubid" & i
                    botaosu.Size() = New System.Drawing.Size(20, 20)
                    botaosu.Location = New System.Drawing.Point(180, (i * 20))
                    Me.Controls.Add(botaosu)
                    i = i + 1
                End While

            Catch ex As Exception
                MsgBox("Ocorreu um erro ao tentar carregar dados!", MsgBoxStyle.Information)
            Finally
                con.Close()
            End Try
        End Using

    End Sub


    Private Sub botao_click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim s As String = sender.name ' nome do botão
        s = s(s.Length - 1) ' identificar o último caracter do nome do botão, para retirar o valor de i
                Dim A = CType(Me.Controls("txtid" & s), TextBox)
        A.Text += 1
    End Sub

 

Edited by Retsu9

Share this post


Link to post
Share on other sites
andreycolli
1 hora atrás, Retsu9 disse:

Boas,

Penso que isto resolve o teu problema:


Private Sub carregadados()

        Dim dr As OleDbDataReader = Nothing
        Using con As OleDbConnection = getconnection()
            Try
                con.Open()
                Dim sql As String = "SELECT * FROM complemento"
                Dim cmd As OleDbCommand = New OleDbCommand(sql, con)
                dr = cmd.ExecuteReader
                i = 1
                While (dr.Read())

                    Dim labl As New Label
                    With labl
                        .Text = dr.Item("nome")
                        .AutoSize() = True
                        .Location = New System.Drawing.Point(100, (i * 20))
                    End With

                    Me.Controls.Add(labl)

                    Dim txtqnt As New TextBox

                    With txtqnt
                        .Text = 0
                        .Name = "txtid" & i
                        .Size() = New System.Drawing.Size(20, 20)
                        .Location = New System.Drawing.Point(200, (i * 20))
                    End With

                    Me.Controls.Add(txtqnt)

                    Dim botaoAcres As New Button
                    With botaoAcres
                        .Text = "+"
                        .Name = "btnacrescid" & i
                        .Size() = New System.Drawing.Size(20, 20)
                        .Location = New System.Drawing.Point(220, (i * 20))
                        .Tag = txtqnt.Name
                        AddHandler .Click, AddressOf botao_click
                    End With

                    Me.Controls.Add(botaoAcres)

                    Dim botaosu As New Button
                    botaosu.Text = "-"
                    botaosu.Name = "btnsubid" & i
                    botaosu.Size() = New System.Drawing.Size(20, 20)
                    botaosu.Location = New System.Drawing.Point(180, (i * 20))
                    Me.Controls.Add(botaosu)
                    i = i + 1
                End While

            Catch ex As Exception
                MsgBox("Ocorreu um erro ao tentar carregar dados!", MsgBoxStyle.Information)
            Finally
                con.Close()
            End Try
        End Using

    End Sub


    Private Sub botao_click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim s As String = sender.name ' nome do botão
        s = s(s.Length - 1) ' identificar o último caracter do nome do botão, para retirar o valor de i
                Dim A = CType(Me.Controls("txtid" & s), TextBox)
        A.Text += 1
    End Sub

 

 

1 hora atrás, Retsu9 disse:

Boas,

Penso que isto resolve o teu problema:


Private Sub carregadados()

        Dim dr As OleDbDataReader = Nothing
        Using con As OleDbConnection = getconnection()
            Try
                con.Open()
                Dim sql As String = "SELECT * FROM complemento"
                Dim cmd As OleDbCommand = New OleDbCommand(sql, con)
                dr = cmd.ExecuteReader
                i = 1
                While (dr.Read())

                    Dim labl As New Label
                    With labl
                        .Text = dr.Item("nome")
                        .AutoSize() = True
                        .Location = New System.Drawing.Point(100, (i * 20))
                    End With

                    Me.Controls.Add(labl)

                    Dim txtqnt As New TextBox

                    With txtqnt
                        .Text = 0
                        .Name = "txtid" & i
                        .Size() = New System.Drawing.Size(20, 20)
                        .Location = New System.Drawing.Point(200, (i * 20))
                    End With

                    Me.Controls.Add(txtqnt)

                    Dim botaoAcres As New Button
                    With botaoAcres
                        .Text = "+"
                        .Name = "btnacrescid" & i
                        .Size() = New System.Drawing.Size(20, 20)
                        .Location = New System.Drawing.Point(220, (i * 20))
                        .Tag = txtqnt.Name
                        AddHandler .Click, AddressOf botao_click
                    End With

                    Me.Controls.Add(botaoAcres)

                    Dim botaosu As New Button
                    botaosu.Text = "-"
                    botaosu.Name = "btnsubid" & i
                    botaosu.Size() = New System.Drawing.Size(20, 20)
                    botaosu.Location = New System.Drawing.Point(180, (i * 20))
                    Me.Controls.Add(botaosu)
                    i = i + 1
                End While

            Catch ex As Exception
                MsgBox("Ocorreu um erro ao tentar carregar dados!", MsgBoxStyle.Information)
            Finally
                con.Close()
            End Try
        End Using

    End Sub


    Private Sub botao_click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim s As String = sender.name ' nome do botão
        s = s(s.Length - 1) ' identificar o último caracter do nome do botão, para retirar o valor de i
                Dim A = CType(Me.Controls("txtid" & s), TextBox)
        A.Text += 1
    End Sub

 

Obrigado Retsu9, é exatamente isso que eu precisava, muito obrigado

Share this post


Link to post
Share on other sites
Retsu9

Deverá existir outros métodos de o fazer, pelo que não sei se essa é a mais optimizada.

Deverás também entender o que foi feito, fiz dessa forma porque tu identificas os objectos com a variável 'i', caso mudes o método de dar nomes aos objectos deverás modificar o procedimento do click do botão em conformidade.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.