Como posso pegar um valor de uma textBox e colocar em uma variavel para eu comparar com um if?
valor de texto? propriedade Text
if (txtTexto.Text == "meu texto") {
}
// para variável
String s = txtTexto.Text;
// a mesma coisa pra settar
txtTexto.Text = s;
// já vão umas dicas de conversão:
int i = Int32.parse(txtTexto.Text);
s = txtTexto.Text.ToString("N2"); // formato 99...99,99
vlw cara…
Pow porque nao existe só Java no mundo? rs rs
e se for em float?
tipo R$ 90,00
Olha como ele esta. Não consegui precisao… nao sei oque esta acontecendo
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Mariana
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if((valor1.Text!=null)&&((valor2.Text)!=null)){
float valorNum1 = float.Parse(valor1.Text);
float valorNum2 = float.Parse(valor2.Text);
if (valorNum1 < valorNum2)
{
float total = (valorNum2-valorNum1)*100/valorNum2;
MessageBox.Show("Houve Acrescimo de R$:" + total + "%");
}
else
{
float total = (valorNum1-valorNum2)* 100 / valorNum1;
MessageBox.Show("Houve queda de :R$" + total + "%");
}
}
else MessageBox.Show("Insira valores nos dois campos");
}
}
}
ainda bem que não existe só Java no mundo -.-
String s = "90,00";
float f = float.parse(s); // 1. tem que ser com vírgula e não ponto, 2. também serve Single.parse()
// 1.
f = float.parse(s.Replace('.', ',')); // troca . por ,
// 2.
f = Single.parse(s);
edit:
tenta trocar seu evento no código por algo como:
[code]private void button1_Click(…) {
try {
if (valor1.Text == null || valor1.Text == “”)
throw new Exception (“O primeiro valor é inválido”);
else if (valor2.Text == null || valor2.Text == “”)
throw new Exception (“O segundo valor é inválido”);
float valorNum1 = float.Parse(valor1.Text.Replace('.', ','));
...
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}[/code]
fica muito ilegível esse aninhamento de ifs pra validação, melhor jogar exception pra parar a execução
porque o codigo só da 100%???
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Mariana
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if ((valor1.Text != "") && ((valor2.Text) != ""))
{
float valorNum1 = float.Parse(valor1.Text.Replace(",","."));
float valorNum2 = float.Parse(valor2.Text.Replace(",","."));
if (valorNum1 < valorNum2)
{
//float total = (valorNum1 - valorNum2) * 100 / valorNum2;
float total = (valorNum2*100)/valorNum2;
MessageBox.Show("Houve Acrescimo de R$:" + total + "%");
}
else
{
float total = (valorNum1* 100) / valorNum1;
MessageBox.Show("Houve queda de :R$" + total + "%");
}
}
else MessageBox.Show("Insira valores nos dois campos");
}
}
}
matematicamente falando, em "(valorNum2100)/valorNum2" (o mesmo vale pro valorNum1), se substituirmos a variável pela incógnita x:
(x * 100) / x = x * 100 / x = x/x * 100 = 1100 = 100
sempre vai ser 100 .-.
afffffff troquei os valores
Mas mesmo assim esta dando errado.
por exemplo: tenho 100 reais e agora tenho 200
quantos % é?
esta dando 200%
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Mariana
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if ((valor1.Text != "") && ((valor2.Text) != ""))
{
float valorNum1 = float.Parse(valor1.Text);
float valorNum2 = float.Parse(valor2.Text);
if (valorNum1 > valorNum2)
{
//float total = (valorNum1 - valorNum2) * 100 / valorNum2;
float total = (valorNum1*100)/valorNum2;
MessageBox.Show("Houve Acrescimo de R$:" + total + "%");
}
else
{
float total = (valorNum2*100) / valorNum1;
MessageBox.Show("Houve queda de :R$" + total + "%");
}
}
else MessageBox.Show("Insira valores nos dois campos");
}
}
}
eu nao to batendo bem hoje
Caraca… foi \o/
meu deus preciso de férias
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Mariana
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if ((valor1.Text != "") && ((valor2.Text) != ""))
{
float valorNum1 = float.Parse(valor1.Text);
float valorNum2 = float.Parse(valor2.Text);
if (valorNum1 < valorNum2)
{
float total = (valorNum2 - valorNum1) * 100 / valorNum1 ;
MessageBox.Show("Houve Acrescimo de R$:" + total + "%");
}
else
{
float total = (valorNum1 - valorNum2) * 100 / valorNum2;
MessageBox.Show("Houve queda de :R$" + total + "%");
}
}
else MessageBox.Show("Insira valores nos dois campos");
}
}
}
tai o código pra alguem consultar