My code is:
protected void txttotal_TextChanged(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtmaths.Text);
int b = Convert.ToInt32(txtsci.Text);
int c = Convert.ToInt32(txtenglish.Text);
int tot = a + b + c;
txttotal.Text = Convert.ToString(tot);
}
I am trying get the total marks to a textbox, but it is not working.Can you help me please?
You are trying to change the text in the same textbox that the text is changing, are you sure this is what you want to do?
Wouldn't you want to change the Total when the maths,sci and english values change? if so look at the following.
protected void txtmaths_TextChanged(object sender, EventArgs e)
{
UpdateTotals();
}
protected void txtsci_TextChanged(object sender, EventArgs e)
{
UpdateTotals();
}
protected void txtenglish_TextChanged(object sender, EventArgs e)
{
UpdateTotals();
}
protected void UpdateTotals()
{
int a = Convert.ToInt32(txtmaths.Text);
int b = Convert.ToInt32(txtsci.Text);
int c = Convert.ToInt32(txtenglish.Text);
int tot = a + b + c;
txttotal.Text = tot.ToString();
}
It looks like this method is the even handler for the TextChanged event of txttotal. I am guessing on "not working" here: You might run into a loop if you change a control in the handler to its changed-event.
You might want to check if you are hitting the handler because of a changed done by the handler.
Problem : there is no problem with the code but it looks you are writing your code inside total textbox TextChanged event handler, so who will raise the total textbox TextChanged event.
Solution : So you need to have some Button like Sumbit and you need to move the above code into Submit button click event handler as below:
protected void btnSubmit_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtmaths.Text);
int b = Convert.ToInt32(txtsci.Text);
int c = Convert.ToInt32(txtenglish.Text);
int tot = a + b + c;
txttotal.Text = Convert.ToString(tot);
}
You have the code to update txtTotal in txtTotal_TextChanged event. You need to put the code you have in some method and call it on change of txtmaths, txtsci, txtenglish instead of txttotal_TextChanged
protected void UpdateTotal()
{
txttotal.Text = Convert.ToInt32(txtmaths.Text) + Convert.ToInt32(txtsci.Text) + Convert.ToInt32(txtenglish.Text);
}
protected void txtmaths_TextChanged(object sender, EventArgs e)
{
UpdateTotal();
}
I can see this erroring due to type exceptions.
This is a safer way of doing this with less potential errors:
protected void txttotal_TextChanged(object sender, EventArgs e)
{
int a = 0;
int b = 0;
int c = 0;
if (!Int32.TryParse(txtmaths.Text, ref a))
// handle error...
if (!Int32.TryParse(txtsci.Text, ref b))
// handle error...
if (!Int32.TryParse(txtenglish.Text, ref c))
// handle error...
int tot = a + b + c;
txttotal.Text = Convert.ToString(tot);
}
Related
I want to have the abilitiy to add numeric fields using an event as i have over 50 i dont fancy adding event to each one is there a better way of doing the following. Also I cant seem to get sumEarnings to be reconized by the compiler does anybody have any suggestions.
protected void Page_Load(object sender, EventArgs e)
{
wagesnET.ValueChanged += sumEarnings();
}
protected void btnSave_Click(object sender, EventArgs e)
{
StringDictionary KeyValue = new StringDictionary();
KeyValue.Add("", "");
foreach (string key in Request.Form)
{
if (!key.StartsWith("checkbox")) continue;
}
}
private void sumEarnings(object sender, EventArgs e)
{
int total = Convert.ToInt16(wagesnET.Text) + Convert.ToInt16(partnerearningscic.Text);
return total;
}
sumEarnings is a method without return type, change it as
private int sumEarnings()
{
return Convert.ToInt16(wagesnET.Text) + Convert.ToInt16(partnerearningscic.Text);
}
To attach it to multiple controls from the code you can use enumerator. For example:
foreach (Control c in this.Controls)
{
//if (c is TextBox)
c.ValueChanged += sumEarnings();
}
You need to remove the () from the event handler assignment:
wagesnET.ValueChanged += sumEarnings;
After you calculate the new total value udpate the appropriate control:
private void sumEarnings(object sender, EventArgs e)
{
int total = Convert.ToInt16(wagesnET.Text) + Convert.ToInt16(partnerearningscic.Text);
// todo: update the total value...
}
It's not possible to have an event handler that return a value at the same time.
I have a mathematical problem and I´m trying to solve it, the problem is that you have 81 coins, but one is fake and it´s heavier than the others,you have to find out which one is the fake one by using a scale and doing only 4 comparisons.
I´m trying to make it like a game, when a users decides which coin will be the fake one, and the other player has to find it.
I made an array named monedasf and made all the values 0, so when the users type in the coin that wants to be the fake one, the value changes to 1. I´m trying right now to print the array, but I don´t know if I have to print it in a multiline textbox or where, here´s the code I have untill now.
public partial class Form1 : Form
{
public static int[] monedasf = new int[81];
public Form1()
{
InitializeComponent();
for( int i = 0; i<=80;i++)
{
monedasf[i] = 0;
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
int n;
n = Convert.ToInt32(textBox1.Text);
monedasf[n] = 1;
textBox1.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i <= 80; i++)
textBox2.Text = Convert.ToString(monedasf[i]);
}
}
I have only BASIC KNOWLEDGE of programming, that´s why my code might be so primitive :D
Try using something like this:
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i <= 80; i++)
textBox2.Text += monedasf[i].ToString() + " ";
}
If you want you can replace " " with any separator you want, like "\n" for newline.
Actually your code would work too, the problem is you were reseting textBox2's text by using assign operator:
textBox2.Text = Convert.ToString(monedasf[i]); // will clear and then print
textBox2.Text += Convert.ToString(monedasf[i]); // will not clear and print
All you need is not to reset previous text inside.
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = string.Join(", ", monedasf);
}
Use string.Join which is very useful for display.
Hi im trying to create this program where the txtbox1 will multiply on user's input and automatically shows the total in decimal point same as in other txtboxes, but i'm having a trouble to sum up all the subtotals of every result to overall txtbox total
here is my code in every subtotals
private void txtbox11_TextChanged_1(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtbox12.Text))
txtTotal11.Text =
(Convert.ToInt32(txtbox11.Text)*Convert.ToDecimal(112.61)).ToString();
}
private void txtbox12_TextChanged_1(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtbox12.Text))
txtTotal12.Text =
(Convert.ToInt32(txtbox12.Text) * Convert.ToDecimal(32.10)).ToString();
}
How do i automatically get the sum of txtTotal11 & txtTotal12 to display on txtTotal13?
or should i put an event like "textChanged" on every txtTotal# too?
thanks guys i'm really having a headache on this.
It will be something like:
private void txtboxSubTotal1_TextChanged_1(object sender, EventArgs e)
{
CalcGrandTotal();
}
private void txtboxSubTotal2_TextChanged_1(object sender, EventArgs e)
{
CalcGrandTotal();
}
private void CalcGrandTotal()
{
decimal grandTotal = 0;
decimal parseValue= 0;
if (!string.IsNullOrEmpty(txtboxSubTotal1.Text) && decimal.TryParse(txtboxSubTotal1.Text, parseValue))
grandTotal += parseValue;
if (!string.IsNullOrEmpty(txtboxSubTotal2.Text) && decimal.TryParse(txtboxSubTotal2.Text, parseValue))
grandTotal += parseValue;
txtboxGrandTotal.Text = grandTotal.ToString();
}
Try this: (in every TextChanged event)
decimal total13;
private void txtbox11_TextChanged_1(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtbox12.Text))
var thisTotal = (Convert.ToInt32(txtbox11.Text)*Convert.ToDecimal(112.61)).ToString();
txtTotal11.Text = thisTotal
total13 += thisTotal
txtTotal13.Text = thisTotal.ToString();
}
i am developing an inventory system in which i fetch the data from database through textbox1 and the data compatible to the entry in textbox1 appears in textbox8 and textbox10 which are integers. Now i want to multiply textbox8 and textbox10 and the result should be display into textbox7 when the integers appears in textbox8 and textbox10.
i am using this piece of code for multiplication of two textboxes and result in the third text box does not appear:
private void textBox7_TextChanged(object sender, EventArgs e)
{
Int32 val1 = Convert.ToInt32(textBox10.Text);
Int32 val2 = Convert.ToInt32(textBox8.Text);
Int32 val3 = val1 * val2;
textBox7.Text = val3.ToString();
}
You are using the wrong textbox for your TextChanged event. Your textboxes 8 and 10 are changing but you have attached the shown code to textbox7.
public MyForm : Form {
public MyForm(){
InitializeComponents();
// Here you define what TextBoxes should react on user input
textBox8.TextChanged += TextChanged;
textBox10.TextChanged += TextChanged;
}
private void TextChanged(object sender, EventArgs e)
{
int val1;
int val2;
if (!int.TryParse(textBox8.Text, out val1) || !int.TryParse(textBox10.Text, out val2))
return;
Int32 val3 = val1 * val2;
// Here you define what TextBox should show the multiplication result
textBox7.Text = val3.ToString();
}
}
And finally remove the code inside textBox7_TextChanged(object sender, EventArgs e)
You assigned the event to the wrong control and you're not checking for valid input. You can use int.TryParse to look for valid, numerical input.
private void textBox8_TextChanged(object sender, EventArgs e)
{
Multiply();
}
private void textBox10_TextChanged(object sender, EventArgs e)
{
Multiply();
}
public void Multiply()
{
int a, b;
bool isAValid = int.TryParse(textBox8.Text, out a);
bool isBValid = int.TryParse(textBox10.Text, out b);
if (isAValid && isBValid)
textBox7.Text = (a * b).ToString();
else
textBox7.Text = "Invalid input";
}
Are you getting any compile or bug problems? You need to give your controls meaningful names to better differentiate these controls. Double check that you are referencing your controls properly. In any case, your code should work. My code below catches a failure to convert to int32 below.
try
{
textBox7.Text = (Convert.ToInt32(textBox10.Text) * Convert.ToInt32(textBox8.Text)).ToString();
}
catch (System.FormatException)
{ }
Change your code to the following:
private void textBox7_TextChanged(object sender, EventArgs e)
{
Int32 val1 = Convert.ToInt32(textBox10.Text);
Int32 val2 = Convert.ToInt32(textBox8.Text);
Int32 val3 = val1 * val2;
textBox7.Text = Convert.ToString(val3);
}
On my GUI (Graphical User Interface), I have a button named Enter and a label.
When I click Enter, I want my result to be shown in the label. How do I do that?
For windows forms use the .Text property on the label:
private void btnEnter_Click(object sender, EventArgs e)
{
int themeaningoflifeuniverseandeverything = 420 / 10;
lblResult.Text = themeaningoflifeuniverseandeverything.ToString();
}
See exampe: ButtonEvent.zip
Double click on the button in the designer. This should create you a handler function for the buttons click event, something like this:
private void Button_Click(object sender, EventArgs e)
{
}
then in the function add the code to set the text of the lable:
lable.Text = myResult;
you should end up with something like this:
private void Button_Click(object sender, EventArgs e)
{
lable.Text = myResult;
}
As you said you have an int which is a percentage, and you have the value 0, I suspect that you are having problems getting the percentage not writing the value to the lable. so you might want to look at this question as I suspect that you have something like:
int valuea;
int valueb;
int result= valuea/valueb*100;
in this example if valuea=45 and valueb=100 the value of result will be 0, not 45.
int result = 0; // declare a private variable to hold the result
// Event handler for Click of Enter button
private void Enter_Click(object sender, EventArgs e)
{
result = Add(10,20); // set result to result of some function like Add
label.Text = result.ToString();
}
private int Add(int a, int b)
{
return a + b;
}
NOTE: I assume you are a beginner working with Winforms.
In winforms or webforms:
label.Text = Enter.Text;
Double click on button to generate event and write following code
private void Button_Click(object sender, EventArgs e)
{
lable1.Text = ur result; //result must be in string format otherwise convert it to string
}