如何检查表单上是否点击了按钮? [英] How to check button clicked or not on form?

查看:78
本文介绍了如何检查表单上是否点击了按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一张表格说A。我想执行一个任务,其中表单A执行代码的某些部分,需要在按钮单击同一表单上执行。在这种情况下控制应该等到按钮点击并且点击按钮后,应该执行下面的代码。



以下是我的代码:



  private   void  button1_Click_1( object  sender,EventArgs e) //  发送按钮//  
{
try
{
List< DataGridViewRow> rows_with_checked_column = new List< DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToBoolean(row.Cells [Column4.Name] .Value)== true
{
rows_with_checked_column.Add(row);
}
}
foreach (DataGridViewRow row in dataGridView1.Rows )
{
if (Convert.ToBoolean(row.Cells [Column4.Name] .Value)== true
{
for int i = 0 ; i < = row.Cells.Count - 1 ; i ++)
{
row.Cells [i] .Style.BackColor = Color.Aqua;
// data = row.Cells [1] .Value.ToString();
}
}
row.Cells [Column4.Name] .Value = false ;
}
}
catch {}
}

方法: :
public void GetData()
{
case _
if (res.StartsWith( _))
{
System.Windows.Forms.MessageBox.Show( 发送缩进设备);
unscr = Encoding.ASCII.GetBytes( @!#);
networkStream.Write(unscr, 0 ,unscr.Length);

if // 检查button1是否被点击的条件//)
{
string d = ifile.IniReadValue( Config Section INDENT_NO);
unscr = Encoding.ASCII.GetBytes( + data);
networkStream.Write(unscr, 0 ,unscr.Length);
hash = Encoding.ASCII.GetBytes( );
networkStream.Write(hash, 0 ,hash.Length);
}
else
{ return ; // 这应该在相同的情况下执行};
}
}





在上面的代码中,当控件出现在这种情况下时,首先检查if condition是否点击了上面的按钮如果没有点击else语句控制再次返回到case并再次检查,直到contion变为true并且当它变为true值时它会执行if中的代码并且离开并打破大小写。



编辑:



案例采用不同的方法getdata()。

解决方案

< pre lang =c#> bool isBtnClicked = false ;
private void button1_Click_1( object sender,EventArgs e) // 发送按钮//
{
< span class =code-comment> // 您的代码..

isBtnClicked = ;
}


case _
if (res.StartsWith( _))
{
System.Windows.Forms.MessageBox.Show( 向设备发送缩进);
unscr = Encoding.ASCII.GetBytes( @!#);
networkStream.Write(unscr, 0 ,unscr.Length);

if (isBtnClicked)
{
string d = ifile.IniReadValue( Config Section INDENT_NO);
unscr = Encoding.ASCII.GetBytes( + data);
networkStream.Write(unscr, 0 ,unscr.Length);
hash = Encoding.ASCII.GetBytes( );
networkStream.Write(hash, 0 ,hash.Length);
isBtnClicked = false ;
}
else
{ return ; // 这应该是相同的情况下执行};
}





-KR


Hello ,
I have 1 form says A . I want to perform a task in which form A executing some part of a code which is needed to be execute on button click on same form . Control should wait in that case till button click and after button clicked, bellow code should executes.

Following is my code :

private void button1_Click_1(object sender, EventArgs e) //send button//
{
      try
      {
         List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
         foreach (DataGridViewRow row in dataGridView1.Rows)
         {
               if (Convert.ToBoolean(row.Cells[Column4.Name].Value) == true)
               {
                      rows_with_checked_column.Add(row);
               }
         }
         foreach (DataGridViewRow row in dataGridView1.Rows)
         {
               if (Convert.ToBoolean(row.Cells[Column4.Name].Value) == true)
               {
                  for (int i = 0; i <= row.Cells.Count - 1; i++)
                  {
                     row.Cells[i].Style.BackColor = Color.Aqua;
                     // data = row.Cells[1].Value.ToString();
                  }
               }
            row.Cells[Column4.Name].Value = false;
            }
         }
      catch { }
}
 
 The method::
 public void GetData()
{
case "_":
if (res.StartsWith("_"))
{
     System.Windows.Forms.MessageBox.Show("Send indent to Device");
      unscr = Encoding.ASCII.GetBytes("@!#");
      networkStream.Write(unscr, 0, unscr.Length);
 
      if (//conditin that checks button1wasclicked or not//)
      {
            string d = ifile.IniReadValue("Config Section", "INDENT_NO");
            unscr = Encoding.ASCII.GetBytes("!" + data);
            networkStream.Write(unscr, 0, unscr.Length);
            hash = Encoding.ASCII.GetBytes("#");
            networkStream.Write(hash, 0, hash.Length);
      }
      else
         { return; //this should go in same case to be executes };
 }
}



In the above code when control comes in this case first it checks in "if condition" that the above button is clicked or not if not clicked in "else statement control again returns to case and again checks till if contion gets true and when it gets true value it executes code in if and leave and break case."

editted :

the case is in different method getdata().

解决方案

bool isBtnClicked = false;
private void button1_Click_1(object sender, EventArgs e) //send button//
{
// your code..

isBtnClicked = true;
}


case "_":
if (res.StartsWith("_"))
{
     System.Windows.Forms.MessageBox.Show("Send indent to Device");
      unscr = Encoding.ASCII.GetBytes("@!#");
      networkStream.Write(unscr, 0, unscr.Length);
 
      if (isBtnClicked)
      {
            string d = ifile.IniReadValue("Config Section", "INDENT_NO");
            unscr = Encoding.ASCII.GetBytes("!" + data);
            networkStream.Write(unscr, 0, unscr.Length);
            hash = Encoding.ASCII.GetBytes("#");
            networkStream.Write(hash, 0, hash.Length);
            isBtnClicked = false;
      }
      else
         { return; //this should go in same case to be executes };
 }



-KR


这篇关于如何检查表单上是否点击了按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