如何一次从多个文本框中获取值?

时间:2014-10-03 14:28:10

标签: c# winforms textbox

我有一个表单,其中有许多文本框供用户输入数据。问题是我无法弄清楚如何从框中获取值!

用户按“保存”以保存所有值,因此我有一个“保存”此信息的过程,但正如我所说,我无法弄清楚如何提取值。我有一个搜索,发现什么都没有。继承了c#中的一些代码:

// this is the procedure i want to use the value in
public static save()
{
    MessageBox.Show(trackBoxString);
}

// the event handler. trackBoxString is a public static string so that I could access it from                       different procedures
private void trackBox_TextChanged(object sender, EventArgs e)
{
    trackBoxString = trackBox.Text;
}

这只会打开一个空白消息框

根据要求提供更多代码

public static raceBoxString =“”; public static trackBoxString =“”;

    static void saveSetup()
    {
        try
        {
            string server = "localhost";
            string database = "*****";
            string uid = """""""";
            string password = """"""";
            MySqlConnection connection;
            string connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password;
            connection = new MySqlConnection(connectionString);
            connection.Open();
            connection.Close();

            MessageBox.Show(trackBoxString);

        }
        catch (MySqlException ex)
        {
            MessageBox.Show(ex.ToString());
        }

    }

private void raceBox_TextChanged(object sender,EventArgs e)         {             raceBoxString = raceBox.Text;         }

    private void trackBox_TextChanged(object sender, EventArgs e)
    {
        trackBoxString = trackBox.Text;
    }

1 个答案:

答案 0 :(得分:-1)

好吧,我将trackBox.Text移动到了保存按钮事件处理程序中,出现了错误。但是当我保存并重新打开程序时它很好,所以我认为Windows有一点时刻!谢谢你的帮助

错误是;

An object reference is required for the non-static field, method, or property