why is my textBox passing a blank string - c#

I am creating code for a code behind file for my assignment that involves creating a simple calculator. I am running into problems and i narrowed it down to the string from the textBox. When i try and set the textBox.Text to itself once the user clicks the equal button on the calculator, the calculators textBox becomes blank. here is my code for my code behind file, don't mind the other code that is in it as I was originally just trying to see what parts of the loop were being run.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
public partial class Assign2_Calc : System.Web.UI.Page
{
protected void ButtonEqual_Click(object sender, EventArgs e)
{
string inputStr = inputBox.Text;
double value1 = 0;
double value2 = 0;
double result = 0;
Calculator myCalculator = new Calculator();
string operandsVal;
string[] operands = Regex.Split(inputStr, #"(\+)(\-)(\/)(\*)");
for (int i = 0; i < operands.Length; i ++)
{
if(i == 0 || i==4 || i==8 || i==12 || i==16 || i==20)
{
operandsVal = operands[i];
//numMeh = Convert.ToInt32(operandsVal);
value1 = 1;
result = operands.Length;
}
else if (i == 2)
{
value2 = 2;
result = 2;
}
else
{
switch (operands[i])
{
case "+":
result = myCalculator.Add(value1, value2);
break;
}
}
}
inputBox.Text = inputStr; //result.ToString();
}
}

Related

Cannot get int to be displayed in text box for dice roller app

I'm trying to get back into programming and I'm having trouble getting the final int answers into the text boxes at the end. It has been a few years since I've coded, so if I messed up big time, please let me know.
{
int dice_total;
int dice_num;
int diff_num;
int succ_num = 0;
int ones = 0;
Boolean comp_num = false;
string Succ;
string Comp;
dice_total = int.Parse(Dice.Text);
diff_num = int.Parse(Diff.Text);
Random random = new Random();
dice_num = random.Next(dice_total);
if (dice_num >= diff_num)
{
succ_num++;
}
else
{
if (dice_num == 1)
{
ones++;
}
}
if (ones >= succ_num)
{
comp_num = true;
}
else
{
comp_num = false;
}
Succ = succ_num.ToString();
Comp = comp_num.ToString();
}```
[your text box name].Text=[some int].ToString();
For example:
label1.Text = product.BuyingPrice.ToString();
I can't seem to get it count up the successes. For those who don't know, WoD has you roll d10s and you are given a difficulty. You have to roll that difficulty number or higher to get successes. If there are more (or equal) 1s than total successes, its a complication. This isn't even including code for when you roll a 10 (which has you roll again while still counting as a success).
Ex. Roll 5d10s with a difficulty of 6
6, 8, 10, 1, 1 = 3 success
Roll again for the 10: 1
Total: 3 successes and a complication
using Accord.Math.Distances;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Roller
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int dice_total;
int dice_add;
int dice_num;
int diff_num;
int succ_num = 0;
int ones = 0;
Boolean comp_num = false;
public void button1_Click(object sender, EventArgs e)
{
dice_total = int.Parse(Dice.Text);
diff_num = int.Parse(Diff.Text);
for (dice_add = 0; dice_add < dice_total; dice_add++)
{
Random random = new Random();
dice_num = random.Next(dice_total);
if (dice_num >= diff_num)
{
succ_num++;
}
else
{
if (dice_num == 1)
{
ones++;
}
}
if (ones >= succ_num)
{
comp_num = true;
}
else
{
comp_num = false;
}
}
Succ.Text = succ_num.ToString();
Comp.Text = comp_num.ToString();
}
}
}

Bubble Sort using C# windows application form how to clear label.text every button click

Hi guys every time I pressed the button to generate the sorted string the output gets concatenated in the previous label output. I'm lost on how to clear the previous output on the label before showing the new one here's my code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace You_Source
{
public partial class Form1 : Form
{
string method;
public Form1()
{
InitializeComponent();
}
private void radio_checked(object sender, EventArgs e)
{
RadioButton radio = (RadioButton)sender;
method = radio.Text;
//label_Output.Text = method;
}
private void button_Sort_Click(object sender, EventArgs e)
{
string input = textBox_Input.Text;
label_Output.Clear();
if ( method == "Bubble Sort")
{
char[] charInput = input.ToCharArray();
char temp;
for (int j = 0; j <= charInput.Length - 2; j++)
{
for (int i = 0; i <= charInput.Length - 2; i++)
{
if (charInput[i] > charInput[i + 1])
{
temp = charInput[i + 1];
charInput[i + 1] = charInput[i];
charInput[i] = temp;
}
}
}
foreach (char letter in charInput)
label_Output.Text = label_Output.Text+letter;
}
}
}
can anyone give me a hint on what to do.
example( if i enter "cba" the output would be "abc", then when i entered another input in the text box "zyx" the new label output would be "abcxyz". I just one the new one "xyz" to be shown.
You can just set the string to "" at some point of your code, and overwrite it with the new string

Problem with button CreatePrizeButton_Click

When I start the program, unfortunately, after not entering data and pressing the "Create Prize" button, I do not receive the message "This form contains incorrect information. Please check and try again." What am I doing wrong?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TrackerLibrary;
namespace TrackerUI
{
public partial class CreatePrizeForm : Form
{
public CreatePrizeForm()
{
InitializeComponent();
}
private void CreatePrizeButton_Click(object sender, EventArgs e)
{
if (ValidateForm())
{
PrizeModel model = new PrizeModel(
placeNameValue.Text,
placeNumberValue.Text,
prizeAmountValue.Text,
prizePercentageValue.Text);
foreach (IDataConnection db in GlobalConfig.Connections)
{
db.CreatePrize(model);
}
}
else
{
MessageBox.Show("Ten formularz zawiera błędne informacje. Proszę sprawdzić i spróbować ponownie.");
}
}
private bool ValidateForm()
{
bool output = true;
int placeNumber = 0;
bool placeNumberValidNumber = int.TryParse(placeNumberValue.Text, out placeNumber);
if (placeNumberValidNumber == false)
{
output = false;
}
if (placeNumber < 1)
{
output = false;
}
if (placeNameValue.Text.Length == 0)
{
output = false;
}
decimal prizeAmount = 0;
int prizePercentage = 0;
bool prizeAmountValid = decimal.TryParse(prizeAmountValue.Text, out prizeAmount);
bool prizePercentageValid = int.TryParse(prizePercentageValue.Text, out prizePercentage);
if (prizeAmountValid == false || prizePercentageValid == false)
{
output = false;
}
if (prizeAmount <= 0 && prizePercentage <= 0)
{
output = false;
}
if (prizePercentage < 0 || prizePercentage > 100)
{
output = false;
}
return output;
}
}
}
Hi
When I start the program, unfortunately, after not entering data and pressing the "Create Prize" button, I do not receive the message "This form contains incorrect information. Please check and try again." What am I doing wrong?

Problems about checking for prime number in a Windows Form app

My app is to test for prime number. First time I enter a prime number, the result is true and number is displayed to user. But second time, the prime number check is not functioning as expected. Here is my code:
private void button1_Click(object sender, EventArgs e)
{
label3.Text = textBox1.Text;
float a = (float)Convert.ToDouble(textBox1.Text);
if (check_Number(ref a) == true)
{
ResultCheck.Text = "Input Number is Prime";
}
else if (check_Number(ref a) == false)
{
ResultCheck.Text = "Input Number is not Prime";
}
}
here is an example program that uses trial division.
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 PrimeCheck
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = textBox1.Text;
double testthis = 0;
if (double.TryParse(textBox1.Text, out testthis))
{
if (CheckForPrime(testthis))
{
MessageBox.Show("prime time!!");
}
}
}
bool CheckForPrime(double number)
{//there are better ways but this is cheap and easy for an example
bool result = true;//assume we are prime until we can prove otherwise
for (double d = 2; d < number; d++)
{
if (number % d == 0)
{
result = false;
break;
}
}
return result;
}
}
}

Loading images using a textbox, reading character by character

I would like to do a program that after entering the number into the textbox and will display the appropriate images. For example, if you type 1 and 2, will show candle and a swan, the program will help in remembering the mnemonic system of numbers. This is my code:
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 WindowsFormsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string dane = textBox1.Text;
if (dane == "")
{
MessageBox.Show("No number");
}
else
{
bool jest_liczba = true;
try
{
double dane_ok = System.Convert.ToDouble(dane);
}
catch
{
MessageBox.Show("no int");
jest_liczba = false;
}
if (jest_liczba == true)
{
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string temp = textBox1.Text; //table
for (int i = 0; i < temp.Length; i++)
{
textBox1.Text = char.ToString(temp[i]);
}
string _katalog = #"c:\obrazki\"; //load picture
string _typ = ".jpg";
int _liczba;
if (Int32.TryParse(textBox1.Text, out _liczba))
{
pictureBox1.Image = Image.FromFile(_katalog + _liczba + _typ);
}
}
}
}
Program after entering a couple of numbers, displays only one image. Please help
I'm not sure what your for..loop is accomplishing. Just comment it out:
// string temp = textBox1.Text; //table
// for (int i = 0; i < temp.Length; i++)
// {
// textBox1.Text = char.ToString(temp[i]);
// }
That loop currently replaces the TextBox text with the last character of the temp string.

Categories

Resources