andreycolli Posted February 20, 2018 at 11:07 PM Report #609430 Posted February 20, 2018 at 11:07 PM 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
Rechousa Posted February 20, 2018 at 11:54 PM Report #609431 Posted February 20, 2018 at 11:54 PM (edited) 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 February 20, 2018 at 11:54 PM by Rechousa 1 Report Pedro Martins Sharing is Knowledge! http://www.linkedin.com/in/rechousa
andreycolli Posted February 21, 2018 at 02:15 AM Author Report #609432 Posted February 21, 2018 at 02:15 AM 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
Rechousa Posted February 21, 2018 at 02:21 AM Report #609433 Posted February 21, 2018 at 02:21 AM 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
andreycolli Posted February 21, 2018 at 03:59 AM Author Report #609435 Posted February 21, 2018 at 03:59 AM (edited) 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 February 21, 2018 at 04:01 AM by andreycolli
Solution Retsu9 Posted February 21, 2018 at 10:58 AM Solution Report #609436 Posted February 21, 2018 at 10:58 AM (edited) 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 February 21, 2018 at 10:59 AM by Retsu9
andreycolli Posted February 21, 2018 at 12:58 PM Author Report #609440 Posted February 21, 2018 at 12:58 PM 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
Retsu9 Posted February 21, 2018 at 01:55 PM Report #609442 Posted February 21, 2018 at 01:55 PM 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now