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?
Related
I'm creating a small program which can save and load char array values. Then, I got stuck with two problems.
I have no idea how to make the program end after saving the data.
After loading the char array, it looks the game starts where I saved last time. However, when I put "#" on the place where is already marked, it is accepted. (It is supposed to display error message)
This is when I start new game.
It displays error message properly.
Here is class which includes streamWriter and streamReader.
public class History
{
public char QUIT = 'Y';
public char CONTINUE = 'N';
public char inputGameContinue;
public void WriteFile(char []arr)
{
FileStream sb = new FileStream("MyFile.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(sb);
WriteLine("If you want to save the data, enter" + QUIT +"| To continue, enter "+CONTINUE );
inputGameContinue = char.Parse(ReadLine());
if(inputGameContinue=='Y')
{
sw.Write(arr);
WriteLine("The data is saved!");
}
sw.Close();
}
public void ReadFile()
{
string path = "MyFile.txt";
WriteLine("New game? >> 1 | Load saved data? >>2 ");
int command = int.Parse(ReadLine());
if (command == 2)
{
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
WriteLine(sr.ReadLine());
}
}
}
}
}
Class2
using System;
using static System.Console;
using System.IO;
namespace createSample
{
public class writeRead
{
public static void Main(string[] args)
{
InputClass inputClass = new InputClass();
ArrayValue arrayValue = new ArrayValue();
History history = new History();
WriteLine("Welcome to game!");
WriteLine("");
history.ReadFile();
do
{
inputClass.inputNumber();
while (true)
{
if (arrayValue.arr[inputClass.input] == '#')
{
WriteLine("{0} already marked '#'. Try another.", inputClass.input);
inputClass.inputNumber();
}
else
{
arrayValue.arr[inputClass.input] = '#';
arrayValue.printArray();
history.WriteFile(arrayValue.arr);
break;
}
}
}
while (checkWhenFinish(arrayValue)!=1 );
WriteLine("All letters are marked with '#'");
Read();
}
public static int checkWhenFinish(ArrayValue a)
{
if(a.arr[0] != '0' && a.arr[1] != '1' && a.arr[2] != '2' && a.arr[3] != '3' && a.arr[4] != '4')
{
return 1;
}
else
{
return 0;
}
}
}
}
Class3
using System;
using System;
using System.Numerics;
using System.Reflection.Emit;
using static System.Console;
using System.IO;
namespace createSample
{
public class ArrayValue
{
public char []arr = { '0','1', '2', '3', '4' };
public void printArray()
{
WriteLine("{0},{1},{2},{3},{4}", arr[0], arr[1], arr[2], arr[3], arr[4]);
}
}
}
Class 4
using System;
using System;
using System.Numerics;
using System.Reflection.Emit;
using static System.Console;
using System.IO;
namespace createSample
{
public class InputClass
{
public int input;
public void inputNumber()
{
while (true)
{
Write("Enter number ? (0 to 4) >> ");
if (!int.TryParse(ReadLine(), out input))
{
input = -1;
}
if (input == 0|| input == 1 || input == 2 || input == 3 || input == 4 )
{
break;
}
else
{
WriteLine("Error! Try again!");
}
}
}
}
}
The problem is when you call history.ReadFile() it can read the file and display its contents, but it never updates arrayValue.arr so when the check is done later, arrayValue.arr[inputClass.input] is still 2.
You might want to pass in arrayValue by reference to ReadFile() to have it updated:
history.ReadFile(ref arrayValue);
then in History
public void ReadFile(ref ArrayValue arrayValue)
{
...
while (sr.Peek() >= 0)
{
var line = sr.ReadLine();
WriteLine(line);
for (int i = 0; i < arrayValue.arr.Length; i++)
{
arrayValue.arr[i] = line[i];
}
}
...
}
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();
}
}
}
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;
}
}
}
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();
}
}
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.