I want to add 23 picture boxes which I have been using in my form . I tried this way but it did not work:
List<PictureBox> pbList = new List<PictureBox>();
for (int i = 0; i < 23; i++)
pbList.Add(pictureBox +i);
You are trying to add them by name, you have to do a find:
List<PictureBox> pbList = new List<PictureBox>();
for (int i = 0; i < 23; i++)
pbList.Add((PictureBox)Controls.Find("pictureBox" + i));
Edit: If your picture boxes are nested within other controls, you will have to pass "true" to the Find() method so that all children are searched. The other solutions (including mine) will only search for picture boxes that are directly within the control container "Controls"
var pbList = Controls.OfType<PictureBox>().Select(a => a.Name.StartsWith("pictureBox"));
Related
So, I simply want to dynamically change my what label is currently edited through an array, to achieve as little code as possible.
This is what it looks like right now:
string[] poängLabels = new string[10];
for (int i = 3; i <= 9; i++)
{
poängLabels[i] = ("label{0}.Text" + i);
}
You should do it like this:
poängLabels[i] = ((Label)this.Controls.Find("label" + i)).Text;
or
poängLabels[i] = ((Label)this.Controls["label" + i]).Text;
You have to be able to enumerate controls somehow, putting (or having them) in the array is one option:
var result = new { label1, label2, ... }.Select(label => label.Text).ToArray();
My code is C# windows form
I have a file with data:
David One And Two/Three
Alex One Two Four And Five/Six
Amanda Two Seven/Ten
Micheal Seven/Nine
and trying to have them in array like
string[] Names = File.ReadAllLines("C:\Students\Name.txt", Encoding.Default);
and have them in Group of Radio Buttons
RadioButton[] n = new RadioButton[Names.Length];
for (int i = 0; i < Names.Length; i++)
{
n[i] = new RadioButton();
n[i].Text = Names[i];
n[i].Location = new Point(10, 10 + i * 20);
groupBox1.Controls.Add(n[i]);
}
But it shows as my attached image
I tried without Encoding.Default and Encoding.UTF8 but the same problem.
What is I'am doing wrong? Please see my image and help me. Thank you in advance!
I'm guessing your file contains empty lines at the end. You can try to remove them before creating a radio button:
for (int i = 0; i < Names.Length; i++)
{
var name = Names[i];
if(name.Trim() == string.Empty) continue;
n[i] = new RadioButton();
n[i].Text = Names[i];
n[i].Location = new Point(10, 10 + i * 20);
groupBox1.Controls.Add(n[i]);
}
If your text is trimmed you can try to increase the width of the control:
n[i].Width = 300; // Put a value which will show the entire text
I want to make button array that include string and integer. I have 30 buttons and named B1, B2, ...., B30 and I want to change their color depend on the counter value. How can I do that? These are what I have done and I stuck
for(Cnt = 0; cnt < 30; cnt++)
{
Button[] Tombol = new Button[]{"B"+(cnt+1)};
Tombol[cnt].BackColor = Color.Red
}
The Control (custom) initialization in the Form (in your case, the Control being a Button) requires more than a simple declaration. Apart from giving name (which you do), two other important things to do are:
To add it to the Control parent
To locate it nicely
Thus, adding those considerations to the Button you are to create, you could do something like this
int noOfBtns = 30;
Button[] Tombols = new Button[30]; //actually, you may not need this at all
for (int cnt = 0; cnt < numOfBtns; cnt++)
{
Button tombol = new Button();
tombol.BackColor = Color.Red;
tombol.Location = new Point(5, cnt * 25); //Read (2) please formulate this more properly in your case
tombol.Name = "B" + (cnt + 1).ToString();
// others like size (maybe important depending on your need), color, etc
this.Controls.Add(tombol); //Read (1) this refers to the `Form` if the parent control you want to put your button to is `Form`. But change the `this` as you see fit
Tombols[cnt] = tombol; //again, actually you may not need this at all
}
Take care of how you formulate the location of your button, very important. The example I gave above is really simple formulation, which might not fit if your number of buttons grow large. But that gives you the basic idea on how important it is to set the location of the Button right.
You may need the array of Button at all, unless for some reason you want to list it. But even you want to list it, you should use List and List.Add instead of Array.
I suggest using Linq to generate the array:
Button[] Tombol = Enumerable
.Range(0, 30)
.Select(i => new Button() {
Text = String.Format("B{0}", i + 1),
BackColor = Color.Red,
//TODO: compute desired color as function of "i" like
// BackColor = Color.FromArgb(i * 8, 255 - i * 8, 128),
//TODO: assign Parent, Location, Size etc. like this:
// Parent = this,
// Location = new Point(10 + 40 * i, 10),
// Size = new Size(35, 20),
})
.ToArray();
I mean like this:
Button[] Tombol = new Button[30];
for(cnt = 0; cnt < 30; cnt++)
{
Tombol[cnt] = new Button
{
Text = "B" + (cnt+1),
BackColor = Color.Red
};
}
First you create the array and in the for loop you instantiate the actual buttons one by one.
You have to initialize the button array first
int numOfBtns = 30;
Button[] Tombol = new Button[numOfBtns];
and after you can fill required items
for (int cnt = 0; cnt < numOfBtns; cnt++)
{
// Name, Content, Foreground .. whatever
Tombol[cnt].Name = "B" + (cnt + 1);
}
At the moment you are recreating your array on each loop of your code rather than creating the array outside of the loop and then adding buttons to it in the loop.
In addition all your buttons are being created in the same position, so they are on top of each other meaning you can only see one of them.
Try something like this, based on a modified version of the code from this previous answer which creates the buttons, adds them to a list you can search and the sets the positions of the buttons too:
int top = 50;
int left = 100;
var buttons = new Dictionary<string, Button>();
for (int i = 0; i < 30; i++)
{
Button button = new Button();
buttons.Add("B"+i, button);
button.Left = left;
button.Top = top;
this.Controls.Add(button);
top += button.Height + 2;
button.BackColor = Color.Red;
}
You can then refer to an individual button later with the code:
buttons["B3"].BackColor= Color.Green;
I have a 16 element int array and 16 textboxes (textBox1, textBox2 ....) that look like 4x4 matrix. Is there any way to put textboxes values to every array element not using code like this:
array[1] = (int)textBox1.Text;
array[2] = (int)textBox2.Text;
One possibility would be to store the references to the TextBox instances in an array.
TextBox[] Boxes;
And then use a 'for' loop to populate the values.
for (int i = 0; i < 16; i++)
{
array[i] = (int)Boxes[i].Text;
}
You could use a function to get the text box's text as an integer using it's "index" from the form's control collection:
int GetBoxText(int index)
{
return Convert.ToInt32(this.Controls["textBox" + i.ToString()].Text);
}
Note that this has no error checking of any kind. You could add some if you wanted to. All this does is get the text of the control named textBox + whatever i is from the form's control collection and convert it to an integer.
IMHO the best way to design is by the way it's meant to be. In particular, rectangular/multidimensional arrays may be useful in this scenario:
public partial class Form1 : Form {
TextBox[,] textBoxes;
int[,] values;
public Form1() {
InitializeComponent();
textBoxes = new TextBox[4, 4];
values = new int[textBoxes.GetLength(0), textBoxes.GetLength(1)];
for(int r = 0; r < textBoxes.GetLength(0); r++) {
for(int c = 0; c < textBoxes.GetLength(1); c++) {
values[r, c] = int.Parse(textBoxes[r, c].Text);
}
}
}
}
I have a program that generates a grid of buttons and i want to store these buttons in an nested array list but the way I am doing does not seem to be correct
This is my code:
List<List<Button>> buttonsList = new List<List<Button>>();
List<Button[]> rowList = new List<Button[]>();
//Some method that creates a grid of buttons
buttons = new Button[row][];
for (int r = 0; r < row; r++)
{
buttons[r] = new Button[col];
buttonsList.Add(rowList[r]);
for (int c = 0; c < col; c++)
{
buttons[r][c] = new Button();
rowList[c].Add(buttons[r][c]);
}
}
First thing you need to understand is that a List<T> and a T[] are not equivalent* (*see footnote). Your code seems to want to treat a list as an array, and that's just not going to work.
A List<List<Button>> can be used in the following manner:
var listOfLists = new List<List<Button>>();
var listOfButtons = new List<Button>();
listOfButtons.Add(new Button());
listOfLists.Add(listOfButtons);
And you can continue to add buttons to the button list, create more button lists, add buttons to those, and add all of them to the list of lists and you can do all of this in a loop or nested loops.
An array Button[][] is treated differently. It's not dynamically sized, and would be used in a different manner.
var arrayOfArrays = new Button[10][];
var arrayOfButtons = new Button[3];
arrayOfButtons[0] = new Button();
arrayOfButtons[1] = new Button();
arrayOfButtons[2] = new Button();
arrayOfArrays[0] = arrayOfButtons;
And then you still have 9 more arrays of buttons you could set (in this example).
Your code attempts to add an array of buttons to a list expecting a list of buttons. And there are a few other items to sort through, but the basic idea is that you are mixing up these two different collections, and the compiler is loudly complaining.
To fix your code, begin by employing the proper types for what you need to do, and then consistently and correctly use those types in your logic.
As a starter, see how far you get with these snippets.
// using List<List<Button>>
var buttonLists = new List<List<Button>>();
for (int r = 0; r < row; r++)
{
var buttonRow = new List<Button>();
buttonLists.Add(buttonRow);
for (int c = 0; c < col; c++)
{
buttonRow.Add(new Button());
}
}
// using Button[][]
var buttonArrays = new Button[row][];
for (int r = 0; r < row; r++)
{
buttonArrays[r] = new Button[col];
for (int c = 0; c < col; c++)
{
buttonArrays[r][c] = new Button();
}
}
*Unless you are referencing them via a common interface type, in which case they still aren't equivalent but are compatible, but that is beyond the scope of this answer and not particularly useful in this context, given the code snippets involved.