Hi all,

I've declared a static int variable call Click=0; in the Pay.Designer.cs.
then i just want to get the value of it in a button click of another form.
But it says :
The event 'System.Windows.Forms.Control.Click' can only appear on the left hand side of += or -=
It gives aline under Click

public partial class LostBookEntry : Form
    {
  private void button1_Click(object sender, EventArgs e)
        {
                Pay ppay = new Pay(int.Parse(dataGridView1.Rows[ee.RowIndex].Cells["Fine"].Value.ToString()), "Fine", textBox1.Text);
               
                ppay.Show();
                int i = ppay.[B]Click;[/B]
                if (i==1)
                {
                    Pay pppay = new Pay(int.Parse(dt.Rows[0][0].ToString()), "Lost",
textBox1.Text);
                    pppay.Show();
           
        }
}

this is my

namespace HD
{
    partial class Pay
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        /// 
        //internal static int click = 0;
        internal System.Int32 Bid = 0;
        internal System.Int32 code = 0;
        internal string mid = null;
        [B]internal static int click = 0;[/B]

        public Pay(int i,string cat,string mid)
      
        public Pay(int i,string s,System.Int32 bid,System.Int32 ccode)
        
        public Pay(string s)
      
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

       
        internal System.Windows.Forms.Label Label5;
        internal System.Windows.Forms.Label Label4;
        internal System.Windows.Forms.TextBox TextBox2;
        internal System.Windows.Forms.Label Label3;
        internal System.Windows.Forms.Button Button2;
        internal System.Windows.Forms.Button Button1;
        internal System.Windows.Forms.Label Label2;
        internal System.Windows.Forms.ComboBox ComboBox1;
        internal System.Windows.Forms.TextBox TextBox1;
        internal System.Windows.Forms.Label Label1;

    }
}

Pls help me.

Dani AI

Generated

The compiler is complaining because ppay.Click resolves to the WinForms Click event on Control, which is write-only from the outside (+=/-=). You cannot read an event. Also, C# is case-sensitive, so naming a field click invites a collision with Click on Form, as spotted. Even if you use the type name, relying on a static field in a form is brittle. A better pattern is to return a result from the dialog or raise your own domain event with a clear name.

Example: expose a result on Pay, show it modally, and read it only if the user confirmed.

// Pay.cs
public partial class Pay : Form
{
    public bool PaymentConfirmed { get; private set; }

    private void okButton_Click(object sender, EventArgs e)
    {
        PaymentConfirmed = true;
        DialogResult = DialogResult.OK; // closes dialog
    }
}
// Caller
if (!int.TryParse(dataGridView1.CurrentRow?.Cells["Fine"]?.Value?.ToString(), out var amount))
{
    MessageBox.Show("Fine amount is invalid.");
    return;
}

using (var dlg = new Pay(amount, "Fine", textBox1.Text))
{
    if (dlg.ShowDialog(this) == DialogResult.OK && dlg.PaymentConfirmed)
    {
        using (var lostDlg = new Pay(/* your lost params here */))
            lostDlg.ShowDialog(this);
    }
}

Notes:

  • is right: keep your code out of Pay.Designer.cs. Designer files can be regenerated and wipe changes. Put fields, properties, and handlers in Pay.cs.
  • Prefer descriptive names like PaymentConfirmed or ClickCount over click/i so intent is obvious.
  • As suggested, avoid long one-liners that assume parsing will succeed. int.TryParse plus clear validation messages will save you time later.
  • If this handler is on a Button click, ee.RowIndex will not exist. Use CurrentRow/SelectedRows or capture the row index earlier.

Recommended Answers

All 4 Replies

To reference static fields you need to use the class name not the name of an instance.
e.g. int i = Pay.Click; .
The error you are getting is because ppay.Click refers to the Click event that can only be set.

[Edit] I would also advise against writing you own code in the designer.
Put you code in the and leave to the design tools.
That way you are certain that you will never loose any of your code when you re-design.

To reference static fields you need to use the class name not the name of an instance.
e.g. int i = Pay.Click; .
The error you are getting is because ppay.Click refers to the Click event that can only be set.

[Edit] I would also advise against writing you own code in the designer.
Put you code in the and leave to the design tools.
That way you are certain that you will never loose any of your code when you re-design.

oh i understood.
thank you fo your advice.

I would recommend not put that whole line for the int.Parse on one line - unless you can guarantee that the cell value will always parse to an integer. It is bad form, and will make it harder to find errors down the track.

Same for this line... dt.Rows[0][0].ToString() - presumably the first cell contains a 'magic value'. If you wrote this, and left the company, and someone else comes along and changes the text, they could inadvertently creak the application, it's a little brittle. Better to use a contact, such as _TITLE (if that is what you are intending) - that also makes unit testing must easier and readable.

I know that wasn't the question you asked, but I just thought I'd mention it!

You are also being affected by the fact that C# is case-sensitive.

You defined your property as "click" (all lower case). But your code referenced ppay.Click (capitalized). Most likely you can thank auto-complete for that.

I would also suggest that you choose your names more carefully. "click" is an integer, you assign it to "i" and compare it with 1. None of that gives the reader any clue what "click" is. Is it a count? Is it a flag? click's purpose isn't apparent to someone reading your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.