I have the following codes that is the button on and off:
private void OnActivity_Click(object sender, RoutedEventArgs e)
{
if ((sender as Button).Content.Equals("On"))
{
(sender as Button).Content = "Off";
}
else
{
(sender as Button).Content = "On";
}
}
I have another method named protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
How can I call the sender as Button of OnActivity_Click into another method ? I need to trigger On and Off then On again in the navigation method. Someone help me pls ! Thanks !
Inside OnNavigatedTo you can use the button reference. For example:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
OnActivity_Click(this.btn1, null);
}
Your button here has the name "btn1" (set it in xaml).
Give name to the button in xaml like x:name="btn"
and then use it in OnNavigatedTo method like this,
btn.content="On";
Related
I have multiple button click events:
private void button1_Click(object sender, EventArgs e)
{
Procedure(1);
}
private void button16_Click(object sender, EventArgs e)
{
Procedure(16);
}
However, I want to achieve something like this:
private void button[i]_Click(object sender, EventArgs e)
{
Procedure(i);
}
In Winforms, there is a call to InitializeComponent() in the class constructor.
In that method (it will take you to the form designer if you put the mouse cursor on the method and right click > goto definition or F12) you will see how events are hooked up:
button1_Click += button1_Click...
You can simply subscribe the buttons click event to your Procedure method.
button1_Click += CallToProcedure;
How do you work out which button was clicking then? You simply get it off the sender argument in the parameter:
private void CallToProcedure(object sender, EventArgs e)
{
Button btn = sender as Button;
int i = Convert.ToInt32(btn.Name.Replace("button", string.Empty));
Procedure(i);
}
You can store the parameter you're passing to your button (i in your example) in the Button's Tag property then use something like this:
var senderButton = sender as Button;
if (sender != null)
{
Procedure((int) senderButton.Tag);
}
If you can rely on the naming, then simple:
private void button_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
int i = Convert.ToInt32(btn.Name.Replace("button", ""));
Procedure(i);
}
use button.Tag property, is designed for such cases.
read is doc / MSDN
I want to make a click event for a bunch of buttons. The problem is that I want to use the button's Text, and pass it to a function. Now the click event is passed a object sender. When I tried changing that to Button sender, it gave errors. But I don't know how else I can work with the senders Text.
Here is the normal code, which gave a single error:
private void guess_Click(object sender, EventArgs e)
{
guess(sender.Text);
}
I changed it to this, which gave errors:
private void guess_Click(Button sender, EventArgs e)
{
guess(sender.Text);
}
Question:
How can I work with the Button's Text property within this click event, which is a single click_event for multiple buttons?
Step 1: You need to subscribe to the Button Click event of all your buttons to the same EventHandler. so that button click on all your Buttons will fire the same `Event Handler.
Step 2: You need to cast the object sender into Button and then access its Text property to get the Button Text.
Try This:
button1.Click += new System.EventHandler(MyButtonClick);
button2.Click += new System.EventHandler(MyButtonClick);
button3.Click += new System.EventHandler(MyButtonClick);
private void MyButtonClick(object sender, EventArgs e)
{
Button btnClick = (Button)sender ;
guess(btnClick.Text);
}
Cast sender to type button.
Example:
private void guess_Click(object sender, EventArgs e)
{
guess(((Button)sender).Text);
}
You need to cast the sender object to the Button type and use that:
private void guess_Click(object sender, EventArgs e)
{
Button senderBtn = senderBtn as Button;
if(senderBtn != null)
{
guess(senderBtn.Text);
}
}
In Windows 8, when a page is navigated, I need to put the focus to a specific TextBox.
I used:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
myControl.Focus(Windows.UI.Xaml.FocusState.Keyboard);
}
But it does not work. However, if I call myControl.Focus(Windows.UI.Xaml.FocusState.Keyboard); from a button click event, it works fine.
How can I set the focus to a TextBox when the page is loaded?
Try calling myControl.Focus(Windows.UI.Xaml.FocusState.Keyboard);
with this.Dispatcher
A code sample:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
Dispatcher.RunAsync(
CoreDispatcherPriority.Normal,
() => myControl.Focus(FocusState.Keyboard));
}
Try this:
myControl.Focus(Windows.UI.Xaml.FocusState.Keyboard);
and set this control(mycontrol) as the first control in your XAML code.
On Windows Phone 8.1 add a Loaded event to the page and call the focus in there instead of OnNavigatedTo event
private void Page_Loaded(object sender, RoutedEventArgs e)
{
SearchTextBox.Focus(FocusState.Keyboard);
}
I did it by adding it in the Loaded event of the TextBox:
xaml:
<TextBox x:Name="SearchTextBox"
Loaded="SearchTextBox_Loaded"/>
in the code behind .cs:
private void SearchTextBox_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
SearchTextBox.Focus(Windows.UI.Xaml.FocusState.Keyboard);
}
I am trying to do something like: when a user select an item on a listbox, then the function listboxClicked will be triggered. However, the first click often does not able to trigger the function. It only triggers the function when I click the same item or another item for the second time.
May I know what's wrong with my code? Thank you.
My Code:
private void listbox_SelectedIndexChanged(object sender, EventArgs e)
{
listbox.MouseClick += listboxClicked;
}
private void listboxClicked(object sender, EventArgs e)
{
if (listbox.SelectedIndex != -1)
{
//do something
}
}
Try this one:
Listbox1_SelectedValueChanged(object sender, EventArgs e)
{
Listbox listbox = (Listbox)sender;
MessageBox.Show(listbox.SelectedItem.ToString());
}
How can I click or load the click() event of a button in codebehind?
I already tried
btn.Click();
but this gives an error. I am using ASP.NET
I will assume that you have a button called Button1
and that you have double clicked it to create an event handler.
To simulate the button click in code, you simply
call the event handler:
Button1_Click(object sender, EventArgs e).
e.g. in your Page Load Event
protected void Page_Load(object sender, EventArgs e)
{
//This simulates the button click from within your code.
Button1_Click(Button1, EventArgs.Empty);
}
protected void Button1_Click(object sender, EventArgs e)
{
//Do some stuff in the button click event handler.
}
If you don't need the context provided by the sender and eventargs then you can just refactor to create a method (eg DoStuff()) and make your event handler just call that. Then when you want to call the same functionality from elsewhere you can just call DoStuff(). It really depends on whether you want to actually simulate a click or not. If you want to simulate a click then other methods are better.
Do you wish to call all the event handlers attached to the button, or just one?
If just one, call the handler:
btn.btn_Click(btn, new EventArgs());
If all of them, trigger the event:
var tmpEvent = btn.Click;
if (tmpEvent != null)
tmpEvent(btn, new EventArgs());
Try this:
YourButton_Click(sender, e);
Edit try this:
protected void Page_Init(object sender, EventArgs e)
{
Button1.Click += new EventHandler(Button1_Click);
}
void Button1_Click(object sender, EventArgs e)
{
}
in the .cs page
Just call the buttons click function.
http://forums.asp.net/t/1178012.aspx
Instead of trying to call the event just call the function that event uses.
Example: btn.Click() calls
btn_Click(object sender, EventArgs e)
so you should call the function btn_Click(btn,new
EventArgs());
It's been a long time ago, but here is my solution
You can use:
btn.PerformClick();
protected void Page_Load(object sender, EventArgs e)
{
functionName();
}
protected void btn_Click(object sender, EventArgs e)
{
functionName();
}
public void functionName()
{
//function code goes here, and call this function where ever you want
}
try this one.......
YourButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
This works for me.