You must Sign In to post a response.
  • Category: .NET

    Getting false value of buttonclick event when exit from event?

    I have buttonColumn in datagridview which when clicked i set its bool value to true but when leaving this event values doesn't remains same , it sets false value when exit from event. why this happens,

    class form1()
    {
    protected bool isbuttonclick = false;

    public void Start()
    {
    new Thread(GetData).Start();
    }

    public void GetData()
    {
    if(isbuttonclick == true)
    {
    Messagebox.show("clicked succsess");
    }
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
    if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
    {
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
    if (Convert.ToBoolean(row.Cells[SerialNo.Name].Value) == true)
    {
    //do something//
    }
    // row.Cells[SerialNo.Name].Value = false;
    }
    isbuttonclick = true;
    ans = MessageBox.Show("Are you sure to send indent?", "Save & Print", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

    if (ans == DialogResult.Yes)
    {
    byte[] b = Encoding.ASCII.GetBytes("a");
    this.networkStream.Write(b, 0, b.Length);
    }
    }
    }

    Error Explanation Document

  • error (343532-1-error.docx)
  • #763510
    Hi,

    Use Static Keyword to over come this issue,

    Ex:

    public static bool isbuttonclick = false;


    once the boolean value is true until you assign false it maintain that over the postback.

    Try this and let me know if you still face any issue.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #763511
    Hello Priyanka,

    Refer the below code :

    static bool IsButtonClick = false;
    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    if (IsButtonClick == false)
    {
    IsButtonClick = true;
    }

    if (IsButtonClick == true)
    {
    MessageBox.Show("Value is true.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
    {
    MessageBox.Show("Value is false.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    }

    It will be true till you will not set false to IsButtonClick variable.

    Hope this will work for you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #763512
    protected members can be accessed only by code in the same class or struct, or in a class that is derived from that class.

    I hope in your case you are trying to use set the values in the Button click and trying to get the value in the Thread.

    I think the thread is running before setting the the value in the button click.

    Even though u set "Static" access modifier you may get false value if you call the thread before setting the value in the click event. So make sure you are calling the thread after setting the values.

    By Nathan
    Direction is important than speed

  • #763513
    Thanks everyone all issues happens because of not declaring static. Thanks once again , I learnt something new today.


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.

    Submit New Thread
    Return to Return to Discussion Forum