Is it possible to make Windows Phone 8 back button event (OnBackKeyPress) by another method? I have been trying to call that event from outside button click or page initializer. but it gives an error?
OnBackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>(OnBackKeyPress);
No overload for 'OnBackKeyPress' matches delegate
'System.EventHandler'
Just override the back key press event like below,
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
//Do your work here
base.OnBackKeyPress(e);
}
You can try this one
public Page()
{
InitializeComponent();
BackKeyPress +=PageBackKeyPress;
}
void PageBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
// code
}
Just type "override" (without quotes) and then press space, all the overridden methods will appear, select the onBackKeyPress method.
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base.OnBackKeyPress(e);
}
this method will appear, now u can write ur chunk of code inside.
Related
The question is this:
when the mouse cursor moved on the button some thing should be happen but I don't know what exactly have to write
When you select the button in the VS-designer you will have access to the properties and events (lightning Icon in the property window).
In the events-listing are all events that the button can fire. May be for your purpose the events: ´MouseEnter´ and ´MouseLeave´ would be a good choice. Just double click the event and Visual Studio will generate the appropriate method. Like this:
private void button1_MouseEnter(object sender, EventArgs e)
{
// my code
this.button1.BackColor = Color.Red;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
// my code
this.button1.BackColor = Color.Green;
}
In my example I just change the backcolour of the button when the mouse is on the button and change it again when it leaves the button.
Practically you could run any code inside the generated method.
You can create eventHandler like this :
myButton.MouseMove += new MouseEventHandler(doSomething);
Where myButton is the button from which you want to trigger the event when mouse moves over it. and doSomething() is the method defined as like the following:
public void doSomething(object sender, MouseEventArgs e)
{
// do what ever you want
}
I am wanting to put Event KeyDown for when the user. Click on one button of the KeyBoard, and it will do something.
But It's not working, it says that my Form does not contain a definition for the button(btnCalc_Click) and no method extension.
My code is :
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
if (btnLimpar.Enabled)
{
btnLimpar.PerformClick();
}
else
{
this.btnLimpar_Click(null, new EventArgs());
}
}
if (e.KeyCode == Keys.End)
{
if (btnCalcular.Enabled)
{
btnCalcular.PerformClick();
}
else
{
this.btnCalcular_Click(null, new EventArgs());
}
}
}
It get's wrong in the code : this.btnCalcular_Click(null, new EventArgs());
Error: FormularioHospital.Form1' does not contain a definition for 'btnCalcular_Click' and no extension method 'btnCalcular_Click' accepting a first argument of type 'FormularioHospital.Form1' could be found.
`
Option 1:
Go to designer and simply double click on your button. it will create that method for you.
Option 2:
Create this method, then using designer, assign that method to click event of your button.
private void btnCalcular_Click(object sender, EventArgs e)
{
// codes that you want execute when your button clicked
}
First Option
You should create a button btnCalcular
Double Click on the Button
It will create the necessary procedure for you.
Second option
You can create a button btnCalcular
and Paste this code on your code behind
private void btnCalcular_Click(object sender, EventArgs e)
{
}
I'm a beginner and have an assignment in which I must program the game of NIM. I begin with 15 "tokens" and at each turn a maximum of three can be removed, or "hidden". So far I am hiding these tokens on click by doing the following.
private void button1_Click(object sender, EventArgs e)
{
button1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
}
I simply copied and pasted that multiple times and changed the button numbers so that my buttons will close on click. This might be obvious, but is there a more efficient way to do this, instead of having 15 button close methods?
You can use the same click event for every single button, and make use of the sender object, casting it to Button:
private void buttonsToClose_Click(object sender, EventArgs e)
{
((Button)sender).Visible = false;
}
Then just add that handler to every single button you want to close itself on click.
Note, though, that this will throw an InvalidCastException if you or anyone else uses this handler on an object that is not a Button, so if you're actually going to use this code I would add some sort of conditional to check the real type of the sender.
Additionally, you could reuse this for any Control object by casting sender to Control instead, given that Button inherits from Control, and all Control objects have the Visible property. Here's an example, with a conditional to guard against an invalid cast:
private void controlToMakeInvisible_Click(object sender, EventArgs e)
{
if (sender.GetType() == typeof(Control))
{
((Control)sender).Visible = false;
}
}
A final note - it seems from your post like you may have a slight misunderstanding about the way events are created and wired in with objects in Windows Forms. If you go into the Designer, add a click event, and see it pop into your Form code as follows:
private void button1_Click(object sender, EventArgs e)
the name of this method has no bearing on its function. The button1 part of button1_Click doesn't actually have any logical linkage with the Button button1 - it's just the default name assigned by the Designer. The actual assignment of the method button1_Click to the Button.Click event is auto-generated into your Form's Designer.cs method.
The point of this is that if you copy and paste button1_Click and change every incidence of button1 with button2, like so:
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
}
it's not going to fire when button2 gets clicked. In actual fact, it's never going to fire at all, because the method hasn't actually been connected to any controls/events.
just call your event in a foreach loop.
private void Form1_Load(object sender, EventArgs e)
{
foreach (var button in Controls.OfType<Button>())
{
button.Click += button_Click;
}
}
void button_Click(object sender, EventArgs e)
{
((Control) sender).Visible = false;
}
if you change:
Controls.OfType<Button>()
to
Controls.OfType<Control>()
it will set visible to false for any Control. so you can control what item you want the event to be raised for easily.
OfType summary: Filters the elements of an IEnumerable based on a specified type.
I made a WebBrowser and it works except the back button after pressing the back button the app closes and does not go back one in history. How can I solve the problem? I found solutions in the internet but they don't seem to work.
public MainPage()
{
this.InitializeComponent();
this.webBrowser.Navigate(new Uri("http://www.google.com", UriKind.Absolute));
this.webBrowser.LoadCompleted += webBrowser_LoadCompleted;
this.webBrowser.NavigationFailed += webBrowser_NavigationFailed;
this.webBrowser.IsScriptEnabled = true;
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
webBrowser.InvokeScript("eval", "history.go(-1)" );
}
P. S.:that is not the whole script but i think the rest is unneccesary if not tell me :)
P. P. S.:I'm new to Windows Phone programing.
Web browser is just a control inside the page and pressing the device back button navigates back to the previous page or exits the app if it has only one page. So, you would need to stop page navigation on back key press something like this.
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel=true;
}
This prevents a backnavigation
Now rest is to go to the previous page which can be done by
webBrowser.InvokeScript("eval", "history.go(-1)" );
so the event becomes
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
e.Cancel=true;
webBrowser.InvokeScript("eval", "history.go(-1)" );
}
Try to do:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
WB1.InvokeScript("eval", "history.go(-1)");
e.Cancel = true;
}
When you override OnBackKeyPress, and you don't perform e.Cancel = true; it will do your code, but will also do what normal BackButton does - NavigateBack, Exit App and so on. But you must remember to leave the User an ability to exit your App or Navigate Back, so it will be more suitable to check some conditions (e.g. your webbrowser history is not null) and then do e.Canel, otherwise Exit the App.
To exit the app when you are in the root (so you can approve the cerfitication requirements), and also go back in navigation until you are in the root, try with this
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
if (MiniBrowser.CanGoBack){
e.Cancel = true;
MiniBrowser.InvokeScript("eval", "history.go(-1)");
}
}
I try to record some internet activity through mouse hook in webbrowser control in winform application. Vary rare happens to click to a link and to not record that click just because everything happens too quickly and my code in
public void WebBrowser1Document_Click(object sender, HtmlElementEventArgs e)
{
tempHtmlElement = webBrowser1.Document.ActiveElement;
...
method couldn't reach to the end. In 90% record happens but some time it passes webBrowser1_Navigating event after that as I said passes WebBrowser1Document_Click just the beginning and webBrowser1.Document.ActiveElement breaks. It doesn't matter I have variable for the ActiveElement this variable looses access to some properties as Name for example. So my question is how to pause webbrowser1 activity until record is made. It's a STA application.
Assuming that Navigation occurs before Click method completes, You can pause by a tricky method. Use a global variable CanNavigate.
bool canNavigate;
public void WebBrowser1Document_Click(object sender, HtmlElementEventArgs e)
{
canNavigate=false;
tempHtmlElement = webBrowser1.Document.ActiveElement;
...
...
//At the End
canNavigate=true;
}
void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
do
{
Application.DoEvents(); Thread.Sleep(100);
} while (!canNavigate);
}