Click here to Skip to main content
15,894,175 members
Home / Discussions / C#
   

C#

 
GeneralRe: Getting TypeInitializationEcxeption for runing Code: Pin
Member 1144974014-Feb-15 8:26
Member 1144974014-Feb-15 8:26 
GeneralRe: Getting TypeInitializationEcxeption for runing Code: Pin
OriginalGriff14-Feb-15 9:32
mveOriginalGriff14-Feb-15 9:32 
GeneralRe: Getting TypeInitializationEcxeption for runing Code: Pin
Member 1144974014-Feb-15 8:39
Member 1144974014-Feb-15 8:39 
QuestionHow to install system input language programatically in c# Pin
habib_ashraf14-Feb-15 1:14
habib_ashraf14-Feb-15 1:14 
SuggestionRe: How to install system input language programatically in c# Pin
Richard MacCutchan14-Feb-15 1:51
mveRichard MacCutchan14-Feb-15 1:51 
Question[Solved] Array Issue Pin
Linus Agren14-Feb-15 0:01
Linus Agren14-Feb-15 0:01 
GeneralRe: Array Issue Pin
harold aptroot14-Feb-15 0:14
harold aptroot14-Feb-15 0:14 
AnswerRe: Array Issue Pin
OriginalGriff14-Feb-15 0:42
mveOriginalGriff14-Feb-15 0:42 
The first thing to note is that it's a loop - which means the code inside it is executed every time the value of "i" is incremented: so for your example, the code within the loop is executed 3 times: for i == 0, i == 1, and i == 2.
And because your two if conditions are opposite:
C#
if (textBox2.Text == kos[i])
...
if(textBox2.Text != kos[i])
It will always print one string or the other.
In fact, because they are opposite, you don't need to test again at all: the else code will be executed if the first if fails, so your code is the equivalent of
C#
for (int i = 0; i <= kos.Count() - 1; i++)
{
    if (textBox2.Text == kos[i])
    {
        kosCheckLabel.Text = "YES.";
        kosCheckLabel.ForeColor = System.Drawing.Color.Red;
    }
    else
    {
        kosCheckLabel.Text = "NO.";
        kosCheckLabel.ForeColor = System.Drawing.Color.Lime;
    }
}

If you want to just print once - "Yes" if it is there, or "No" if it isn't, the normal way is to use a bool value to "store" the current result:
C#
bool isThere = false;
for (int i = 0; i <= kos.Count() - 1; i++)
{
    if (textBox2.Text == kos[i])
    {
        isThere = true;
        break;   // No point in looking at more.
    }
}
if (isThere)
    {
    kosCheckLabel.Text = "YES.";
    kosCheckLabel.ForeColor = System.Drawing.Color.Red;
    }
else
    {
    kosCheckLabel.Text = "NO.";
    kosCheckLabel.ForeColor = System.Drawing.Color.Lime;
    }

But the simplest way to do it is to use a Linq method:
C#
string[] kos = new string[] { "test1", "test2", "test3" };

private void button2_Click(object sender, EventArgs e)
        {
        if (kos.Contains(textBox2.Text)
            {
            kosCheckLabel.Text = "YES.";
            kosCheckLabel.ForeColor = System.Drawing.Color.Red;
            }
        else 
            {
            kosCheckLabel.Text = "NO.";
            kosCheckLabel.ForeColor = System.Drawing.Color.Lime;
            }
        }

Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

GeneralRe: Array Issue Pin
Linus Agren14-Feb-15 1:52
Linus Agren14-Feb-15 1:52 
GeneralRe: Array Issue Pin
OriginalGriff14-Feb-15 2:06
mveOriginalGriff14-Feb-15 2:06 
GeneralRe: Array Issue Pin
Linus Agren14-Feb-15 2:13
Linus Agren14-Feb-15 2:13 
GeneralRe: Array Issue Pin
OriginalGriff14-Feb-15 2:19
mveOriginalGriff14-Feb-15 2:19 
GeneralRe: Array Issue Pin
Dave Kreskowiak14-Feb-15 2:44
mveDave Kreskowiak14-Feb-15 2:44 
QuestionCan we implement CSS on a Window Form Pin
Member 1145143613-Feb-15 19:29
Member 1145143613-Feb-15 19:29 
AnswerRe: Can we implement CSS on a Window Form Pin
Richard MacCutchan13-Feb-15 21:35
mveRichard MacCutchan13-Feb-15 21:35 
SuggestionC# WinForms RichTextBox Block-Style Selection Pin
Mr Cirwos13-Feb-15 6:56
Mr Cirwos13-Feb-15 6:56 
GeneralRe: C# WinForms RichTextBox Block-Style Selection Pin
Eddy Vluggen13-Feb-15 7:54
professionalEddy Vluggen13-Feb-15 7:54 
GeneralRe: C# WinForms RichTextBox Block-Style Selection Pin
Mr Cirwos13-Feb-15 22:42
Mr Cirwos13-Feb-15 22:42 
GeneralRe: C# WinForms RichTextBox Block-Style Selection Pin
Eddy Vluggen14-Feb-15 4:16
professionalEddy Vluggen14-Feb-15 4:16 
GeneralRe: C# WinForms RichTextBox Block-Style Selection Pin
Richard MacCutchan13-Feb-15 23:22
mveRichard MacCutchan13-Feb-15 23:22 
GeneralRe: C# WinForms RichTextBox Block-Style Selection Pin
Mr Cirwos13-Feb-15 23:34
Mr Cirwos13-Feb-15 23:34 
GeneralRe: C# WinForms RichTextBox Block-Style Selection Pin
Richard MacCutchan13-Feb-15 23:41
mveRichard MacCutchan13-Feb-15 23:41 
QuestionHow to add website into compatibility view ? Pin
namerg13-Feb-15 6:30
namerg13-Feb-15 6:30 
AnswerRe: Thoughts why Mini Application Stops Working once executed ? Pin
phil.o13-Feb-15 7:31
professionalphil.o13-Feb-15 7:31 
GeneralRe: Thoughts why Mini Application Stops Working once executed ? Pin
namerg13-Feb-15 7:40
namerg13-Feb-15 7:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.