C
C#15mo 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
JakenVeina15mo 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
NikolaaaOP15mo ago
yes but i did it but now idk how to put my color back if the texbox no longer needs to be red
Omnissiah
Omnissiah15mo ago
you can use the default/system colors
Nikolaaa
NikolaaaOP15mo 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
JakenVeina15mo 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
NikolaaaOP15mo 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
JakenVeina15mo ago
then why are you running it before you have checked all of the other textboxes?
Nikolaaa
NikolaaaOP15mo ago
wdym?
JakenVeina
JakenVeina15mo ago
I mean what I said
Nikolaaa
NikolaaaOP15mo ago
how do i not run it before it has checked all the textboxes
JakenVeina
JakenVeina15mo 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
NikolaaaOP15mo ago
but it is checking all of them in foreach
JakenVeina
JakenVeina15mo ago
yes and it is also setting the color in the foreach for each other textbox it checks
Nikolaaa
NikolaaaOP15mo ago
yea idk where to set the color
JakenVeina
JakenVeina15mo ago
instead of checking them all, and THEN setting the color I've just told you where to set it
Nikolaaa
NikolaaaOP15mo ago
and how do i collect the info about all textboxes and then set the color
JakenVeina
JakenVeina15mo ago
by doing that how do you collect info in C#?
Nikolaaa
NikolaaaOP15mo ago
in an array?
JakenVeina
JakenVeina15mo ago
if you want
Nikolaaa
NikolaaaOP15mo ago
ok i will try that
JakenVeina
JakenVeina15mo ago
I don't see how that makes sense but go for it
Nikolaaa
NikolaaaOP15mo ago
wdym what other options i have
JakenVeina
JakenVeina15mo ago
what is the info that you want to collect?
Nikolaaa
NikolaaaOP15mo ago
the characters of the textboxes
JakenVeina
JakenVeina15mo ago
no what info do you need to make your decision about the color?
Nikolaaa
NikolaaaOP15mo ago
the characters of the textboxes because A+B+C is the same as B+C+A
JakenVeina
JakenVeina15mo ago
right
Nikolaaa
NikolaaaOP15mo ago
i mean the characters in the text
JakenVeina
JakenVeina15mo ago
but what info do you need to make your decision?
Nikolaaa
NikolaaaOP15mo ago
idk how to explain it if the characters match the current textbox
JakenVeina
JakenVeina15mo ago
or not when you say "if X or not" what does that immediately tell you you want in code?
Nikolaaa
NikolaaaOP15mo ago
if statement ?
JakenVeina
JakenVeina15mo ago
which requires what?
Nikolaaa
NikolaaaOP15mo ago
uh bool?
JakenVeina
JakenVeina15mo ago
precisely
Nikolaaa
NikolaaaOP15mo ago
if the textboxes have matching characters
JakenVeina
JakenVeina15mo 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
NikolaaaOP15mo ago
yea but dont want to have nested if statements for all textboxes? do i need to use an array or what
JakenVeina
JakenVeina15mo ago
why don't you want to have nested if statements for all of the other textboxes?
Nikolaaa
NikolaaaOP15mo ago
because isnt there any other better way?
JakenVeina
JakenVeina15mo 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
NikolaaaOP15mo 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
JakenVeina15mo ago
I don't see why you need three layers of ifs
Nikolaaa
NikolaaaOP15mo ago
well no it would actually be if { } if { }and then ifs for every textbox i was thinking you want to do that
JakenVeina
JakenVeina15mo ago
nothing you have described to me looks like that
Nikolaaa
NikolaaaOP15mo ago
so how would i do it with foreach
JakenVeina
JakenVeina15mo 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
NikolaaaOP15mo ago
so allTextBoxes is what? an array?
JakenVeina
JakenVeina15mo 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
NikolaaaOP15mo ago
ugh list ?
JakenVeina
JakenVeina15mo ago
do you not already have code that foreaches over all of the textboxes?
Nikolaaa
NikolaaaOP15mo 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
JakenVeina15mo ago
that works just as well
Nikolaaa
NikolaaaOP15mo ago
wdym i need the characters tho cause i need to compare them
JakenVeina
JakenVeina15mo 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
NikolaaaOP15mo ago
ok let me try also if its a list doesnt it need to be otherTextBox in allTextBoces
JakenVeina
JakenVeina15mo ago
what?
Nikolaaa
NikolaaaOP15mo ago
look at your foreach()
JakenVeina
JakenVeina15mo ago
fixed
Nikolaaa
NikolaaaOP15mo ago
k thanks
Azrael
Azrael15mo ago
No braces?! Preposterous.
Omnissiah
Omnissiah15mo ago
brace for yourself
Accord
Accord15mo 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