show items of an array in MessageBox - c#

I'm having a problem printing out an array that I have created through a function.
All it says in the MessageBox is System.int32[], what have I done wrong?
private int[] sekunder(int tid)
{
int sekunder, minuter, timmar;
sekunder = tid;
minuter = sekunder / 60;
timmar = minuter / 60;
int[] beräknaTid = { sekunder, minuter, timmar };
return beräknaTid;
}
private void button1_Click(object sender, EventArgs e)
{
int tid;
tid = Convert.ToInt32(textBox1.Text);
MessageBox.Show(Convert.ToString(sekunder(tid)));
}

try this:
Array Contains Multiple Elements You Need to Iterate through them
private void button1_Click(object sender, EventArgs e)
{
int tid;
tid = Convert.ToInt32(textBox1.Text);
foreach (var item in sekunder(tid))
{
MessageBox.Show(Convert.ToString(item));
}
// for comma separated
//use this : MessageBox.Show(string.Join(",",sekunder(tid)))
}

you can also join all values in your array and show them
private void button1_Click(object sender, EventArgs e)
{
int tid;
tid = Convert.ToInt32(textBox1.Text);
MessageBox.Show(string.Join(", ",sekunder(tid)));
}

Related

How to remove items in listbox that was randomly selected using timer

private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
timer2.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
int a = int.Parse(label3.Text)
+ 1;
label3.Text = a.ToString();
if (label3.Text == "10")
{
listBox1.Items.Add(label1.Text);
label3.Text = "0";
}
}
private void timer2_Tick(object sender, EventArgs e)
{
Random rnd = new Random();
int num = rnd.Next(1, listBox2.Items.Count);
label1.Text = num.ToString();
if (label3.Text == "10")
{
listBox1.Items.Add(label1.Text);
listBox2.Items.Remove(label1.Text); //this line of code isnt working. It doesnt delete anything.
}
}
What i wanted to happen is my label1 will shuffle all the items(1-10) in my listbox2 using timer1 then if the timer reach 10 seconds the listbox1 will add label1 last number as its item and listbox2 will remove it. It's working fine but it doesnt remove what's in the listbox2
You can use:
int i = listBox2.Items
.Cast<ListBoxItem>()
.ToList()
.FindIndex(x=>x==label1.Text);
listBox2.RemoveAt(i);
UPDATE:
Random rnd = new Random();
private void timer2_Tick(object sender, EventArgs e)
{
int num = rnd.Next(1, listBox2.Items.Count);
label1.Text = num.ToString();
if (label3.Text == "10")
{
listBox1.Items.Add(label1.Text);
listBox2.Items.Remove(label1.Text); //this line of code isnt working. It doesnt delete anything.
}
}

Making values to perform addition in a single textbox

