简体   繁体   English

将按钮作为C#中的参数传递

[英]passing button as a parameter in C#

I'm new to C# and I want to practise my skills on using parameters however I ran into a little trouble. 我是C#的新手,我想练习如何使用参数,但是遇到了一些麻烦。 I am designing a system where the system should view each question from the database whenever the student clicks on the 'view' button. 我正在设计一个系统,每当学生单击“查看”按钮时,系统都应从数据库中查看每个问题。

OBJECTIVE: 目的:

what I want to achieve is that when the user clicks on the button which is labelled 'next'. 我要实现的是,当用户单击标记为“下一个”的按钮时。 The system should 'view; 系统应“查看; the next question onto the datagrid. 下一个问题放在datagrid上。 I thought maybe I should make a query which says something like "select question from ... where questionID = btnView. And maybe have a function where whenever the user clicks on the button it passes 1,2,3...10 as questionID (as there are 10 questions) 我以为也许我应该做一个查询,上面写着“从...中选择问题……wherequestID = btnView。”,也许有一个函数,每当用户单击按钮时,它就会通过1,2,3 ... 10作为questionID (因为有10个问题)

this is my C# Code: 这是我的C#代码:

try
{
string mydbConnection = "datasource=localhost;port=3306;Initial Catalog=project;username=***;password=***;";
MySqlConnection connDB = new MySqlConnection(mydbConnection);
MySqlCommand cmdDataBase = new MySqlCommand("SELECT questions.question, questions.answer FROM questions WHERE questionID ='" + questionID +  "' ;",connDB);
connDB.Open();
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource1 = new BindingSource();
sda.Update(dbdataset);
bSource1.DataSource = dbdataset;
dataGridView1.DataSource = bSource1;
sda.Update(dbdataset);
this.dataGridView1.Columns[3].Visible = false;
this.dataGridView1.Columns[0].Visible = false;
connDB.Close();
}

and this is my button function to get each 'questionID' 这是我获取每个“ questionID”的按钮功能

private void button1_Click(object sender, EventArgs e)
{
   for (int i = 1; i >= 10; i++)
  {
    int questionID = i;
  }
  viewQuestion(questionID);
}

questionID does not exist in the current context. questionID在当前上下文中不存在。

NOTE: 注意:

The stuff on the datagrid WORKS I just want it to view each question when the user clicks on the button. 我只希望datagrid上的内容在用户单击按钮时查看每个问题。

EDIT: 编辑:

  int questionID =  0;
  for (int i = 1; i >= 10; i++)
  {
    int questionID = i;
  }
  viewQuestion(questionID);

causes: 原因:

A local or parameter named 'questionID' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter 无法在此范围内声明名为“ questionID”的本地或参数,因为该名称在封闭的本地范围内用于定义本地或参数

when the user clicks on the button which is labelled 'next'. 当用户单击标记为“下一个”的按钮时。 The system should 'view'. 系统应“查看”。

So there is no need for a loop in a single click, You have to make the questionID as a global variable in the same class, and increment the value of questionID in each click. 因此,无需单击即可循环。您必须将questionID设为同一类中的全局变量,并在每次单击中增加questionID的值。 which means you can do something like this: 这意味着您可以执行以下操作:

int questionID = 0;   // Global variable
private void button1_Click(object sender, EventArgs e)
{
   if(questionID <=10)
   {
      viewQuestion(questionID);
      questionID++;
   }
   else
   {
      // Display message that question over
   }
}

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

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