c# - 如何在 C# 中单击按钮时更改按钮颜色?
问题描述
我正在创建一个在线测试应用程序,其中我在运行时在表单加载时生成大约 100 个按钮。这是一段代码:
private void addQuestion_Reviewbutton()
{
for (int i = 1; i <= clsGlobalVars.gnTotalQuestion; i++)
{
Button button = new Button();
button.Location = new Point(160, 30 * i + 10);
button.Click += new EventHandler(ButtonClickOneEvent);
button.Tag = i;
button.Name = "Question" + i;
button.Text = i.ToString();
button.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.button));
button.BackgroundImageLayout = ImageLayout.Stretch;//.Zoom;
button.FlatAppearance.BorderSize = 0;
button.Size = new System.Drawing.Size(47, 41);
button.BackColor = Color.Transparent;
button.FlatStyle = FlatStyle.Flat;
button.Font = new System.Drawing.Font("Segoe UI Semibold", 12);
button.ForeColor = Color.White;
button.Cursor = Cursors.Hand;
flowLayoutPanel1.Controls.Add(button);
}
}
并单击此按钮,我正在更改背景颜色。
void ButtonClickOneEvent(object sender, EventArgs e)
{
Button button = sender as Button;
//button.BackColor = Color.Yellow;
button.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.button_Orange));
lblQuestionNo.Text = ((int)button.Tag).ToString()+".";
btnNext.Focus();
}
我在名为“下一步”的表单上有一个按钮。现在我的问题是,如果我目前的问题是“1”。然后我按下按钮下一步我想更改文本为“2”的按钮的背景图像。
我需要您的宝贵指导来解决这个问题。
解决方案
哟检查这个!
tldr; 指导是
点击事件是否转到 ButtonClickOneEvent ?
如何调用其他按钮
...
利润?
protected void Page_Load(object sender, EventArgs e) { addQuestion_Reviewbutton(); } void ButtonClickOneEvent(object sender, EventArgs e) { Button button = sender as Button; var currentbtn_id = button.ID; int nextid = Convert.ToInt32( currentbtn_id.Substring("Question".Length)) + 1; var nextBtn = flowLayoutPanel1.FindControl("Question"+ nextid.ToString() ); (nextBtn as Button).BackColor = Color.Red; }
推荐阅读
- python - Python , pyinstaller 与 matplotlib 和 numpy
- sql - MariaDB/SQL:如何在所有表中查找系统 ID 列等于指定值的行?
- java - 如何在运行时覆盖特定模块的 Maven 属性?
- amazon-web-services - AWS lambda:防止通过 S3:TestEvent 调用表单
- javascript - 有没有其他人在他们的“ServiceArea”实体上使用邮政编码来进行食品订购谷歌操作并能够使它们工作?
- kubernetes - 创建 kubernetes java 客户端控制器来监视 pod
- google-analytics - Bigquery Intraday 表覆盖过程
- javascript - React Redux 重新选择 - 未定义状态
- excel-online - 如何删除工作表(如果存在)并使用 Office 脚本创建具有相同名称的新工作表
- php - MySQLi Query - 结果颜色替代表行