I am thinking of finding an easy way to put numbers into an array using a for loop. I have made the easy design with text showing how the name system works!
My code is like this:
double[,] kast = new double[3, 8];
string[] navn = new string[8];
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSorter_Click(object sender, EventArgs e)
{
for(int i = 1; i > 4; i++)
{
for(int x = 1; i > 9; i++)
{
kast[i, x] = Convert.ToDouble(txtKast + i + x + .Text);
}
}
}
I don't know how I will make it work with the Convert.ToDouble part since "kast" is a double array.
Related
private void button5_Click(object sender, EventArgs e)
{
int gTotal = 1;
for (int gCount = 0; gCount < listBox3.Items.Count - 1; gCount++)
gTotal += (listBox3.Items.Add(gCount));
label1.Text = gTotal.ToString();
}
Hey guys i'm not entirely sure of how to use Listboxes, but my question is how can I display the numbers that populate in my list box 3 to go into the label?
private void button5_Click(object sender, EventArgs e)
{
int gTotal = 1;
var collection = listBox3.Items.Cast<String>().ToList();
for (int gCount = 0; gCount < collection.Count - 1; gCount++)
{
int item;
if (int.TryParse(collection[gCount], out item)
{
gTotal += item;
}
}
label1.Text = gTotal.ToString();
}
Cast the items to a List then make sure that items are ints, if so add them up.
Parse your items.
private void button5_Click(object sender, EventArgs e)
{
int gTotal = 1;
for (int gCount = 0; gCount < listBox3.Items.Count; gCount++)
gTotal += int.Parse(listBox3.Items[gCount].ToString());
// assuming all items in the listbox is an int.
label1.Text = gTotal.ToString();
}
What trying to do
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();
public void GridCreate()
{
Graphics g = pictureBox1.CreateGraphics();
SolidBrush brushBlack = new SolidBrush(Color.Black);
Rectangle[,] block = new Rectangle[16, 16];
for (int i = 0; i <= block.GetLength(0) - 1; i++)
{
for (int n = 0; n <= block.GetLength(0) - 1; n++)
{
block[n, i] = new Rectangle(i * blockSize, n * blockSize, 20, 20);
g.FillRectangle(brushBlack, block[n, i]);
}
}
data.block = block;
}
private void Form1_Shown(object sender, EventArgs e)
{
GridCreate();
}
I'm trying to make a grid in WindowsForms using PictureBox, but related code is not working correctly.This data.block = block; part works, but this g.FillRectangle(brushBlack, block[n, i]); doesn't work at all. I think the problem is in the Form1_Shown event, because this:
private void Form1_Click(object sender, EventArgs e)
{
GridCreate();
}
executes perfectly fine.
Override protected override void OnShown(EventArgs e) gives the same result as Form1_Shown.
The problem is CreateGraphics(), which is a temporary surface that gets erased when the PictureBox refreshes itself.
Just create the grid once, then draw the data in the Paint() event:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GridCreate();
pictureBox1.Paint += pictureBox1_Paint;
}
private void GridCreate()
{
Rectangle[,] block = new Rectangle[16, 16];
for (int i = 0; i < block.GetLength(1); i++) // this is the 2nd dimension, so GetLength(1)
{
for (int n = 0; n < block.GetLength(0); n++) // this is the 1st dimension, so GetLength(0)
{
block[n, i] = new Rectangle(i * blockSize, n * blockSize, 20, 20);
}
}
data.block = block;
}
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics; // use the SUPPLIED graphics, NOT CreateGraphis()!
for (int i = 0; i < data.block.GetLength(1); i++) // this is the 2nd dimension, so GetLength(1)
{
for (int n = 0; n < data.block.GetLength(0); n++) // this is the 1st dimension, so GetLength(0)
{
g.FillRectangle(Brushes.Black, data.block[n, i]);
}
}
}
How to access array from another method in this example? I'm new in c# and i really appreciate if you will help me. Thanks in advance!
private void button1_Click(object sender, EventArgs e) {
int[] array1 = new int[5];
for (int i = 0; i < 5; i++) {
array1[i] = i;
}
}
private void button2_Click(object sender, EventArgs e) {
int[] array2 = new int[5];
for (int i = 0; i < 5; i++) {
array2[i] = array1[i];
}
}
As Jon Skeet mentioned in his comment, local variables are by definition local to the method which defines them. If you want something to be visible globally, you need to make it an instance variable, or return it from the method.
Assuming that only array1 needs to be visible, it suffices to declare that one outside.
// Declare the array globally.
int[] array1 = new int[5];
private void button1_Click(object sender, EventArgs e)
{
// Initially the array inside this method.
for(int i=0;i<5;i++)
array1[i]=i;
}
private void button2_Click(object sender, EventArgs e)
{
int[] array2 = new int[5];
// Copy from the global array
for(int i=0;i<5;i++)
{
array2[i]=array1[1];
}
}
Declare both arrays outside to enable access.
int[] array1, array2;
private void button1_Click(object sender, EventArgs e) {
array1 = new int[5];
for (int i = 0; i < 5; i++) {
array1[i] = i;
}
}
private void button2_Click(object sender, EventArgs e) {
array2 = new int[5];
for (int i = 0; i < 5; i++) {
array2[i] = array1[i];
}
}
I have a folder browser button and a combobox which shows selected path. I want it keep previously selected paths. I wrote this code and it works fine. But I wanna know if there is more efficient way to do this.
private void Form1_Load_1(object sender, EventArgs e)
string hurrem, hurrem2, hurrem3, hurrem4, hurrem5;
hurrem = Settings.Default["com1"].ToString();
hurrem2 = Settings.Default["com2"].ToString();
hurrem3 = Settings.Default["com3"].ToString();
hurrem4 = Settings.Default["com4"].ToString();
hurrem5 = Settings.Default["com5"].ToString();
comboBox2.Items.Add(hurrem);
comboBox2.Items.Add(hurrem2);
comboBox2.Items.Add(hurrem3);
comboBox2.Items.Add(hurrem4);
comboBox2.Items.Add(hurrem5);
comboBox2.SelectedIndex = 0;
and
private void button1_Click(object sender, EventArgs e)
{
//..code
Settings.Default["com5"] = Settings.Default["com4"];
Settings.Default["com4"] = Settings.Default["com3"];
Settings.Default["com3"] = Settings.Default["com2"];
Settings.Default["com2"] = Settings.Default["com1"];
Settings.Default["com1"] = path1.ToString();
Settings.Default.Save();
You can use the LimitedStack from here: Limit size of Queue<T> in .NET?. You could then add methods for serializing/deserializing the data using Settings.Default.
public void Save()
{
int i = 0;
foreach (var item in _stack)
{
Settings.Default["com" + i++] = item;
}
Settings.Default.Save();
}
public void Load()
{
for (int i = 0; i < Limit; i++)
{
_stack.Add((T)Settings.Default["com" + i++]);
}
}
Storing the items (assuming you have the limited stack saved in a variable called myStack):
myStack.Push(path1.ToString());
myStack.Save();
Adding the items to the combobox:
for (int i = 0; i < myStack.Limit; i++)
{
comboBox2.Items.Add(myStack.Pop());
}