C
C#11mo ago
Nikolaaa

❔ problems with foreach loop

if (buttonTextBoxMap.TryGetValue(currentButton, out TextBox currentTextbox))
{


string currentText = new string(currentTextbox.Text.OrderBy(c => c).ToArray());

foreach (TextBox textBox in textBoxes)
{
if (textBox != currentTextbox) // Provjerite da li je trenutni TextBox isti kao currentTextbox
{
string textBoxText = new string(textBox.Text.OrderBy(c => c).ToArray());

if (textBoxText == currentText)
{
currentTextbox.Background = new SolidColorBrush(Color.FromArgb(76, 255, 0, 0));
}/*
else if (textBoxText != currentText)
{
currentTextbox.Background = new SolidColorBrush(Color.FromArgb(76, 0, 0, 0));
}*/ //<----------- THE PROBLEM IS THIS ELSE IF
}
}
}
if (buttonTextBoxMap.TryGetValue(currentButton, out TextBox currentTextbox))
{


string currentText = new string(currentTextbox.Text.OrderBy(c => c).ToArray());

foreach (TextBox textBox in textBoxes)
{
if (textBox != currentTextbox) // Provjerite da li je trenutni TextBox isti kao currentTextbox
{
string textBoxText = new string(textBox.Text.OrderBy(c => c).ToArray());

if (textBoxText == currentText)
{
currentTextbox.Background = new SolidColorBrush(Color.FromArgb(76, 255, 0, 0));
}/*
else if (textBoxText != currentText)
{
currentTextbox.Background = new SolidColorBrush(Color.FromArgb(76, 0, 0, 0));
}*/ //<----------- THE PROBLEM IS THIS ELSE IF
}
}
}
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
JakenVeina
JakenVeina11mo ago
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?
Nikolaaa
Nikolaaa11mo ago
yes but i did it but now idk how to put my color back if the texbox no longer needs to be red
boiled goose
boiled goose11mo ago
you can use the default/system colors
Nikolaaa
Nikolaaa11mo ago
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*
JakenVeina
JakenVeina11mo ago
put it where it will run when you want it to run and not run when you don't want it to run
Nikolaaa
Nikolaaa11mo ago
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
JakenVeina
JakenVeina11mo ago
then why are you running it before you have checked all of the other textboxes?
Nikolaaa
Nikolaaa11mo ago
wdym?
JakenVeina
JakenVeina11mo ago
I mean what I said
Nikolaaa
Nikolaaa11mo ago
how do i not run it before it has checked all the textboxes
JakenVeina
JakenVeina11mo ago
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
Nikolaaa
Nikolaaa11mo ago
but it is checking all of them in foreach
JakenVeina
JakenVeina11mo ago
yes and it is also setting the color in the foreach for each other textbox it checks
Nikolaaa
Nikolaaa11mo ago
yea idk where to set the color
JakenVeina
JakenVeina11mo ago
instead of checking them all, and THEN setting the color I've just told you where to set it
Nikolaaa
Nikolaaa11mo ago
and how do i collect the info about all textboxes and then set the color
JakenVeina
JakenVeina11mo ago
by doing that how do you collect info in C#?
Nikolaaa
Nikolaaa11mo ago
in an array?
JakenVeina
JakenVeina11mo ago
if you want
Nikolaaa
Nikolaaa11mo ago
ok i will try that
JakenVeina
JakenVeina11mo ago
I don't see how that makes sense but go for it
Nikolaaa
Nikolaaa11mo ago
wdym what other options i have
JakenVeina
JakenVeina11mo ago
what is the info that you want to collect?
Nikolaaa
Nikolaaa11mo ago
the characters of the textboxes
JakenVeina
JakenVeina11mo ago
no what info do you need to make your decision about the color?
Nikolaaa
Nikolaaa11mo ago
the characters of the textboxes because A+B+C is the same as B+C+A
JakenVeina
JakenVeina11mo ago
right
Nikolaaa
Nikolaaa11mo ago
i mean the characters in the text
JakenVeina
JakenVeina11mo ago
but what info do you need to make your decision?
Nikolaaa
Nikolaaa11mo ago
idk how to explain it if the characters match the current textbox
JakenVeina
JakenVeina11mo ago
or not when you say "if X or not" what does that immediately tell you you want in code?
Nikolaaa
Nikolaaa11mo ago
if statement ?
JakenVeina
JakenVeina11mo ago
which requires what?
Nikolaaa
Nikolaaa11mo ago
uh bool?
JakenVeina
JakenVeina11mo ago
precisely
Nikolaaa
Nikolaaa11mo ago
if the textboxes have matching characters
JakenVeina
JakenVeina11mo ago
bool anyOtherTextBoxesMatchCurrentTextBox;

// Check all other textboxes

