why is Xamarin giving error with button Clicked? - c#

Basically I am getting this error
EventHandler "PressMeButton_Clicked" with correct signature not found in type "login.MainPage" (login)
login is the name of my project and login_Clicked is my clicked event handler
I'm trying to make a simple login page,and on the documentation I've seen various ways of Clicked working but everytime I try to write the method on the xaml.cs file when I compile it keeps giving the error above, if I remove the method it works.
<Button x:Name="PressMeButton"
Text="Press Me!"
Pressed="PressMeButton_Pressed"
Clicked="PressMeButton_Clicked" />
this is the XAML part you can see on Clicked the name I have for the event
and
private void PressMeButton_Clicked(object sender, EventArgs e)
{
(sender as Button).Text = "I was just clicked!";
}
So I expect to show me a text saying I was clickd but it does not even compile.

This is an old question, but I ran into a similar issue. There was no good explanation why it would give the error. It would even happen on an empty handler that the IDE generated itself. I tried cleaning the solution, rebuilding, etc.
I found that there were some stuck build tasks going still. They appeared in the bottom bar that normally shows "Ready". For me, the solution was restarting VS. Once it started back up, and build worked fine.

event handlers in the code-behind should be protected, not private
protected void PressMeButton_Clicked(object sender, EventArgs e)
{
(sender as Button).Text = "I was just clicked!";
}

Related

C# Winforms Event is adding _1 at the end of everything

My project became corrupt today and since fixing i have noticed that at the end of events like button click its adding _1 at the end of everything
for example
private void createNewUserToolStripMenuItem_Click_1(object sender, EventArgs e)
{
}
where as it should obviously be
private void createNewUserToolStripMenuItem_Click(object sender, EventArgs e)
{
}
I have no idea what has happened but its causing me to repeat a lot of work. I have not duplicated controls or anything like that so i am very confused
Does anyone have any ideas? Please this is over 2 years of work and its looking like its knackered
Thanks to Idle_Mind this worked -
The designer lost the fact that the control was wired up and when you double click it it sees that there is already a method with that name so it adds one. All you need to do is select that control, go to the Events listing in the Properties pane, find that event, and then change the listed handler using the dropdown back to the original method. –

.NET Maui Event Handlers not being added at XAML Level

I'm starting to wonder if it's just some setting somewhere, but adding event handlers to controls in XAML is proving to be frustrating in .NET Maui. For instance, if I just want to create this button:
<Button Text="Good job"
x:Name="GoodJobButton"
Clicked="GoodJobButton_Clicked"/>
It will create a perfectly fine Event Handler in the code behind:
private void GoodJobButton_Clicked(object sender, EventArgs e)
{
//Do cool magic
}
But it's not actually attaching the handler to the control. I get an error unless I do this in the constructor:
GoodJobButton.Clicked += GoodJobButton_Clicked;
And WOW, I don't want to have to do that for everything I have in this app. I'm migrating over from Xamarin, and this is pretty much the last problem I have to solve until I'm off and running again.
Anyway, please tell me this is a bug or something. Otherwise, shame me if you must. Thanks!

Textbox_Enter event not does nothing

I want to be able to tell when the user cliked on a textbox and is in "edit" mode. Everywhere I look I see the textbox_Enter and textbox_Leave events being used with the instructions within that and it works fine. For me however it doesn't do anything. I tried elimination as many outside factors as possible, including creating a brand new project just for testing purposes and copied some code samples yet again nothing happens when I click on the textbox. I'm using Visual Studio 2017 on Windows 10 with Visual C# Application Windows Form (.NET Framework)
Also here's a sample of the code I try to use if it helps for whatever reason
private void textbox_Enter(object sender, ControlEventArgs e)
{
label.Text = "ok";
}
First of all the type of the e parameter is not correct: It must be EventArgs, not ControlEventArgs:
private void textbox_Enter(object sender, EventArgs e)
{
// Do something
}
Second, you need to register the event in the forms designer with the textbox control in the properties window:
You need to wire up this method to the textbox enter event. Select the control and then look at the events section in the properties tab.

Xamarin forms xaml button click handler not fired c#

I has rey use VS 2015 with xamarin forms to create a cross platform project. I add the xaml page which contain a text box and button. In the code behind, I has code as below
public Inno()
{
InitializeComponent();
btntest.Clicked += Btntest_Clicked;
}
Private void Btntest_Clicked(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(Name.Text))
{
return;
}
}
When I run the android simulator to test the page, it fire the constructor. But when I clicke the button, it not fire the click event. Anything goes wrong? Please help. Thanks
Theres a few things you can try:
hook up the event handler in the xaml, see if that works - because you are subscribing to the event in the constructor this shouldn't make a difference functionality wise.
Edit: to do this, in your xaml:
<Button Clicked="buttonClicked" />
and Press tab when intellisense prompts you to create a new handler.
as #hankide said - set a breakpoint and make sure it isn't actually entering it and that the problem isn't the code inside the event handler.
You need to make sure that your btntest object has been properly mapped to the button on the XAML side of things. By default, XAML objects don't have "names" as you may be accustomed to in WinForms or WebForms and need to have a name property assigned to them. For me, I typically have my buttons marked up as:
<Button x:Name=btntest Text="Click Me></Button>
Then in the code behind I have to find the button by name in order to wire up the event:
var testButton = this.FindByName<Button>("btntest");
testButton.Clicked += Btntest_Clicked;
From there everything works as expected.
Try this:
btntest.Clicked += new EventHandler(Btntest_Clicked);
Thanks everyone for the answer...not sure why I close the solution and re-open it, it work fine.

How do you PerformClick(); for a button on a different tab?

I'm using Visual c# express 2010, I have 3 tabs and on the first tab there is a button that exits the program. I'm trying to call that button click on the 2nd and 3rd tab with
btnExit.PerformClick();
but since it isn't visible nothing happens. How would I call the invisible button click?
any help would be appreciated
EDIT:
Thanks for the replies, the two answers work great but I found a way that I think is easier and better.
instead of systematically changing tabs or calling a whole different method, I did this
btnExit_Click(sender, e);
I can put that in any other button click and it works great, very simple to.
I think it's better to create a method that actually has the code to exit the program, and call that method from btnExit click event and also other buttons click event, than PerformClick of the exit button.
void ExitApplication()
{
// code to exit the application
}
protected void btnExit_Click(object sender, EventArgs e)
{
ExitApplication();
}
protected void ButtonInOtherTab_Click(object sender, EventArgs e)
{
ExitApplication();
}
This way it's easier to read and understand.
myTabs.SelectedTab = specificTab;
btnExit.PerformClick();

Categories

Resources