C# winform iterate textbox property [duplicate] - c#

This question already has answers here:
Making an indexed control array?
(7 answers)
Closed 3 years ago.
I am new with C# and trying to iterate values of 4 textboxes on my winform application, before i look into list arrays, is it possible to loop all 4 of the textboxes appending the index number?
Example
for(i =0; i == 4; i++){
textBox_+ i +.Text
}

Certainly, though you do it slightly differently. You need to iterate over the Control collection and search for your textboxes.
E.g.
foreach (var textBox in this.Controls.OfType<TextBox>())
{
textBox.Text = "bla";
}
If you want to access only certain textboxes - you could tag them via Tag property and search only for those. E.g.
foreach (var textBox in this.Controls.OfType<TextBox>())
{
if (textBox.Tag == "sometag")
textBox.Text = "bla";
}

Related

Iterating through variable names with for loop [duplicate]

This question already has answers here:
Variables in a loop
(9 answers)
Loop through object variables with different number on the name [duplicate]
(5 answers)
Iteration with variable name [duplicate]
(1 answer)
Closed 2 years ago.
I am trying to use a for loop to iterate through a series of variable names each ending in a number from 1 to 10. I have seen a few other answers to this question but have been unable to make any work for my specific situation. My code is as follows:
string cat2Pos0 = cat2[0];
int numOfPos0 = cat2.Where(x => x.Equals(cat2Pos0)).Count();
List<int> indexOfPos0 = new List<int>();
bool check = cat2.Contains(cat2Pos0);
int index = 0;
if (check == true)
{
for (int i = 0; i < numOfPos0; i++)
{
index = cat2.FindIndex(x => x == cat2Pos0);
indexOfPos0.Add(cat2.IndexOf(cat2Pos0));
}
}
else if (cat2Pos0 == "-")
{
numOfPos0 = 17;
}
I need to loop through 10 variables names cat1 - cat10. In the code: whenever there is the phrase "cat" I need to be able to adjust it depending on a for loop e.g. cat1 or cat5:
string cat3pos0 = cat3[0];
or:
index = cat3.FindIndex(x => x == cat3Pos0);
Unfortuantely, I am unable to simply write out each variation individually as that would use up almost 3700 lines of code and I was hoping that there would be a better way of achieveing this.
Many thanks, all help is greatly appreciated,
Josh
See here how to use reflection for this. (something like this.GetType().GetField("cat" + i.ToString());.)
But I would really suggest changing your variables to one array of 10 variables. So cat will be an array of arrays (since your cat's seem to be arrays).

Is there an equivalent of the HTML function getElementById() for in C# for Form Controls? [duplicate]

This question already has answers here:
Get a Windows Forms control by name in C#
(14 answers)
Closed 3 years ago.
We are supposed to code Conway's Game Of Life in a C# Winform application. So, I created a 10 by 10 "game board" out of buttons. Pink means dead, blue means alive. And for ease of access, each button is named after its coordinates on the game board (e.g. x1y1, x10y9, x5y5). But then I realized I have no idea how to loop through 100 form controls and perform do stuff to each individual according to their color/value. In HTML/Javascript, I used the following loop:
for(row = 1; row <= 10; row++){
board[row] = new Array(10); //creates a 10 by 10 matrix to store the board's values in it.
for(col = 1; col <= 10; col++){
var position = "x" + row + "y" + col;
var val = parseInt(document.board.elements[position].value);
board[row][col] = val;
}
}
So the question is this: Is there a way to call form controls through strings not variable names? Something like Form1.getControlByName(string) or something?
You could set the Name property of each Button control you create. E.g. let that button is a reference to a Button control:
button.Name = "11";
Then you could use Control.ControlCollection.Find method to find the button control you are looking for as below:
Button button = this.Controls.Find("11", true).FirstOrDefault() as Button;
The this is a reference to the Form instance. You need to call FirstOrDefault, since Find returns an array of the Controls, whose name is "11". Last you have to use the as operator to convert the Control object to a Button. If conversion fails the value of button is null. So after this conversion you have to check if it is not null:
if(button != null)
{
// place here your code
}

for-loop to foreach with negative(i--) statement [duplicate]

This question already has answers here:
Possible to iterate backwards through a foreach?
(13 answers)
Closed 7 years ago.
If I have list of data, I want delete separate rows (not bulk delete) with a loop.
Here my Code
var objecctCount = listItemsDelete.Count;
for (int i = objecctCount; i > 0; i--)
{
listItemsDelete[i].DeleteObject();
spDataContext.ExecuteQuery();
}
where,
listItemsDelete is list of data,
listItemsDelete[i].DeleteObject(); delete separate row from listItemsDelete,
spDataContext.ExecuteQuery(); is helps update values to the listItemsDelete
My logic is working good.
my question is, Is it possible to change the for-loop to foreach with negative statement? Because my knowledge about foreach is helps to work with positive statement(I++).
So you want to reverse the loop?
foreach(var obj in listItemsDelete.Reverse())
{
obj.DeleteObject();
spDataContext.ExecuteQuery();
}
Since you are using .NET 2 you can't use LINQ. Stay with your for-loop but fix following error.
Instead of
for (int i = objecctCount; i > 0; i--)
// ...
use this:
for (int i = objecctCount - 1; i >= 0; i--)
// ...

How to Create a Dynamic Array? [duplicate]

This question already has answers here:
Dynamic array in C#
(9 answers)
Closed 7 years ago.
static void Main(string[] args)
{
int numberOfTheWords = 1;
string[] words = new string[numberOfTheWords];
Console.WriteLine("You can exit from program by writing EXIT ");
Console.WriteLine("Enter the word: ");
for(int i = 0; i < numberOfTheWords; i++)
{
words[i] = Console.ReadLine();
if (words[i] == "EXIT")
break;
else
numberOfTheWords++;
}
}
Guys I am trying to expand the lengt of the array but "numberOfTheWords" variable is in the "for loop' scope" so it does not effect the global "numberOfTheWord" variable and i cannot expand the array length. What I am trying to achieve is to make dynamic array. I do not want to declare the length of the array. When the user input a word, the length of the array will be increased automatically. Can you help me about how to do this?
This can be easily done with a List.
Example:
List<string> words = new List<string>();
...
words.Add(Console.ReadLine());
Lists are dynamically expanding and you don't have to manage the size of the list on your own. The .NET Framework does that for you. You can also insert an item anywhere in the middle or delete one from any index.
You just need to use List:
var words = new List<string>();
An array does not dynamically resize. A List does. With it, we do not need to manage the size on our own. This type is ideal for linear collections not accessed by keys.

C# - Randomizing lines of a text box [duplicate]

This question already has answers here:
Randomize a List<T>
(28 answers)
Closed 8 years ago.
Say i have a text box with the following content:
Word
Entry
List
Sentry
Each on their own line. How can i randomize them, to appear something like this(on button click):
Entry
List
Sentry
Word
Or any random combination. Now note, i have something like 100,000 separate lines for some of the files i import. I need the to be randomized on button click. Thanks!
What im going to do is have 2 multi-line text boxes next to each other, the user can randomize each list, then a separate button will combine both lists into one file, delimited by a colon(:). Thanks a ton!
Instead of "Randomize", think "Shuffle":
void Shuffle<T>(IList<T> items)
{
// creating a new object here for demo purposes
// Really, the same object should be re-used across method calls
var random = new Random();
for (int i = items.Count; i > 1; i--)
{
// Pick random element to swap.
int j = random.Next(i); // 0 <= j <= i-1
// Swap.
T tmp = items[j];
items[j] = items[i - 1];
items[i - 1] = tmp;
}
}
The Windows Forms textbox has a helpful Lines property to make this easy to use:
string[] lines = MyTextBox.Lines;
Shuffle(lines);

Categories

Resources