Ir ao conteúdo
  • Cadastre-se

sou novato em linguagem de programação em geral.


Nathanofr

Posts recomendados

Me ajudem!

sou novo nesse mundo e gostaria de aprender sobre programação.

iniciei em visual studio 2010. Criei uma calculadora ela não apresenta erros mais levei um tempo para criar os códigos por não saber muito. A minha duvida é tem como diminuir esses códigos usando variáveis ou sei la outra forma. Os códigos da calculadora são esses:


'TextBox1 é o primeiro valor
'TextBox2 é o segundo valor
'TextBox3 é o resultado do TextBox1 e TextBox2
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
TextBox1.Visible = True
TextBox2.Visible = False
TextBox1.Text = TextBox1.Text & "00"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
TextBox1.Visible = True
TextBox2.Visible = False
TextBox1.Text = TextBox1.Text & "0"
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
TextBox1.Visible = True
TextBox2.Visible = False
TextBox1.Text = TextBox1.Text & "1"
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
TextBox1.Visible = True
TextBox2.Visible = False
TextBox1.Text = TextBox1.Text & "2"
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
TextBox1.Visible = True
TextBox2.Visible = False
TextBox1.Text = TextBox1.Text & "3"
End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
TextBox1.Visible = True
TextBox2.Visible = False
TextBox1.Text = TextBox1.Text & "4"
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
TextBox1.Visible = True
TextBox2.Visible = False
TextBox1.Text = TextBox1.Text & "5"
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
TextBox1.Visible = True
TextBox2.Visible = False
TextBox1.Text = TextBox1.Text & "6"
End Sub

Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
TextBox1.Visible = True
TextBox2.Visible = False
TextBox1.Text = TextBox1.Text & "7"
End Sub

Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
TextBox1.Visible = True
TextBox2.Visible = False
TextBox1.Text = TextBox1.Text & "8"
End Sub

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
TextBox1.Visible = True
TextBox2.Visible = False
TextBox1.Text = TextBox1.Text & "9"
End Sub

Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
Label1.Text = "+"
TextBox3.Text = TextBox1.Text
TextBox1.Text = ""
Button3.Enabled = True
End Sub

Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
Label1.Text = "-"
TextBox3.Text = TextBox1.Text
TextBox1.Text = ""
Button3.Enabled = True
End Sub

Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
Label1.Text = "*"
TextBox3.Text = TextBox1.Text
TextBox1.Text = ""
Button3.Enabled = True
End Sub

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
Label1.Text = "/"
TextBox3.Text = TextBox1.Text
TextBox1.Text = ""
Button3.Enabled = True
End Sub

Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
TextBox1.Visible = False
TextBox2.Visible = True
TextBox2.Text = Math.Sqrt(TextBox1.Text)
TextBox1.Text = ""
Button3.Enabled = True
End Sub

Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
Label1.Text = ""
Button3.Enabled = True
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.beep_high, AudioPlayMode.Background)
End If
TextBox1.Text = TextBox1.Text & ","
Button3.Enabled = False
End Sub

Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
If SomOnToolStripMenuItem.Checked = True Then
My.Computer.Audio.Play(My.Resources.cash_register, AudioPlayMode.Background)
End If
TextBox1.Visible = False
TextBox2.Visible = True
If Label1.Text = "+" Then
TextBox2.Text = Val(TextBox3.Text) + Val(TextBox1.Text)
End If
If Label1.Text = "-" Then
TextBox2.Text = Val(TextBox3.Text) - Val(TextBox1.Text)
End If
If Label1.Text = "*" Then
TextBox2.Text = Val(TextBox3.Text) * Val(TextBox1.Text)
End If
If Label1.Text = "/" Then
TextBox2.Text = Val(TextBox3.Text) / Val(TextBox1.Text)
End If
TextBox1.Text = ""
TextBox3.Text = ""
Label1.Text = ""
Button3.Enabled = True
End Sub

Private Sub SomOffToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SomOffToolStripMenuItem.Click
ToolStripDropDownButton1.Text = "Som Off"
SomOffToolStripMenuItem.Checked = True
SomOnToolStripMenuItem.Checked = False
End Sub

Private Sub SomOnToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SomOnToolStripMenuItem.Click
ToolStripDropDownButton1.Text = "Som On"
SomOnToolStripMenuItem.Checked = True
SomOffToolStripMenuItem.Checked = False
End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class
'if é para Acionar som em calculadora

Link para o comentário
Compartilhar em outros sites

bom dia, eu fiz desta forma e funciona perfeitamente.