I recently got stuck in my project as I have requirements that I need to fulfil, the need to perform addition in a single textbox.
I've view the most similar posts and it gave a good idea of it, Addition using a single TextBox.
Instead of int, I am required to use double like how it was done with int.
private int i = 0;
private int[] a = new int[2];
private void button1_Click(object sender, EventArgs e)
{
int b;
if(Int32.TryParse(textBox1.Text, out b))
{
a[i] = b;
i++;
textBox1.Text = "";
}
else
{
MessageBox.Show(#"Incorrect number");
}
}
private void resultbutton2_Click(object sender, EventArgs e)
{
int sum = a[0] + a[1];
MessageBox.Show("Sum: " + sum);
}
}
Instead, what code should I use to create a similar things for double?
If you want to keep your code, just do this :
private int i = 0;
private double[] a = new double[2];
private void button1_Click(object sender, EventArgs e)
{
double b;
if (Double.TryParse(textBox1.Text, out b))
{
a[i] = b;
i++;
textBox1.Text = "";
}
else
{
MessageBox.Show(#"Incorrect number");
}
}
private void resultbutton2_Click(object sender, EventArgs e)
{
double sum = a[0] + a[1];
MessageBox.Show("Sum: " + sum);
}
But you can try this for adding more than 2 doubles :
private double result = 0.0;
private void button1_Click(object sender, EventArgs e)
{
double b;
if (Double.TryParse(textBox1.Text, out b))
{
result += b,
textBox1.Text = "";
}
else
{
MessageBox.Show(#"Incorrect number");
}
}
private void resultbutton2_Click(object sender, EventArgs e)
{
MessageBox.Show("Sum: " + result);
}
double b = 0;
try{
b = Convert.ToDouble(textBox1.Text);
}
catch(e){
// Error Handling
}
Docs: https://learn.microsoft.com/en-us/previous-versions/windows/apps/zh1hkw6k(v=vs.105)
you can try use something like:
Double.Parse("1.2");
some examples here:
https://msdn.microsoft.com/en-us/library/fd84bdyt(v=vs.110).aspx

Adding textbox values to an array not working C#

I'm trying to add textbox and numericUpandDown values to an array, but it doesn't seem to be working.
Carro []carros = new Carro[1];
private Carro carro;
public Form1()
{
..
}
private void Form1_Load(object sender, EventArgs e)
{
..
}
private void AdicionarCarro()
{
this.carro = new Carro(textboxCor.Text, textboxMarca.Text, textboxModelo.Text,
(int.Parse(numUpDownCilindrada.Text)), (int.Parse(numUpDownVelocidade.Text)));
}
private Carro[] AdicionarArray(Carro carro, Carro[] array)
{
AdicionarCarro();
int novoTamanho = array.Length + 1;
Carro[] carros = new Carro[novoTamanho];
for (int i = 0; i < array.Length; i++)
{
carros[i] = array[i];
}
carros[novoTamanho] = carro;
return carros;
}
private void buttonGravar_Click(object sender, EventArgs e)
{
AdicionarArray(carro, carros);
}
When I type the values and click on the "buttonGravar", it gives me this
Error:
I'd be much delighted to get some tips/help on it.
Using System.Collection.Generic.List<T> would be much simpler, since it doesn't have a fixed size:
List<Carro> carros = new List<Carro>();
carros.AddRange(array);
carros.Add(carro);
return carros;
Better way:
private List<Carro> Carros;
public Form1()
{
Carros = new List<Carro>();
..
}
private void Form1_Load(object sender, EventArgs e)
{
..
}
private void AdicionarCarro()
{
var carro = new Carro(textboxCor.Text, textboxMarca.Text, textboxModelo.Text,
(int.Parse(numUpDownCilindrada.Text)), (int.Parse(numUpDownVelocidade.Text)));
Carros.Add(carro);
}
private void buttonGravar_Click(object sender, EventArgs e)
{
AdicionarCarro();
}
To help you understand your code:
carros[novoTamanho] = carro;
should be
carros[novoTamanho - 2] = carro;
Reason:
Array index starts from 0. novoTamanh represents new length (starting at 1, not 0 unlike index), which is outside array.
It's an index out of range exception because your array Carro is of size tmanho:
Carro[] carros = new Carro[novoTamanho];
and carros can contain exactly "novoTamanho" items indexed from "0" to "novoTamanho -1"
You can simply solve this by defining:
int novoTamanho = array.Length + 2;
Or if you do not want to manage indexes, use Lists:
List<Carro> listCarro = new List<Carro>;
listCarro.AddRAnge(array);
listCarro.Add(carro);
return listCarro.ToArray();

Add string to same textBox instead of current string

I have form which should change text inside textbox on button click. Text should be read from List, k.pojam. So on first click to the button text Box should show first element in list, second click second element etc. How to fix problem ?
//List<Karta> list; this list already has some number of Karta objects
int cardCounter=0;
private void btnNext_Click(object sender, EventArgs e)
{
int currentCounter = 0;
foreach (Karta k in list)
{
if(cardCounter==currentCounter)
{
txtBoxPojam.Text = k.Pojam;
txtBoxPojam.Show();
cardCounter++;
}
currentCounter++;
}
edit txtBoxPojam.Text += k.Pojam; // I need something like this, but instead of adding string I would like to write another string instead of current, because obviously txtBoxPojam.Text = k.Pojam; doesnt work?
int mainCounter = 0;
int totalItems = 0;
List<string> LstItems = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
LstItems.Add("Test Item 1");
LstItems.Add("Test Item 2");
LstItems.Add("Test Item 3");
LstItems.Add("Test Item 4");
LstItems.Add("Test Item 5");
totalItems = LstItems.Count();
}
private void btnNext_Click(object sender, EventArgs e)
{
try
{
if (mainCounter > totalItems)
{
//your implementation
return;
}
txtNext.Text = LstItems[mainCounter].ToString();
mainCounter++;
}
catch (Exception)
{
}
}
Instead of txtBoxPojam.Text = k.Pojam; working combination is txtBoxPojam.Text = string.Empty; and txtBoxPojam.Text += k.Pojam;
txtBoxPojam.Text = string.Empty;
int cardCounter=0;
private void btnNext_Click(object sender, EventArgs e)
{
int currentCounter = 0;
foreach (Karta k in list)
{
if(cardCounter==currentCounter)
{
txtBoxPojam.Text += k.Pojam;
txtBoxPojam.Show();
cardCounter++;
}
currentCounter++;
}

Increment number every time button is click

I want to increase an int variable i whenever I click on a button. But what I get is only int value of 1 and it doesn't increase anymore.
Here is my code:
private int i;
protected void btnStart_Click(object sender, EventArgs e)
{
i++;
lblStart.Text = i.ToString();
}
By each request (Clicking on the button), a new instance will be created.
So your non-static variable will be reset to 0.
You can define i as static:
private static int i;
protected void btnStart_Click(object sender, EventArgs e)
{
i++;
lblStart.Text = i.ToString();
}
But please note that the i variable is shared between all the users.
To improve this issue, you can use Session.
Session is an ability to store data of each user in a session.
So you can use following property to change the i variable in each session:
private int i
{
get
{
if (Session["i"] == null)
return 0;
return (int)Session["i"];
// Instead of 3 lines in the above, you can use this one too as a short form.
// return (int?) Session["i"] ?? 0;
}
set
{
Session["i"] = value;
}
}
protected void btnStart_Click(object sender, EventArgs e)
{
i++;
lblStart.Text = i.ToString();
}
As you know other answer is correct i want add another answer
You must in webform save your variables in ViewState
Just define your variables like this
public int i
{
get { Convert.ToInt32( ViewState["i"] ); }
set { ViewState["i"] = value ; }
}
Convert lblStart.Text value to int every time and assign it to i. Then increase i.
private int i;
protected void btnStart_Click(object sender, EventArgs e)
{
i = Int32.Parse(lblStart.Text);
i++;
lblStart.Text = i.ToString();
}
I have similar questions as yours and I believe the issue is because the event click did not store the value that has been increased before, therefore it could not be incremented the next time you clicked, so here's my code:
protected void btn_add_Click(object sender, EventArgs e)
{
string initial;
int increment;
int quantity;
initial = TextBoxQty.Text;
increment = Convert.ToInt16(initial);
increment++;
TextBoxQty.Text = increment.ToString();
quantity = increment;
}
You can use a hidden field, initialize them to 0.
private int i;
protected void btnStart_Click(object sender, EventArgs e)
{
i = int.Parse(myHiddenField.Value);
i++;
myHiddenField.Value = i;
lblStart.Text = i.ToString();
}
protected static int a = 0;
protected void btnStart_Click(object sender, EventArgs e)
{
a = a+1;
lblStart.Text = i.ToString();
}
It Works for me but on page_load() it initiates the value from 1 again !
this is actually my first time doing this
int i = 0;
while (i>=0)
{
Console.WriteLine(i);
Console.ReadLine();
i++;
}

Categories