-3

I am trying to get the values I entered in the TextBox, but somehow my code only picks up on one of the two textboxes. When I removed the first textbox, the second one started failing. Still, adding another blank on BEFORE the first one did not fix it. Here's my code:

class WindowManager
{
    //Background
    private Panel BackDrop;

    //Main menu components
    private Label headerLabel, optionsLabel, aantalTeamsLabel, aantalBracketLabel;
    private Button startButton;
    private TextBox teamsTextBox, bracketTextBox;

    public WindowManager(Panel BackDrop)
    {
        this.BackDrop = BackDrop;
    }

    public void startWindow()
    {
        //Adds the components required for the main menu
        SetMain();
    }

    private void SetMain()
    {
        headerLabel = new Label();
        headerLabel.Text = "Welkom bij de GameBasics poule simulator.";
        headerLabel.Size = new Size(250, headerLabel.Size.Height);
        headerLabel.Location = new Point(10, 10);
        BackDrop.Controls.Add(headerLabel);

        optionsLabel = new Label();
        optionsLabel.Text = "Maak een keuze.";
        optionsLabel.Size = new Size(250, optionsLabel.Size.Height);
        optionsLabel.Location = new Point(10, 35);
        BackDrop.Controls.Add(optionsLabel);

        aantalTeamsLabel = new Label();
        aantalTeamsLabel.Text = "Aantal teams:";
        aantalTeamsLabel.Size = new Size(85, aantalTeamsLabel.Size.Height);
        aantalTeamsLabel.Location = new Point(10, 60);
        BackDrop.Controls.Add(aantalTeamsLabel);

        teamsTextBox = new TextBox();
        teamsTextBox.Text = "0";
        teamsTextBox.Size = new Size(40, teamsTextBox.Size.Height);
        teamsTextBox.Location = new Point(95, 60);
        BackDrop.Controls.Add(teamsTextBox);

        aantalBracketLabel = new Label();
        aantalBracketLabel.Text = "Aantal bracket \ndeelnemers:";
        aantalBracketLabel.Size = new Size(85, aantalBracketLabel.Size.Height + 5);
        aantalBracketLabel.Location = new Point(10, 90);
        BackDrop.Controls.Add(aantalBracketLabel);

        bracketTextBox = new TextBox();
        bracketTextBox.Text = "0";
        bracketTextBox.Size = new Size(40, bracketTextBox.Size.Height);
        bracketTextBox.Location = new Point(95, 95);
        BackDrop.Controls.Add(bracketTextBox);

        //Adds a start button which executes method Start_Click when clicked
        startButton = new Button();
        startButton.Text = "Start";
        startButton.Size = new Size(100, startButton.Size.Height);
        startButton.Click += new EventHandler(Start_Click);
        startButton.Location = new Point(10, 350 - startButton.Size.Height);
        BackDrop.Controls.Add(startButton);
    }

    private void Start_Click(object sender, EventArgs e)
    {
        int team, bracket;

        if (ConvertToInt(teamsTextBox.Text, out team))
        {
            if (ConvertToInt(bracketTextBox.Text, out bracket))
            {
                PouleManager pM = new PouleManager(team, bracket);
            }
        }
    }

    private bool ConvertToInt(string text, out int result)
    {
        int number;
        bool succes = int.TryParse(text, out number);
        if (succes)
        {
            MessageBox.Show(number.ToString());
            result = number;
            return true;
        }
        else
        {
            MessageBox.Show(text + " is geen getal.");
            result = 0;
            return false;
        }
    }
}

I want my Start_Click to pick up both field and verify they're numbers before passing it on, but somehow on the first ConvertToInt called, doesn't seem to register any changes to the TextBox. It will always pass 0, regardless of my actual input.

Any suggestions?

FvB
  • 713
  • 6
  • 15
  • 2
    It is a simple error. IN your first call to ConvertToInt you pass bracket instead of team – Steve Dec 16 '14 at 21:29
  • 2
    am I the only one not understanding the question? – Steve Dec 16 '14 at 21:29
  • Thanks for responding! What you spotted was an old fixing attempt, but chaning that doesn't make it work. Essentially, I call ConvertToInt twice in Start_Click. When in the first call I use teamsTextBox.Text, this doesn't actually bring back the text I typed in, instead it just gives me a 0 every time. Even so, the second ConvertToInt DOES give me back the text I typed in the box, so I'm not sure how to fix it. – FvB Dec 16 '14 at 21:31
  • in your Start_Click Event your ConvertToInt both use Bracket so it will always have the value of the second textbox, FYI – Sorceri Dec 16 '14 at 21:31
  • I tried editing it, but it still doesn't work. – FvB Dec 16 '14 at 21:34
  • I don't see a single line of code after initialization where you actually set the textbox's text. Am I missing something or is that your actual issue? – TyCobb Dec 16 '14 at 21:34
  • It's a GUI element, so naturally I just select it and type in it while the program is running, then press the button. No matter what I type into teamsTextBox, calling teamsTextBox.Text always gives back "0". – FvB Dec 16 '14 at 21:36
  • @FvB updating the text value isn't sufficient when the window has already been loaded. You need to bind the value of the text property and set up a INotifyPropertyChanged event in your .cs file to notify the window a value has changed. – CalebB Dec 16 '14 at 21:37
  • Know any good examples? Also: that is weird, because the other TextBox's text value does "bind" on it's own – FvB Dec 16 '14 at 21:39
  • A real dirty trick would be to add a textchanged event handler and ensure the textchanged event is fired when you type in the box. If it isnt, then is that the only code you place controls with in your panel? – Sorceri Dec 16 '14 at 21:39
  • @FvB Here is a [Simple WPF Binding Tutorial.](http://www.codeproject.com/Articles/26031/Simple-WPF-databinding-with-some-additional-WPF-go) It's how I figured out data binding for my first WPF Apps. – CalebB Dec 16 '14 at 21:43
  • @FvB You can also find more info and useful resources in [WPF Databinding.](http://stackoverflow.com/questions/7472/wpf-databinding) – CalebB Dec 16 '14 at 21:48

1 Answers1

1

I created a WinForms app to replicate your scenario and tested your amended code. Works fine on my machine: enter image description here

Here's the 'bracket' field being evaluated correctly:

enter image description here

In case you want to compare with your own implementation, I've zipped the code for download here

Rich Turner
  • 10,800
  • 1
  • 51
  • 68