//comandos calculadora
namespace Calculadora
{
//declaração variaveis
public partial class Form1 : Form
{
Boolean Limpaexibicao;
double Operando1 = 0, Operando2 = 0;
string Operador;

public Form1()
{
InitializeComponent();
}
//incio comando botão numeros
private void Btn1_Click(object sender, EventArgs e)
{
if (Limpaexibicao)
{
TxtDisplay.Clear();
Limpaexibicao = false;
}

Button Botao = (Button)sender;
if (Botao.Name.Equals("Btn0"))
{
if (TxtDisplay.Text.Equals("0"))
{
}
else
{
TxtDisplay.Text = TxtDisplay.Text + Botao.Text;
}
}
else
{
if (TxtDisplay.Text.Equals("0"))
{
TxtDisplay.Text = Botao.Text;
}
else
{
TxtDisplay.Text = TxtDisplay.Text + Botao.Text;
}
}

}

private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
Btn0.Click += Btn1_Click;
Btn2.Click += Btn1_Click;
Btn3.Click += Btn1_Click;
Btn4.Click += Btn1_Click;
Btn5.Click += Btn1_Click;
Btn6.Click += Btn1_Click;
Btn7.Click += Btn1_Click;
Btn8.Click += Btn1_Click;
Btn9.Click += Btn1_Click;
}
//fim comando botões numeros
//incio botão limpar C
private void btnLimpar_Click(object sender, EventArgs e)
{
TxtDisplay.Text="0";
TxtDisplay.Focus();
Operando1 = 0;
Operando2 = 0;
Operador = "";
}
//fim botão limpar C
//inicio botão soma
private void BtnSoma_Click(object sender, EventArgs e)
{
if (Operando1 != 0)
{
Operando2 = Double.Parse(TxtDisplay.Text);
}

else
{
Operando1 = double.Parse(TxtDisplay.Text);
}

Operador = "+";
if ((Operando1 != 0) && (Operando2 != 0))
{
BtnIgual_Click(null, null);
}
Limpaexibicao = true;

}
//fim botão soma
//inicio comando botão igual
private void BtnIgual_Click(object sender, EventArgs e)
{
double Resultado = 0;
Operando2 = double.Parse(TxtDisplay.Text);

if (Operador.Equals("+"))
{
Resultado = Operando1 + Operando2;
}
else if (Operador.Equals("-"))
{
Resultado = Operando1 - Operando2;
}
else if (Operador.Equals("*"))
{
Resultado = Operando1 * Operando2;
}
else if (Operador.Equals("/"))
{
if (Operando2 != 0)
{
Resultado = Operando1 / Operando2;
}
else
{
TxtDisplay.Text = "Impossivel dividir por 0";
Limpaexibicao = true;
return;
}

}
Operando1 = Resultado;
TxtDisplay.Text = Resultado.ToString();
Limpaexibicao = true;
}
private void TxtDisplay_TextChanged(object sender, EventArgs e)
{
TxtDisplay.ForeColor = Color.Red;
}
//fim comando botão igual
//comando operação
private void BtnSubtracao_Click(object sender, EventArgs e)
{
if (Operando1 != 0)
{
Operando2 = Double.Parse(TxtDisplay.Text);
}

else
{
Operando1 = double.Parse(TxtDisplay.Text);
}

Operador = "-";
if ((Operando1 != 0) && (Operando2 != 0))
{
BtnIgual_Click(null, null);
}
Limpaexibicao = true;

}

private void BtnMultiplicacao_Click(object sender, EventArgs e)
{
if (Operando1 != 0)
{
Operando2 = Double.Parse(TxtDisplay.Text);
}

else
{
Operando1 = double.Parse(TxtDisplay.Text);
}

Operador = "*";
if ((Operando1 != 0) && (Operando2 != 0))
{
BtnIgual_Click(null, null);
}
Limpaexibicao = true;

}

private void BtnDividir_Click(object sender, EventArgs e)
{
if (Operando1 != 0)
{
Operando2 = Double.Parse(TxtDisplay.Text);
}

else
{
Operando1 = double.Parse(TxtDisplay.Text);
}

Operador = "/";
if ((Operando1 != 0) && (Operando2 != 0))
{
BtnIgual_Click(null, null);
}
Limpaexibicao = true;

}
//fim comando operação
//comando botão ponto
private void BtnPonto_Click(object sender, EventArgs e)
{
if (TxtDisplay.Text.IndexOf (",") > 0)
{

}
else if (TxtDisplay.Text.Length==0)
{
TxtDisplay.Text= "0,";

}
else
{
TxtDisplay.Text = TxtDisplay.Text + (",");
}
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{

if (e.KeyData == Keys.NumPad0)//se o contedudo da tecla for = a numerico 0 temos que chamar o botão zero
{
Btn1_Click(Btn0, null);
}
else if (e.KeyData == Keys.NumPad1)
{
Btn1_Click(Btn1, null);
}
else if (e.KeyData == Keys.NumPad2)
{
Btn1_Click(Btn2, null);
}
else if (e.KeyData == Keys.NumPad3)
{
Btn1_Click(Btn3, null);
}
else if (e.KeyData == Keys.NumPad4)
{
Btn1_Click(Btn4, null);
}
else if (e.KeyData == Keys.NumPad5)
{
Btn1_Click(Btn5, null);
}
else if (e.KeyData == Keys.NumPad6)
{
Btn1_Click(Btn6, null);
}
else if (e.KeyData == Keys.NumPad6)
{
Btn1_Click(Btn6 ,null);
}
else if (e.KeyData == Keys.NumPad7)
{
Btn1_Click(Btn7, null);
}
else if (e.KeyData == Keys.NumPad8)
{
Btn1_Click(Btn8, null);
}
else if (e.KeyData == Keys.NumPad9)
{
Btn1_Click(Btn9, null);
}
else if (e.KeyData == Keys.Decimal)
{
BtnPonto_Click(BtnPonto, null);
}
else if (e.KeyData == Keys.Add)
{
BtnSoma_Click (BtnSoma,null);
}
else if (e.KeyData == Keys.Subtract)
{
BtnSubtracao_Click(BtnSubtracao, null);
}
else if (e.KeyData == Keys.Divide)
{
BtnDividir_Click(BtnDividir, null);
}
else if (e.KeyData == Keys.Multiply)
{
BtnMultiplicacao_Click(BtnMultiplicacao, null);
}
else if (e.KeyData == Keys.Delete)
{
BtnIgual_Click(BtnIgual, null);
}

}
}

}

Link para o comentário
Compartilhar em outros sites

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

Ebook grátis: Aprenda a ler resistores e capacitores!

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!