can anyone help me please,,
i'm trying to display my array elements into textbox and listview
the listview works but the textbox shows my int 6 time, here is my code:
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ArrayList numbers = new ArrayList();
int[] myNumbers = new int[6];
Random rnd = new Random();
int randomNumber;
for (int i = 0; i < 6; i++)
{
do
{
randomNumber = rnd.Next(1, 49);
}
while (numbers.Contains(randomNumber));
numbers.Add(randomNumber);
}
numbers.Sort();
numbers.CopyTo(myNumbers);
//listView1.Items.Add(myNumbers.ToString);
foreach (int j in myNumbers)
{
listView1.Items.Add(j.ToString());
label1.Text += j.ToString();
int num1 = myNumbers[0];
int num2 = myNumbers[1];
int num3 = myNumbers[2];
int num4 = myNumbers[3];
int num5 = myNumbers[4];
int num6 = myNumbers[5];
textBox1.Text += num1;
textBox2.Text += num2;
textBox3.Text += num3;
textBox4.Text += num4;
textBox5.Text += num5;
textBox6.Text += num6;
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
listView1.Clear();
label1.Text = "";
}
}
}
can anyone help me and tell me where am i going wrong?
You should do
textBox1.Text = num1;
not
textBox1.Text += num1;
Edit: You don't need loop...
you're setting the texts within the loop. So you're setting the values six times, which you can see because you're appending with +=. You could move this whole block to just after numbers.CopyTo(myNumbers); so each value is only set once. Also there's no need for +=.
int num1 = myNumbers[0];
int num2 = myNumbers[1];
int num3 = myNumbers[2];
int num4 = myNumbers[3];
int num5 = myNumbers[4];
int num6 = myNumbers[5];
textBox1.Text = num1.ToString();
textBox2.Text = num2.ToString();
textBox3.Text = num3.ToString();
textBox4.Text = num4.ToString();
textBox5.Text = num5.ToString();
textBox6.Text = num6.ToString();
There were few issue in your code.
You don't need both ArrayList numbers and int[] myNumbers, you can access List by index too.
You use so much magic number : 6, 1, 49. They could be replace with variable.
Before adding value to a listView, you can clear it. You don't have to foreach loop on myNumbers you can direcly add them with AddRange method.
The result of label1.Text+=j.ToString() is a bit undreadable as all numbers glued together resulting in a 123564132321.
tbResult.Text = string.Join(", ", numbers); will join then with a nice ", " as separator.
Finaly, if you you don't want to copy past N time the same line for textbox.text initialisation. You can find all the textbox that match a pattern.
var numbers = new List<int>();
while (numbers.Count < userInput)
{
var tempRnd = rnd.Next(lowerBound, upperBound);
if (!numbers.Contains(tempRnd))
{
numbers.Add(tempRnd);
}
}
numbers.Sort();
listView1.Items.Clear();
listView1.Items.AddRange(numbers.Select(x => new ListViewItem { Text = x.ToString() }).ToArray());
tbResult.Text = string.Join(", ", numbers);
int i = 0;
foreach (TextBox tb in this.Controls.OfType<TextBox>().Where(l => l.Name.StartsWith("GeneratedTB_")))
{
tb.Text = numbers[i].ToString();
i++;
}
Related
i've made an array with numbers.
i want to ad up the numbers form the array and show the result in a textbox.
i've to use the foreach loop.
i've already searched on the internet but i can't find anything that helped me.
this is my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnsortar_Click(object sender, EventArgs e)
{
//opdr 9
int[] getallen = { 4,5,9,8,31,44,76,63,88,59 };
int index = 1;
int lengteArray = getallen.Length;
tbsortar.Text = getallen[0].ToString();
while (index < lengteArray)
{
Array.Sort(getallen);
tbsortar.Text = tbsortar.Text + "," + getallen[index].ToString();
index++;
}
//opdr 10
string[] namen = { "kees", "jan", "piet", "henk" };
Array.Sort(namen);
int lengteArray1 = namen.Length;
tbopdr10.Text = namen[0];
for (int i=1; i<lengteArray1; i++)
{
string newLine = Environment.NewLine;
tbopdr10.Text = tbopdr10.Text +newLine + namen[i] ;
}
//opdr 11
double totaal = 0;
foreach(int getal in getallen)
{
//toaal van getallen is 392
totaal += getal;
tbopdr11.Text = tbopdr11.Text + totaal;
}
}
}
Try this:
int[] Numbers = { 1, 2, 3 };
int Result = 0;
for (int i = 0; i < Numbers.Length; i++)
{
Result += Numbers[i];
}
TextBox1.Text = System.Convert.ToString(Result);
The textbox's name is TextBox1.
can't you just use sum?
getallen.Sum()
https://dotnetfiddle.net/sTn1kp
public void caesarcipher()
{
char c, a;
string PT = textBox1.Text;
PT = PT.ToLower();
for (int i = 0; i < PT.Length; i++)
{
c = Convert.ToChar(PT.Substring(i, 1));
if ((int)c + 3 > 122) a = (char)(c + 3 - 26);
else a = (char)(c + 3);
label2.Text = a; //error here as label can not display char
}
}
private void button1_Click(object sender, EventArgs e)
{
caesarcipher();
}
That is my partial code, what I want is that to display all the elements stored in char a in a label and converting the char a to string displays only the last element which does not help.
I hope I am clear enough.
This did the trick thanks to #BradleyUffner and #ChrisDunaway
public void caesarcipher()
{
char c, a;
string PT = textBox1.Text;
PT = PT.ToLower();
label2.Text = " "; //added this line
for (int i = 0; i < PT.Length; i++)
{
c = PT[i]; //changed
//c = Convert.ToChar(PT.Substring(i, 1));
if ((int)c + 3 > 122) a = (char)(c + 3 - 26);
else a = (char)(c + 3);
label2.Text += a.ToString(); //changes here solved the error
}
}
You just need to add each character to the label2.Text property instead of reassigning the property. You can also get rid of some of the explicit casting; char will get cast to an int implicitly when added to an `int:
public void CaesarCipher()
{
label2.Text = "";
foreach (var character in textBox1.Text.ToLower())
{
var intVal = character + 3;
label2.Text += (char)(intVal > 122 ? intVal - 26 : intVal);
}
}
Unless I'm missing something, a will ever only hold a single char since you defined it as char a and are setting it equal to a char.
For this, you would need to change the definitions to char[] a = new char[PT.Length]
Instead calling a =, you would then have to use the index to set the char at the location to what you want. Replacing a = with a[i] = would suffice.
To display the char array, label2.Text = new string(a) will do the trick.
public void caesarcipher()
{
string PT = textBox1.Text;
PT = PT.ToLower();
char c;
char[] a = new char[PT.Length];
for (int i = 0; i < PT.Length; i++)
{
c = Convert.ToChar(PT.Substring(i, 1));
if ((int)c + 3 > 122) a[i] = (char)(c + 3 - 26);
else a[i] = (char)(c + 3);
}
label2.Text = new string(a);
}
private void button1_Click(object sender, EventArgs e)
{
caesarcipher();
}
I want to generate 1000 number randomly and put the result in a rich text box ,but the result I got from my code is just one number appearing in the rich text box !!
private Random _random = new Random();
int min = 000000;
int max = 999999;
string s;
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 1000; i++)
{
s = _random.Next(min, max).ToString("D6") + "\n";
}
richTextBox1.Text = s;
}
You are overriding the value of s each time you get your next number. Instead you have to add the number to a list. Something like this would work.
List<string> numbers = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 1000; i++)
{
numbers.Add(_random.Next(min, max).ToString("D6"));
}
richTextBox1.Text = string.Join(Environment.NewLine, numbers);
}
As most of the answers here using the .net class Random i would not use it, because in a direct comparison it doesn't creates strong random numbers.
Example:
So if you want strong random numbers you should refrain from using Random and use the RNGCryptoServiceProvider from the namesapace System.Security.Cryptography
ExampleCode:
private RNGCryptoServiceProvider _random = new RNGCryptoServiceProvider ();
int min = 000000;
int max = 999999;
private void Form1_Load(object sender, EventArgs e)
{
int[] results = new int[1000];
var buffer = new byte[4];
int min = 100000;
int max = 999999;
for (int i = 0; i < results.Length; i++) {
while(results[i] < min || results[i] > max)
{
_random.GetBytes(buffer);
results[i] = BitConverter.ToInt32(buffer, 0);
}
richTextBox1.Text += results[i].toString();
}
}
You actually need to concatenate the result with previous calculated result, right now it is replacing the string value in s every time loop executes and you end up only with the last value in s, a quick fix is to use contatination using +:
for (int i = 0; i < 1000; i++)
{
s+= _random.Next(min, max).ToString("D6") + "\n"; // now it keeps previous values as well
}
Problem is that you actually overwrite at each iteration the string s. You need to append the number to the old ones.
for (int i = 0; i < 1000; i++)
{
s += _random.Next(min, max).ToString("D6") + "\n";
}
richTextBox1.Text = s;
You could also use AppendText method
for (int i = 0; i < 1000; i++)
{
richTextBox1.AppendText(_random.Next(min, max).ToString("D6") + "\n");
}
Suggestion by Matthew Watson: When generating such a large string it is very adviseable to use a StringBuilder. Is has much better performance than a normal concatenation of strings:
StringBuilder sb = new StringBuilder(8000);
for (int i = 0; i < 1000; i++)
{
sb.AppendLine(_random.Next(min, max).ToString("D6"));
}
richTextBox1.Text = sb.ToString();
s += _random.Next(min, max).ToString("D6") + "\n";
^
|
---- You're missing this plus sign
on this line of code you override the Text of richTextBox1
for (int i = 0; i < 1000; i++)
{
s = _random.Next(min, max).ToString("D6") + "\n";
}
richTextBox1.Text = s;
just change it to (add a + after s)
for (int i = 0; i < 1000; i++)
{
s += _random.Next(min, max).ToString("D6") + "\n";
}
richTextBox1.Text = s;
Note that the maxValue or Random.Next is exclusive, so 999999 is never genereted.
var numbers = Enumerable.Repeat(new Random(), 1000)
.Select(r => r.Next(1000000).ToString("D6")); // the same new Random() instance is used for all .Next
richTextBox1.Text = string.Join("\r\n", numbers);
Or a bit more efficient:
richTextBox1.Text = Enumerable.Repeat(new Random(), 1000).Aggregate(new StringBuilder(7000)
, (b, r) => b.AppendFormat("{0:D6}\n", r.Next(1000000))).ToString(0, 6999);
just You need to add ' + ' for the richtextbox1 as like below
try this one
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 1000; i++)
{
s = _random.Next(min, max).ToString("D6") + "\n";
richTextBox1.Text + = s;
}
}
I want to add the elements from the textboxes to the array but every time I try adding the names it throws an exception. I want to add the elements after the button is clicked.
private void Grade_Click(object sender, EventArgs e)
{
string name;
double grade;
nametextbox.Text = "";
gradetextbox.Text = "";
for (int i = 0; i < 10; i++)
{
grade = Convert.ToDouble(gradetextbox.Text);
grades[i] += grade;
name = nametextbox.Text;
names[i] += name;
}
}
You do this:
gradetextbox.Text = "";
Which sets the Text property to an empty string. Then you do this:
grade = Convert.ToDouble(gradetextbox.Text);
which is trying to convert the empty string to a double, which clearly does not work so it throws an exception.
What you probably need is this:
private double[] grades = new double[10]; // 10 grades
private string[] names = new string[10]; // 10 names
private int currentIndex = 0;
private void Grade_Click(object sender, EventArgs e)
{
if (currentIndex > 10)
{
MessageBox.Show("No more enteries allowed");
}
string name;
double grade;
if (double.TryParse(gradetextbox.Text, out grade))
{
grades[currentIndex] += grade;
}
else
{
MessageBox.Show("Grade enterd is not a valid grade");
return;// do nothing else
}
name = nametextbox.Text;
names[i] += name;
// now clear the textboxes so user can enter another value
nametextbox.Text = "";
gradetextbox.Text = "";
currentIndex = currentIndex + 1;
}
In a math game that I programming, my Windows Form has labels in it where the numbers appear for the sum.
When I click button1, they should change to other numbers, but they don't. The labels that should change are called lblNumber1 and lblNumber2.
Here is my code:
public partial class plussommen : Form
{
int counter = 0;
int correct = 0;
int incorrect = 0;
Random rand = new Random();
int number1, number2;
int answer;
string sum;
public plussommen()
{
InitializeComponent();
number1 = rand.Next(1, 50);
number2 = rand.Next(1, 50);
sum = number1 + " + " + number2 + " = ";
answer = number1 + number2;
}
private void plussommen_Load(object sender, EventArgs e)
{
if (counter < 5)
{
lblNumber1.Text = number1.ToString();
lblNumber2.Text = number2.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
lblCorrect.Text = correct.ToString();
lblIncorrect.Text = incorrect.ToString();
if (textBox1.Text == answer.ToString())
{
MessageBox.Show("Answer correct!");
correct = correct + 1;
}
else
{
MessageBox.Show("Answer incorrect!");
incorrect = incorrect + 1;
}
if (counter < 5)
{
lblNumber1.Text = number1.ToString();
lblNumber2.Text = number2.ToString();
}
else
{
if (counter == 5)
{
MessageBox.Show("You've made five sums, choose another kind of sum.");
this.Close();
Form1 menu = new Form1();
menu.Show();
}
}
}
}
To change the value on the labels on every click, you need to generate new random numbers each time.
if (counter < 5)
{
number1 = rand.Next(1, 50);
number2 = rand.Next(1, 50);
lblNumber1.Text = number1.ToString();
lblNumber2.Text = number2.ToString();
}
In your code, you are not changing the values of number1 and number2 at any time after initialization. This is why they are not updating after every button click.
What you need to do is update the numbers and the answer after every click:
private void button1_Click(object sender, EventArgs e)
{
//...
if (counter < 5)
{
number1 = rand.Next(1, 50);
number2 = rand.Next(1, 50);
answer = number1 + number2;
lblNumber1.Text = number1.ToString();
lblNumber2.Text = number2.ToString();
}
//...
}