Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
would you please tell me what is wrong with this code?
C#
if (button1.Click = true)
{
    this.label1.Text = "custom event";
}


Error is:
the event "...control.click" can only apear on the left hand side of += or -=

I have here a custom event which several buttons can cause.
But in the code I have to know which button caused that custom control.

thank you all

[Edit]Code block added by Jibesh[/Edit]
Posted
Updated 2-Mar-13 22:57pm
v2
Comments
OriginalGriff 3-Mar-13 5:16am    
"no, there is something else
doesn't work"

Is not very helpful. What "doesn't work"? How doesn't it work? What does it do?
Member 9226240 3-Mar-13 8:52am    
sorry, this comment was for first solution, just written to wrong place.

Click is an event - so you can't compare it to a boolean value.

What you actually need to do is look inside the Event handler at the sender parameter, which identifies the button that was pressed:
C#
private void AnyOfMyButtons_Click(object sender, EventArgs e)
    {
    Button wasClicked = sender as Button;
    if (wasClicked != null)
        {
        Console.WriteLine(wasClicked.Text);
        }
    }
Will print the text on the button that was clicked, for example.
 
Share this answer
 
Comments
Jibesh 3-Mar-13 5:07am    
OP Posted this comment as solution

no, there is something else
doesn't work
The problem is here: button1.Click = true. The Button.Click[^] is an event. You can't test it this way. You have to assign an event handler to it, like in the example on the page above.
But I can't imagine how you came to this, since all IDE's create the event handler for you if you choose the event, or simply DoubleClick on the button in design mode.

I have the feeling you treat something in the wrong manner. What is the rest (especially the header) of the method, this code snippet resides in?
 
Share this answer
 
Comments
Jibesh 3-Mar-13 5:06am    
OP Posted this comment as solution

no, there is something else
doesn't work
Member 9226240 3-Mar-13 5:07am    
there are several user controllers. one of them is a user controller with some buttons which controlls 6 layers (userControllers) and make them visible/unvisible.
the one button which is pressed controlls the visibility of his user controller on the main form. (makes it visible, during unvisible others)I was thinking it is a better Idea to use like this.
Zoltán Zörgő 3-Mar-13 5:10am    
1) Are speaking about Windows Forms?
2) What is "user controller"?
3) You have assigned the same event handler to many controls, and you what to test which of them triggered the event?
Member 9226240 3-Mar-13 5:16am    
1=yes, windows forms
3=yes
Hi,
"button1.Click" is an event.If you want to create a custom event you have to add the following code.
This code will generate a run-time button control for you and can perform desired operation on its click event.
C#
private void Form1_Load(object sender, EventArgs e)
       {
           Button btn = new Button();
           btn.Text = "custom button";
           this.Controls.Add(btn);
           btn.Location = new Point(10,10);//you can add the control on your desired location.
           btn.Click += new System.EventHandler(btn_Click);
       }
       private void btn_Click(object sender, EventArgs e)
       {
           //add your code here.
          
       }
 
Share this answer
 
As far as I am aware you test which button is pressed in the event handler roughly shown below, you will need to adjust it too your needs accordingly

C#
Button btn = sender as button;

if (btn.name == "buttonname1")
{
 label1.text = "custom event";
}
 
Share this answer
 
v2
I don't understand what the problem or the requested result are.

it looks like you want to have something like this:

C#
public class MyForm : Form
{
	Button button1;
	Button button2;
	
	public void init()
	{
                //register method to handle event
		button1.Click += handleClick;
		//maybe also
		button2.Click += handleClick;
	}
	
	public void handleClick(object sender, EventArgs e)
	{
		if(send==button1)
		{
			this.label1.Text = "custom event";
		}
		//do another thing even if not click from button1
	}
}
 
Share this answer
 

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