if (anyOtherTextBoxesMatchCurrentTextBox)
currentTextBox.Background = new SolidColorBrush(Color.FromArgb(76, 255, 0, 0));
else
currentTextBox.Background = new SolidColorBrush(Color.FromArgb(76, 0, 0, 0));
bool anyOtherTextBoxesMatchCurrentTextBox;

// Check all other textboxes

if (anyOtherTextBoxesMatchCurrentTextBox)
currentTextBox.Background = new SolidColorBrush(Color.FromArgb(76, 255, 0, 0));
else
currentTextBox.Background = new SolidColorBrush(Color.FromArgb(76, 0, 0, 0));
Nikolaaa
Nikolaaa11mo ago
yea but dont want to have nested if statements for all textboxes? do i need to use an array or what
JakenVeina
JakenVeina11mo ago
why don't you want to have nested if statements for all of the other textboxes?
Nikolaaa
Nikolaaa11mo ago
because isnt there any other better way?
JakenVeina
JakenVeina11mo ago
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"
Nikolaaa
Nikolaaa11mo ago
yea i was thinking you want to do `` if { if { if { } } } if its with foreach its ok but then again how do i do it
JakenVeina
JakenVeina11mo ago
I don't see why you need three layers of ifs
Nikolaaa
Nikolaaa11mo ago
well no it would actually be if { } if { }and then ifs for every textbox i was thinking you want to do that
JakenVeina
JakenVeina11mo ago
nothing you have described to me looks like that
Nikolaaa
Nikolaaa11mo ago
so how would i do it with foreach
JakenVeina
JakenVeina11mo ago
bool anyOtherTextBoxesMatchCurrentTextBox;

foreach(var otherTextBox = allTextBoxes.Except(currentTextBox))
{
if (/* text matches current */)
{

}
}

if (anyOtherTextBoxesMatchCurrentTextBox)
currentTextBox.Background = new SolidColorBrush(Color.FromArgb(76, 255, 0, 0));
else
currentTextBox.Background = new SolidColorBrush(Color.FromArgb(76, 0,
bool anyOtherTextBoxesMatchCurrentTextBox;

foreach(var otherTextBox = allTextBoxes.Except(currentTextBox))
{
if (/* text matches current */)
{

}
}

if (anyOtherTextBoxesMatchCurrentTextBox)
currentTextBox.Background = new SolidColorBrush(Color.FromArgb(76, 255, 0, 0));
else
currentTextBox.Background = new SolidColorBrush(Color.FromArgb(76, 0,
Nikolaaa
Nikolaaa11mo ago
so allTextBoxes is what? an array?
JakenVeina
JakenVeina11mo ago
that probably doesn't actually compile, now that I think about it so, yeah, I guess you would want two ifs
bool anyOtherTextBoxesMatchCurrentTextBox;

foreach(var otherTextBox in allTextBoxes)
{
if (otherTextBox == currentTextBox)
continue;

if (/* text matches current */)
{

}
}

if (anyOtherTextBoxesMatchCurrentTextBox)
currentTextBox.Background = new SolidColorBrush(Color.FromArgb(76, 255, 0, 0));
else
currentTextBox.Background = new SolidColorBrush(Color.FromArgb(76, 0, 0, 0));
bool anyOtherTextBoxesMatchCurrentTextBox;

foreach(var otherTextBox in allTextBoxes)
{
if (otherTextBox == currentTextBox)
continue;

if (/* text matches current */)
{

}
}

if (anyOtherTextBoxesMatchCurrentTextBox)
currentTextBox.Background = new SolidColorBrush(Color.FromArgb(76, 255, 0, 0));
else
currentTextBox.Background = new SolidColorBrush(Color.FromArgb(76, 0, 0, 0));
you tell me whatever refers to all of the text boxes, and is foreach-able
Nikolaaa
Nikolaaa11mo ago
ugh list ?
JakenVeina
JakenVeina11mo ago
do you not already have code that foreaches over all of the textboxes?
Nikolaaa
Nikolaaa11mo ago
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
JakenVeina
JakenVeina11mo ago
that works just as well
Nikolaaa
Nikolaaa11mo ago
wdym i need the characters tho cause i need to compare them
JakenVeina
JakenVeina11mo ago
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?
Nikolaaa
Nikolaaa11mo ago
ok let me try also if its a list doesnt it need to be otherTextBox in allTextBoces
JakenVeina
JakenVeina11mo ago
what?
Nikolaaa
Nikolaaa11mo ago
look at your foreach()
JakenVeina
JakenVeina11mo ago
fixed
Nikolaaa
Nikolaaa11mo ago
k thanks
Azrael
Azrael11mo ago
No braces?! Preposterous.
boiled goose
boiled goose11mo ago
brace for yourself
Accord
Accord11mo ago
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.
Want results from more Discord servers?
Add your server
More Posts