How to set value to textbox in c# from jagged array? - c#

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");
}
}
}

Related

Taking Value From Text Box Into Total (VS - C#)

I am new to the C# scene, so don't really have much knowledge - This is my first project.
I am looking to create a very basic calorie counter, which eventually will include other functions.
Here's what I have so far;
I want to know how to take the value from the text box on the click of the 'Add' button - Which adds to the total value (bottom right)
I'm looking for any tips/videos to help so anything is appreciated.
TIA
I made a simple implementation for you, you could refer to it:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
bool Flag = Int32.TryParse(textBox1.Text, out int result);//Determine if it is a number
if (textBox1.Text == "")//If it is null, falg takes true and skips the next judgment
{ Flag = true; }
else if (!Flag)
{
MessageBox.Show("Input Error");
textBox1.Text = null;
}
}
private void button1_Click(object sender, EventArgs e)
{
Int32.TryParse(textBox1.Text, out int result);
int total =result + Convert.ToInt32(label3.Text);
label3.Text = total.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
label3.Text = "0";
}
}
}

Incorrect calculation output

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.

Index is outside the bound of the array [duplicate]

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

Operator '<=' cannot be applied to operands of type 'string' and 'int' (C#)

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.

How to remove PictureBoxes dynamically?

I am trying to make pictureboxes fall continuously in the form.
Here is the code that I tried.
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 Rain_dropz
{
public partial class Form1 : Form
{
PictureBox[] RD = new PictureBox[500];
int ndrop = 0;
public Form1()
{
InitializeComponent();
}
public void dropIt()
{
for (int i = 0; i < ndrop; i++)
{
RD[i].Top += 10;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Random rnd = new Random();
int l = rnd.Next(1,545);
RD[ndrop] = new PictureBox();
RD[ndrop].BackColor = System.Drawing.Color.MediumBlue;
RD[ndrop].Size = new Size(5, 5);
RD[ndrop].Location = new Point(l, 0);
this.Controls.Add(RD[ndrop]);
ndrop++;
dropIt();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Start();
}
}
}
I think it is better to delete the picture boxes which disappear from the form. How to do that?
You can remove it by removing the picturebox from form controls list.
private void timer1_Tick(object sender, EventArgs e)
{
Random rnd = new Random();
int l = rnd.Next(1,545);
RD[ndrop] = new PictureBox();
RD[ndrop].BackColor = System.Drawing.Color.MediumBlue;
RD[ndrop].Size = new Size(5, 5);
RD[ndrop].Location = new Point(l, 0);
RD[ndrop].LocationChanged += pb_LocationChanged;
this.Controls.Add(RD[ndrop]);
ndrop++;
dropIt();
}
void pb_LocationChanged(object sender, EventArgs e)
{
// FORM_LASTBOUND is the Y-Axis point after which you wanted to remove the picturebox.
if ((sender as PictureBox).Top > FORM_LASTBOUND)
{
this.Controls.Remove(sender as PictureBox);
}
}

Categories

Resources