PlusUltra
PlusUltra

Reputation: 31

Can somebody shorten this code?

I just finished creating a program (I am a beginner at this programming stuff) Now I might be doing this the total wrong way or my logic might not be the greatest at programming but any help would be amazing I will post my code so far below

This code is used when a button is clicked, the button will send a text then the textbox will get the text.

        if (txt1.Text == "")
        {
            txt1.Text = "J";
            btn1.Visible = false;
        }
        else if (txt1.Text != "")
        {
            if (txt2.Text == "")
            {
                txt2.Text = "J";
                btn1.Visible = false;
            }
            else if (txt2.Text != "")
            {
                if (txt3.Text == "")
                {
                    txt3.Text = "J";
                    btn1.Visible = false;
                }
                else if (txt3.Text != "")
                {
                    if (txt4.Text == "")
                    {
                        txt4.Text = "J";
                        btn1.Visible = false;
                    }
                    else if (txt4.Text != "")
                    {
                        if (txt5.Text == "")
                        {
                            txt5.Text = "J";
                            btn1.Visible = false;
                        }
                        else if (txt5.Text != "")
                        {
                            if (txt6.Text == "")
                            {
                                txt6.Text = "J";
                                btn1.Visible = false;
                            }
                            else if (txt6.Text != "")
                            {
                                if (txt7.Text == "")
                                {
                                    txt7.Text = "J";
                                    btn1.Visible = false;
                                }
                                else if (txt7.Text != "")
                                {
                                    if (txt8.Text == "")
                                    {
                                        txt8.Text = "J";
                                        btn1.Visible = false;
                                    }
                                    else if (txt8.Text != "")
                                    {

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

Upvotes: 0

Views: 45

Answers (2)

Sweeper
Sweeper

Reputation: 271175

Note: I don't know whether this works for C# 3 or not (it should). Try it.

First, you should put all of your text fields into an array:

TextField[] textFields = { txt1, txt2, txt3, txt4, txt5, txt6, txt7, txt8, };

Then, loop through the text fields to find a text field that has no text in it:

foreach (TextField tf in textFields) {
    if (tf.Text == "") {

    }
}

After we find it, we want to set its text to "J" and make btn1 invisible. Since we already found the text field, we don't need to continue the loop anymore, so we break:

tf.Text = "J";
btn1.Visible = false;
break;

If this doesn't work in C# 3, just update to C# 5 or 6 alright?

Upvotes: 0

Chris Sharp
Chris Sharp

Reputation: 1995

You need to get all of these text cases into an array for the following loop to work (I have called the array 'txt' here). Based on what you have written this loop should do the same thing as your code but I'm not sure if that's what you really want to do. Your code is setting a single text box to "J" and then hiding your button only if every preceding text field is not an empty string (This will include any of the fields set to null, for example). The conditional then exits.

       `for (int i = 0; i < txt.Length; i++) {
            if(txt[i] != "") {
                continue;
            }
            else if(txt[i] == "") {
                txt[i] = "J";
                btn1.Visible = false;
                break;
            }
        }

Upvotes: 1

Related Questions