I am trying to create a simple form for calculating speed, distance and time traveled, but the calculations come out 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;
namespace _2ndTask
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double speed, time, distance, laiks, garums, atrums; // laiks = time2, garums = distance2, atrums = speed2
private void label1_Click_1(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
bool timeInput = double.TryParse(textBox3.Text, out laiks);
if (!timeInput || laiks < 0 || laiks > 9999)
{
// MessageBox.Show("Ignorēt šo paziņojumu.");
}
// timeInput = float.Parse (textBox3.Text);
}
private void textBox2_TextChanged2(object sender, EventArgs e)
{
bool cGarumsInput = double.TryParse(textBox2.Text, out garums);
if (!cGarumsInput || garums < 0 || garums > 999999)
{
// MessageBox.Show("Ignorēt šo paziņojumu.");
}
}
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
bool speedInput = double.TryParse(textBox3.Text, out atrums);
if (!speedInput || atrums < 0 || atrums > 9999)
{
// MessageBox.Show("Ignorēt šo paziņojumu.");
}
}
private void button2_Click(object sender, EventArgs e)
{
time = (garums / atrums);
label4.Text = time.ToString();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
distance = atrums * laiks;
label4.Text = distance.ToString() + " Km.";
}
private void button1_Click(object sender, EventArgs e)
{
speed = garums / laiks;
label4.Text = speed.ToString() + " Km/h.";
}
}
}
Before I added TryParse the calculations were correct, but the forms would crash whenever values would be deleted from textboxes.
It should output 140 underneath (multiply time and speed) instead it outputs 49. Inputting other numbers also gives wrong result
You have a typo. You are assigning the text from textBox3 to both atrums and laiks. Presumably one of them should use the text from textBox1.
Related
In the picture I want the save button to save the entries of customers to a jagged array and then when I press the show button the saved names should show into the text box below.
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 week5hw
{
public partial class Form1 : Form
{
string[][] str = new string[3][];
public Form1()
{
InitializeComponent();
}
public void Button1_Click(object sender, EventArgs e)
{
String names = textBox1.Text;
str[0] = new string[5];
str[0][0] = names;
}
private void Button2_Click(object sender, EventArgs e)
{
}
}
}
I still don't see how a jagged array applies here. You need to give more details about how the program is supposed to work.
At any rate, you need to declare an int variable to track how many of those spots have been used. That variable can also be used to determine which "row" in your jagged array to store the entered name in.
Might look something like this:
public partial class Form1 : Form
{
int count=0;
string[][] str = new string[3][];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "The number of free space in room is: " + str.Length;
textBox2.Multiline = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim().Length > 0)
{
if (count < str.Length)
{
str[count] = new string[] { textBox1.Text };
count++;
label1.Text = "The number of free space in room is: " + (str.Length - count);
textBox1.Clear();
textBox1.Focus();
}
else
{
MessageBox.Show("No space left!");
}
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Clear();
for(int i=0; i<count; i++)
{
textBox2.AppendText(str[i][0] + "\r\n");
}
}
}
This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 5 years ago.
At First I don't know which one is outside the size. And as you can see I have put % not to be outside the array. This is my code, please don't blame me for any childish mistake. If you have tips for a more efficient encrypting method I am opened
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 System.IO;
using System.Text;
namespace encrypting
{
public partial class Form1 : Form
{
string text;
string key;
public string calcXor(string a, string b)
{
char[] charAArray = a.ToCharArray();
char[] charBArray = b.ToCharArray();
char[] result = new char[6];
int len = 0;
for (int i = 0; i < a.Length; i++)
{
if (i != 0)
result[i] = (char) ( ((int)charAArray[a.Length % i] ^ (int)charBArray[b.Length % i]) ) ; //error
}
return new string(result);
}
private void Crypt()
{
char[] a;
int i, sizeoftext = text.Length,j=0;
string somestring ="";
string key = textBox2.Text;
StringBuilder sb = new StringBuilder(somestring);
for(i=0;i<sizeoftext-1;i++)
{
if (i == key.Length-1)
j = 0;
}
somestring = calcXor(text, key);
if (File.Exists(textBox3.Text)) ;
File.Create(textBox3.Text).Close();
System.IO.File.WriteAllText(textBox3.Text, somestring);
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
text = File.ReadAllText(textBox1.Text);
Crypt();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
key = this.Text;
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fd.FileName;
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
FolderBrowserDialog fd = new FolderBrowserDialog();
if(fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
textBox3.Text = fd.SelectedPath;
textBox3.Text = textBox3.Text +"\\" + textBox4.Text + ".txt";
}
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
}
}
Please help me.
If your strings are longer than 6 characters the following assignment will be out of bounds:
result[i] = (char) ( ((int)charAArray[a.Length % i] ^ (int)charBArray[b.Length % i]) ) ; //error
I am trying to make an If Statement, which when button1 is clicked will be showed in label1, when textbox1 is 25 or above, "Customer can receive £5 off purchase" and will show when 50 or above, "Customer can receive £10 off purchase"
My Code is as followed: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 If_Statement
{
public partial class Form1 : Form
{
int numbera = 25;
int numberb = 50;
public Form1()
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text <= numbera)
{
label1.Text = ("Customer can receive £5 off purchase");
}
if (textBox1.Text <= numberb)
{
label1.Text = ("Customer can receive £10 off purchase");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
Wondering where I am going wrong and if I can have an explanation of why and how to fix it.
Thanks in advance.
You need to convert the value entered to a number before comparing it:
private void button1_Click(object sender, EventArgs e)
{
var number = Double.Parse(textBox1.Text);
if (number <= numbera)
{
label1.Text = ("Customer can receive £5 off purchase");
}
else if (number <= numberb)
{
label1.Text = ("Customer can receive £10 off purchase");
}
}
You should notice that code will break whenever the user enters something like "abc", as that can't be parsed as number, so you'll need to work with a more safer way to validate the user input like Double.TryParse.
This one is driving me a bit crazy. Any help gratefully received. It's a simple program to receive temperature data from an Arduino based temp sensor, and display it in a graph control on a form. The program works fine, and parses the temp data frame a treat. However..... the graph object doesn't refresh, and the whole point is to show the data over time. I thought that the chart1.DataBind command that I put in forced a refresh. I am using Visual Studio 2013 Express. Any thoughts as to what I am doing wrong very much appreciated.
// Start of program.
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 serial_port_with_events_attempt_4
{
public partial class Form1 : Form
{
string RxString;
int RxRead;
int i;
int RxDec1;
int RxDec2;
int RxDec3;
float RxFloat;
float RxFloat2;
string locnString;
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM8";
serialPort1.BaudRate = 9600;
serialPort1.Open();
if (serialPort1.IsOpen)
{
buttonStart.Enabled = false;
buttonStop.Enabled = true;
textBox1.ReadOnly = false;
}
}
private void buttonStop_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
buttonStart.Enabled = true;
buttonStop.Enabled = false;
textBox1.ReadOnly = true;
}
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxRead = serialPort1.ReadByte();
this.Invoke(new EventHandler(DisplayText));
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
private void DisplayText(object sender, EventArgs e)
{
if (RxRead == 126)
{
textBox1.AppendText(Environment.NewLine);
i = 0;
}
if (i< 23)
{
if (i == 7)
{
if (RxRead == 51) // 51 in this position means that the temp sensor is the one in the wooden box
{
textBox1.AppendText("Temperature in Nick's office = ");
locnString = ("Nick's office");
}
}
if (i == 17)
{
RxDec1 = RxRead - 48; // Read the tens unit
}
if (i == 18)
{
RxDec2 = RxRead - 48; // Read the units
}
if (i == 20)
{
RxDec3 = RxRead - 49; // read the decimal
}
if (i == 22)
{
RxFloat = ((RxDec1 * 10) + RxDec2);
RxFloat2 = RxDec3;
RxFloat2 = RxFloat2 / 10;
RxFloat = RxFloat + RxFloat2;
RxString = RxFloat.ToString();
if (RxFloat < 30 && RxFloat >20)
{
// Put the value in the main text box if it is not corrupt, (checking if the range is reasonable
textBox1.AppendText(RxString); // Frig about to get the reads in the right format and added together
// Add a new line into the temperature database
temperature1DataSetTableAdapters.Temp1TableAdapter temp1tableadapter = new temperature1DataSetTableAdapters.Temp1TableAdapter();
temp1tableadapter.Insert(DateTime.Now, RxFloat, locnString);
}
// Delete any old data.
temperature1DataSetTableAdapters.TempTableAdapter temp2tableadapter = new temperature1DataSetTableAdapters.TempTableAdapter();
temp2tableadapter.DeleteTempQuery();
// The above two lines work, but I need to amend to select on date TODO
chart1.DataBind();
}
}
i=i+1;
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'temperature1DataSet.Temp' table. You can move, or remove it, as needed.
this.tempTableAdapter.Fill(this.temperature1DataSet.Temp);
}
}
}
Cheers,
Nick James
Every time a check box is checked I want to be able to add to the progress bar in increments. So lets say if 1 out of 4 check boxes is check than it will equal to lets say 25 percent of the progress bar. Moreover if you uncheck one of the 4 check boxes the progress bar will decrease accordingly. This is what I have to which I am stuck.
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void progressBar1_Click(object sender, EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
int num1 = progressBar1.Maximum / 4;
int num2 = progressBar1.Maximum / 4;
int num3 = progressBar1.Maximum / 4;
int num4 = progressBar1.Maximum / 4;
int numAns;
numAns = num1 + num2 + num3 + num4;
progressBar1.Value = numAns;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked == true)
{
}
else if (checkBox1.Checked == false)
{
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
}
}
}
You can just use the same event handler for all of your checkboxes without making 4 methods for 4 checkboxes...
private const Int32 TOTAL_CHECKBOXES = 4;
private static Int32 s_Checks = 0;
private void OnCheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
++s_Checks;
else
--s_Checks;
progressBar.Value = s_Checks * (progressBar.Maximum / TOTAL_CHECKBOXES);
}
Scrap the ProgressBar1_click, and for each box simply add (if Checked) or subtract (if not) 25 from ProgressBar1.Value on CheckedChanged.
You can wire up the same event to all checkboxes. I added mine to a list, so that if you wanted to add more in the future, you could simply add the handler and add it to the list and you are done.
public Form1()
{
InitializeComponent();
checkBox1.CheckedChanged += CheckedChanged_1;
checkBox2.CheckedChanged += CheckedChanged_1;
checkBox3.CheckedChanged += CheckedChanged_1;
checkBox4.CheckedChanged += CheckedChanged_1;
checkboxesToCount.AddRange(new CheckBox[] {checkBox1, checkBox2, checkBox3, checkBox4});
}
private void CheckedChanged_1(object sender, EventArgs e)
{
progressBar1.Value = 100 * checkboxesToCount.Count((c) => { return c.Checked; }) / checkboxesToCount.Count;
}