简体   繁体   English

单击按钮时更改按钮的文本c#

[英]Change text of button when button is clicked c#

First time messing around with c# and I am trying to change the value of a button when the user clicks it. 第一次弄乱C#,我试图在用户单击按钮时更改按钮的值。

The database has four names in it, when the code executes it only shows the name that is in the last row. 数据库中有四个名称,执行代码时,它仅显示最后一行中的名称。 I would like it to start with the first name, and then each time the button is pressed it should show another name until it has gone through all the names in the DB. 我希望它以名字开头,然后每次按下按钮时,它应该显示另一个名字,直到它遍历数据库中的所有名字为止。

dataReader = command.ExecuteReader();

while (dataReader.Read())
{
    bText = dataReader.GetValue(1).ToString();
    button1.Text = bText;
}

The while loop does not work the way you think it does. while循环无法按您认为的方式工作。 What it does is looping over every result and set the button text. 它的作用是遍历每个结果并设置按钮文本。 It does not pause anywhere (because there is nothing that tells the loop it should). 它不会在任何地方暂停(因为没有任何内容告诉循环应该这样做)。 So it simply iterates over every element as fast as it can. 因此,它只是尽可能快地遍历每个元素。 This explains why you only see the last result. 这解释了为什么您只看到最后的结果。

What you can do is to store a counter somewhere that you increment every time the button is pressed and only use the n-th element. 您可以做的是将计数器存储在某个位置,该位置每次按下按钮都会增加,并且仅使用第n个元素。 Either by skipping the first few entries or depending on the database you use, you could only fetch a specific result entry (in MySQL this would be LIMIT and OFFSET in the select query) 通过跳过前几个条目或根据您使用的数据库,您只能获取特定的结果条目(在MySQL中,这在select查询中为LIMIT和OFFSET)

For your specific problem (only 4 entries) I would fetch all entries at the start of the application and store them in an array. 对于您的特定问题(仅4个条目),我将在应用程序的开头获取所有条目并将它们存储在数组中。 Then use the entry at the current index (=your counter) to set the text of the button. 然后使用当前索引(=您的计数器)上的条目来设置按钮的文本。

You can try this. 你可以试试看 I've table named Colors having two columns: ID and Name. 我有一个名为Colors的表,该表有两列:ID和Name。

public partial class FrmColor : Form
{
    SqlConnection con = new SqlConnection(@"<YOUR CONNECTION STRING HERE>");
    int pointer = 1;
    int rows = 0;

    public FrmColor()
    {
        InitializeComponent();
    }

    private void btnColor_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("Select Name from Colors Where ID = @ID", con);
        cmd.Parameters.AddWithValue("@ID", pointer);

        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();
        rdr.Read();
        string colorname = rdr.GetValue(0).ToString();
        con.Close();

        btnColor.Text = colorname;

        if (pointer == rows)
        {
            pointer = 1;
        }
        else
        {
            pointer++;
        }
    }

    private void FrmColor_Load(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("Select count(Name) From Colors", con);
        con.Open();
        rows = (Int32)cmd.ExecuteScalar();
        con.Close();
    }
}

Don't forget to add using System.Data.SqlClient; 不要忘记using System.Data.SqlClient;添加using System.Data.SqlClient;

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM