Windows窗体接收文本输入

时间:2017-04-24 11:50:59

标签: c# winforms textbox

嘿伙计/女孩我陷入困境,我希望能得到一些帮助。简单地说我试图在窗户上打造一个足球场。因为玩家可以输入他/她想要的尽可能多的团队,所以我把代码放在一个(for)循环中,文本为0.非常方便如果我这样说,但现在我无法检索来自用户的正确输入或不破坏循环。任何想法?

        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);

所以我的最终目标是接收用户验证它们的输入,然后将它们存储在数据库中。

1 个答案:

答案 0 :(得分:1)

那么,根据用户如何激活需要阅读TextBox的事件,您可以获得一些可能的解决方案。
这是TextBox(读取所有TextBox&#39; s)等待输入的地方:

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;
    }
}

现在可以通过名称识别文本框,并且所有文本框都有一个事件。不管你做了多少。

如果稍后只需点击一个按钮(和另一个循环)就可以存储数据,这个解决方案可能更好:

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] );
    }
}

这样每个文本框都是可识别的,并且不会与其他文本框冲突(因为&#39; ThePrefix&#39;)。这基本上与第一种方法相反,因为它根据数据而不是基于文本框名称的数据查找文本框。