Windows form receive text input

Hey guys/girls I got myself stuck and I was hoping I could get some help with it. simply said I'm trying to make a soccerpool on windows form. Because the player can put in as many team's as he/she wants I put the code that makes the betting panels in a (for)loop with the text as 0. very handy if I say so myself but now I can't retrieve the correct input from the user or without breaking the loop. any idea's?

        for (int i = 0; i < hometable.Rows.Count; i++)
        {
            DataRow dataRowHome = hometable.Rows[i];
            DataRow dataRowAway = awayTable.Rows[i];
            
            Label lblHomeTeam = new Label();
            Label lblAwayTeam = new Label();
            
            TextBox txtHomePred = new TextBox();
            TextBox txtAwayPred = new TextBox();

            lblHomeTeam.TextAlign = ContentAlignment.BottomRight;
            lblHomeTeam.Text = dataRowHome["TeamName"].ToString();
            lblHomeTeam.Location = new Point(15, txtHomePred.Bottom + (i * 30));
            lblHomeTeam.AutoSize = true;

            txtHomePred.Text = "0";
            txtHomePred.Location = new Point(lblHomeTeam.Width, lblHomeTeam.Top - 3);
            txtHomePred.Width = 40;

            txtAwayPred.Text = "0";
            txtAwayPred.Location = new Point(txtHomePred.Width + lblHomeTeam.Width, txtHomePred.Top);
            txtAwayPred.Width = 40;

            lblAwayTeam.Text = dataRowAway["TeamName"].ToString();
            lblAwayTeam.Location = new Point(txtHomePred.Width + lblHomeTeam.Width + txtAwayPred.Width, txtHomePred.Top + 3);
            lblAwayTeam.AutoSize = true;

            pnlPredCard.Controls.Add(lblHomeTeam);
            pnlPredCard.Controls.Add(txtHomePred);
            pnlPredCard.Controls.Add(txtAwayPred);
            pnlPredCard.Controls.Add(lblAwayTeam);

So what my end goal is, is recieving the input from the user validating them and then storing them in a database.

Upvotes: 0

Views: 142

Answers (1)

EpicKip
EpicKip

Reputation: 4043

Well, depending on how the user activates an event that requires the reading of the TextBox you have a few possible solutions.
Here is one where the TextBox (read all TextBox's) waits for enter:

private void Form_Load(object sender, EventArgs e)
{
    while(someLoop)
    {
        TextBox theTextBox = new TextBox();
        theTextBox.Name = "SomeUniqeName";//Maybe team name?
        theTextBox.KeyUp += TheTextBox_KeyUp;
    }
}

private void TheTextBox_KeyUp(object sender, KeyEventArgs e)
{
    if ( e.KeyCode == Keys.Enter )
    {
        TextBox textbox = (TextBox) sender;//Get the textbox

        //Just an example
        listOfTeams.First( r => r.TeamName == textbox.Name )
                   .SomeOtherProperty = textbox.Text;
    }
}

The textbox's are now identifiable by their name and all have an event. No matter how many you make.

If you will store the data later with 1 click of a button (and another loop) this solution might be better:

string[] Teams = { "teamA", "teamB", "teamC" };
private void Form1_Load(object sender, EventArgs e)
{
    for ( int i = 0; i < Teams.Length; i++ )
    {
        TextBox theTextBox = new TextBox();

        //Prefix the name so we know this is a betting textbox
        //Add the 'name' (teams[i] in this case) to find it
        theTextBox.Name = "ThePrefix" + Teams[i];
    }

}

private void someButton_Click(object sender, EventArgs e)
{
    //We want all betting textbox's here but also by team name
    for ( int i = 0; i < Teams.Length; i++ )
    {
        //Because we set the name, we can now find it with linq
        TextBox textBox = (TextBox) this.Controls.Cast<Control>()
            .FirstOrDefault( row => row.Name == "ThePrefix" + Teams[i] );
    }
}

This way each textbox is identifiable and won't conflict with other textbox's (because of 'ThePrefix'). This is essentially the other way around from the first method as it looks for the textbox based on data rather than data based on textbox name.

Upvotes: 1

Related Questions