Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :

C#
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().
Posted
Updated 19-Nov-15 18:57pm
v2
Comments
Salman622 16-Nov-15 2:03am    
i would suggest to use background worker here
for eg.
as you say you have a form A.
and i am adding to buttons to it.
say for eg. btn1 and btn2
when you click on btn1 it perform some task and wait for btn2 to be clicked

here what you can do is.
on btn1 click give the task to a background worker in while loop.
in while loop you can have if block like if(//boolean variable to check){//your logic}

and on btn2 click set a variable to true.
in this case what happen is your background code will run continously untill the condition meets and after performing a task you can stop the background worker.
Member 11543226 16-Nov-15 2:32am    
can you give me any example regarding this

1 solution

C#
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
 
Share this answer
 
Comments
Member 11543226 16-Nov-15 1:28am    
i tried this before but when control is in button click event it gets true value but when control outs from event it again gets false value so my case if statement always gets false.
Krunal Rohit 16-Nov-15 1:31am    
You button and the method are on the same form ?

-KR
Member 11543226 16-Nov-15 1:33am    
yes Can you tell me why this happends?
Krunal Rohit 20-Nov-15 1:10am    
Maybe the your button causing the PostBack.

-KR
Member 11543226 20-Nov-15 1:25am    
i am using winapp so is there any solution to disable postback.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900