ricduarte Posted June 24, 2013 at 10:54 AM Report #514685 Posted June 24, 2013 at 10:54 AM Boas! Tenho mais um problema! Na minha PAP eu pretendo fazer o cálculo de várias textboxs em que algumas delas estão vazias e não tou a conseguir fazer o calculo. Tenho 3 textboxs e apenas 1 tá preenchida e depois numa 4ª textbox queria que aparecesse o resultado da soma das 3 textboxs só que depois, quando clico na 4ª textbox para o resultado aparecer, ele dá-me o seguinte erro: "Conversion from string "" to type 'Integer' is not valid."
Andrepereira9 Posted June 24, 2013 at 11:06 AM Report #514692 Posted June 24, 2013 at 11:06 AM Boas Para começar devias de ter posto aqui o codigo que tens Dim aux As Double = 0 If IsNumeric(TextBox1.Text) Then aux += TextBox1.Text End If If IsNumeric(TextBox2.Text) Then aux += TextBox2.Text End If If IsNumeric(TextBox3.Text) Then aux += TextBox3.Text End If TextBox4.Text = aux.ToString A informática chegou para resolver problemas que antes não existiam Quem ri por último é porque está conectado a 52 Kbs.
ricduarte Posted June 24, 2013 at 11:31 AM Author Report #514698 Posted June 24, 2013 at 11:31 AM Muito obrigada pla ajuda Andrepereira9! Funcionou na perfeição!!!
bioshock Posted June 24, 2013 at 06:00 PM Report #514799 Posted June 24, 2013 at 06:00 PM Imaginando que tens 100 textboxes, não podes, de maneira alguma, andar a fazer 100 condições (If's). Fazes um ciclo que te fica mais barato. Por exemplo, se eventualmente essas textboxes estivessem dentro de uma groupbox, percorrias os seus controlos: For Each ctrl As Control In GroupBox1.Controls If TypeOf ctrl Is TextBox Then if IsNumeric(ctrl)Then aux += ctrl.Text End If End If Next
rochinha75 Posted June 26, 2013 at 02:02 AM Report #515147 Posted June 26, 2013 at 02:02 AM Imaginando que tens 100 textboxes, não podes, de maneira alguma, andar a fazer 100 condições (If's). Fazes um ciclo que te fica mais barato. Por exemplo, se eventualmente essas textboxes estivessem dentro de uma groupbox, percorrias os seus controlos: For Each ctrl As Control In GroupBox1.Controls If TypeOf ctrl Is TextBox Then if IsNumeric(ctrl)Then aux += ctrl.Text End If End If Next Boas, estou com uma dúvida do género... No caso dessas checkbox não estarem numa groupbox, como poderei fazer? Dim aux As Double = 0 for x as integer = 1 to 3 If IsNumeric(TextBox(x).Text) Then aux += TextBox(x).Text End If TextBox4.Text = aux.ToString Já experimentei algo deste género, com (), ' ' , " ", [ ] , { } , mas não funciona...
nelsonr Posted June 26, 2013 at 08:59 AM Report #515167 Posted June 26, 2013 at 08:59 AM Se não tiveres grupo, podes ir buscar à form
rochinha75 Posted June 26, 2013 at 09:46 AM Report #515191 Posted June 26, 2013 at 09:46 AM Peço desculpa, mas continuo sem conseguir perceber como poderei fazer... Por exemplo: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click For x As Integer = 1 To 5 If CheckBox(x).Checked = True Then MsgBox(CheckBox(x).Text) Else Next End Sub Da-me sempre erros, diz que checkbox is a type and cannot be used as na expression...
nelsonr Posted June 26, 2013 at 09:53 AM Report #515195 Posted June 26, 2013 at 09:53 AM Não tenho o VB aqui, mas experimenta algo do tipo: For Each ctrl As Control In Me.Controls If TypeOf ctrl Is CheckBox Then If DirectCast(ctrl, CheckBox).Checked = True Then MsgBox(DirectCast(ctrl, CheckBox).Text) End If End If Next
rochinha75 Posted June 26, 2013 at 10:41 AM Report #515234 Posted June 26, 2013 at 10:41 AM (edited) Esse código não dá nenhum erro, mas não consigo com que ele faça o que eu pretendo... Vou passar a explicar mesmo o que pretendo: Public Class Form1 Public tabela(3, 2) As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If CheckBox1.Checked = True Then tabela(1, 1) = Label1.Text Else tabela(1, 1) = "" End If If CheckBox2.Checked = True Then tabela(2, 1) = Label2.Text Else tabela(2, 1) = "" End If If CheckBox3.Checked = True Then tabela(3, 1) = Label3.Text Else tabela(3, 1) = "" End If End Sub em vez de fazer tudo á mão, porque vou necessitar de fazer mais de 100 checkbox pretendia fazer um ciclo for como por exemplo: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click For x As Integer = 1 To 3 If CheckBoxX.Checked = True Then tabela(X, 1) = LabelX.Text Else tabela(X, 1) = "" End If Next End sub o problema é que o programa não me está a aceitar o X em checkbox nem em label, já na tabela não há qualquer problema... Edited June 26, 2013 at 10:43 AM by rochinha75
Andrepereira9 Posted June 26, 2013 at 10:56 AM Report #515239 Posted June 26, 2013 at 10:56 AM (edited) Boas Public tabela(3, 2) As String Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim i As Integer = 1 For Each ctrl As Control In Me.Controls If TypeOf ctrl Is CheckBox Then If DirectCast(ctrl, CheckBox).Checked = True Then MsgBox("A CheckBox " & ctrl.Name & " está selecionada e o texto é " & ctrl.Text) tabela(i, 1) = ctrl.Text Else tabela(i, 1) = "" End If i += 1 End If Next For n As Integer = 1 To i - 1 MsgBox(tabela(n, 1).ToString) Next End Sub Edited June 26, 2013 at 10:56 AM by Andrepereira9 A informática chegou para resolver problemas que antes não existiam Quem ri por último é porque está conectado a 52 Kbs.
rochinha75 Posted June 26, 2013 at 11:44 AM Report #515254 Posted June 26, 2013 at 11:44 AM Com este código ele não faz nada... Não existe nenhuma maneira de fazer tipo checkbox(x) ou checkbox"x", algo deste género e para cada x corresponder a uma checkbox?
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