ApachePilotMPE
ApachePilotMPE

Reputation: 193

Windows Forms Textbox is uneditable by accident

I have an application which asks the user a few simple questions. The user is supposed to input the answer by typing it into a TextBox. When I render the Windows Form though, the TextBox is greyed out, blends in with the background, and is uneditable.

Here's my code:

public string waitForText(Point Locution)
    {
        TextBox WriteAnswerHere = new TextBox();
        WriteAnswerHere.Location = Locution;
        WriteAnswerHere.ReadOnly = false;
        WriteAnswerHere.Focus();
        this.Controls.Add(WriteAnswerHere);

        int waiting = 1;
        while (waiting == 1)
        {
            if (Control.ModifierKeys == Keys.Enter)
            {
                waiting = 0;
            }

        }

        string HowYouAre = WriteAnswerHere.Text;
        this.Controls.Remove(WriteAnswerHere);
        return HowYouAre;
    }

The input is supposed to be given to the application when the Enter key is pressed, hence the (Control.ModifierKeys == Keys.Enter); Any suggestions on what I am doing wrong?

Upvotes: 0

Views: 143

Answers (1)

Reza Shirazian
Reza Shirazian

Reputation: 2353

You shouldn't use a while loop to detect specific key events. Your while loop is holding up the form. I suggest you check out these articles on Events and Event handlers for Windows forms.

http://msdn.microsoft.com/en-us/library/dacysss4.aspx

Upvotes: 1

Related Questions