❔ problems with foreach loop
hello guys, I have a problem with getting the color of my textbox back because if I put it in a foreach loop some textbox in textBoxes list is not gonna have the same name and it activates the else if. I want it to only activate if none of the textboxes in the list have the same name as currentTextbox. I comented the line where the problem is. It will be red and instantly turn back to black (you can not see it but I debugged it, it does turn to red) because some textboxes have the same name and some don't, how do I make it only if all textboxes do not have the same name as currentTextbox
63 Replies
uhhh
okay
so
you're trying to identify all the textboxes that have the same text as a particular one?
except not just the same text, but the same collection of characters?
yes
but i did it but now idk how to put my color back if the texbox no longer needs to be red
you can use the default/system colors
yea but the foreach loop is preventing it from being red then
it turns black instantly
idk where to put the red color change line thats the problem
black*
put it where it will run when you want it to run
and not run when you don't want it to run
if i knew how
i want it to run when none of the textboxes match the characters of current textbox
if it put it in elseif as I put it there then it will always be black even if it should be red
then why are you running it before you have checked all of the other textboxes?
wdym?
I mean what I said
how do i not run it before it has checked all the textboxes
you want to change the color of your textbox based on the state of all of the other text boxes
so, you need to check all of the other textboxes
but it is checking all of them in foreach
yes
and it is also setting the color in the foreach
for each other textbox it checks
yea
idk where to set the color
instead of checking them all, and THEN setting the color
I've just told you where to set it
and how do i collect the info about all textboxes and then set the color
by doing that
how do you collect info in C#?
in an array?
if you want
ok i will try that
I don't see how that makes sense
but go for it
wdym
what other options i have
what is the info that you want to collect?
the characters of the textboxes
no
what info do you need to make your decision about the color?
the characters of the textboxes because A+B+C is the same as B+C+A
right
i mean the characters in the text
but what info do you need to make your decision?
idk how to explain it
if the characters match the current textbox
or not
when you say "if X or not" what does that immediately tell you you want in code?
if statement
?
which requires what?
uh
bool?
precisely
if the textboxes have matching characters
yea but dont want to have nested if statements for all textboxes?
do i need to use an array or what
why don't you want to have nested if statements for all of the other textboxes?
because isnt there any other better way?
not if you want to perform some kind of check for each one
it's right there in the description
"check" = "if"
"for each one" = "foreach"
yea
i was thinking you want to do
``
if
{
if
{
if
{
}
}
}
if its with foreach its ok
but then again how do i do itI don't see why you need three layers of ifs
well no it would actually be
if
{
}
if
{
}and then ifs for every textbox
i was thinking you want to do that
nothing you have described to me looks like that
so how would i do it with foreach
so allTextBoxes is what? an array?
that probably doesn't actually compile, now that I think about it
so, yeah, I guess you would want two ifs
you tell me
whatever refers to all of the text boxes, and is foreach-able
ugh
list
?
do you not already have code that foreaches over all of the textboxes?
oh
yea
i do
a list
im dumb
but thats their names not characters
that list
that i already have
so i would have to get characters again in this code
that works just as well
wdym
i need the characters tho
cause i need to compare them
yes
so, if you have a list of the things you need to check.....
and you know the check you need to perform
and you know how to loop over each one of those things
what remains to be the problem?
ok let me try
also if its a list doesnt it need to be otherTextBox in allTextBoces
what?
look at your foreach()
fixed
k thanks
No braces?!
Preposterous.
brace for yourself
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.