I would like to select all when a user right clicks on my web browser control.
I am developing a win forms app, and use web browsers to display my information, because i can use html to style the words.
The right click context menu is not working for me. The options on it are all irrelevant to my app.
But the context menu after a select has been made i want to keep, the copy, cut, paste options.
I can already select all:
getCurrentBrowser().Document.ExecCommand("SelectAll", true, null);
I would just like to do it in the right click event of the web browser?
Handle MouseDown event:
webBrowser.Document.MouseDown += new HtmlElementEventHandler(Document_MouseDown);
and make sure user pressed Right button, then select all:
void Document_MouseDown(object sender, HtmlElementEventArgs e)
{
if(e.MouseButtonsPressed == MouseButtons.Right)
{
webBrowser.Document.ExecCommand("SelectAll", true, null);
}
}
This article shows how you can replace the context menu of the Web Browser with your own.
Alternatively, if you execute the following Javascript from within the Web Browser, it will disable the default right-click context menu:
document.oncontextmenu=new Function("return false")
If you are using WinForms rather than WPF, you can set the IsWebBrowserContextMenuEnabled to false to prevent the IE context menu, in which case it will use the ContextMenu you supply on the WebBrowser control.
WPF doesn't have the same property exposed on the Web Browser, so it may not be so easy. In this case you may have to use WindowsFormsHost to host the WinForms Web Browser in WPF.
This works :)
When the context menu shows select all is running pushing out the contextmenu i want, with the copy, paste, cut and so on.
private void webCompareSQL_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webCompareSQL.Document != null)
{
webCompareSQL.Document.ContextMenuShowing += DocMouseClick;
}
}
void DocMouseClick(object sender, HtmlElementEventArgs e)
{
webCompareSQL.Document.ExecCommand("SelectAll", true, null);
}
You need to be sure, that the WebBrowser.Document property is already loaded. Then you can register the Mouse event.
`browser.DocumentCompleted += (s, e) => {
browser.Document.MouseMove += (sM, eM) +=> {
Debug.WriteLine(eM.ClientMousePosition.X);
};
};
`
Related
I am using a tab in my program to switch between two forms. I put the code required to switch between forms within the tabPage1_Click event, but it doesn't trigger when the tab is clicked.
I attached the code and properties of the tab. Please let me know if any other information is required to know the problem. Thanks.
private void tabPage1_Click(object sender, EventArgs e)
{
this.Hide();
Home form2 = new Home();
form2.ShowDialog();
this.Close();
}
There are 2 things involved here. Tab control and Tab pages. Tab Control is the parent object which has multiple Tab pages in it.
You have event handler for Tab Page which is tabpage1_Click and not for Tab Control.
tabpage1_Click will be triggered when you click on tab page 1(not on the tab page header).
If you need to capture an event when you click on tab page header use Tab Control click event, something like below.
private void tabControl1_Click(object sender, EventArgs e)
{
//Your code goes here
}
To access the properties of the tab page use tabControl1.SelectedTab
I am using following code in C# to add a Button
Button TextLabel = new Button(); //local variable
TextLabel.Location = new Point(0, 0);
TextLabel.Visible = true;
TextLabel.Enabled = true;
TextLabel.AutoSize = true;
TextLabel.Click += click;
this.Controls.Add(TextLabel);
And its click handler is
protected void click(object o, EventArgs e)
{
MessageBox.Show("hello");
}
Though the Button is visible and responding to mouse hover, but nothing is happening on its click. What could be wrong or missing?
If I write this same code in an independent project, it works!!!!! Strange. but why????
Form Properties: (if required)
1. Show in taskbar: false
2. Borderless
3. 50% Opaque
Today I realised that just registering click event for a control will not make any event to work unless its parent (in my case its form) on which that control is still active.
Parent control will receive event notification earlier than its child controls. This is a simple and obvious observation, but if not paid attention will make undesirable effects.
That's the mistake I did, I made another form active on my form activated event, hence any control in it didn't received events like mouse clicks.
Talking of 'hover effects are working', then yes, even if a form is inactive, hover works.
So I just removed the line of code that made another form active and everything is working fine now.
private void Form1_Activated(object sender, EventArgs e)
{
//if (form2!=null) form2.BringToFront(); //commented this
}
I want to trigger Windows hotkeys using my c# application. For example, if I selected copy button in my Application, i want to trigger the Ctrl - C hotkey. If I selected the run button in my application, I want to trigger the Win - R hotkey.
How can I do that?
Thank you.
You can use SendKeys.Send method
For example in your button click event, to trigger CTRL + C combination you can use this
SendKeys.Send("^c") // CTRL + C
Note: By the way I wouldn't suggest you to do it in button click event.Probably you are trying to copy some text from your textbox.But when you click your button textbox is losing it's focus and selected text is disappear.So key is sending correctly but you can't copy anything.
"If I selected the run button in my application, I want to trigger the Win - R hotkey."
Run Dialog
In JScript, it's a lot simpler do display the Run dialog box. It is still possible in C#, though. You need a reference to Shell32:
Then add using Shell32; in your code-behind.
In your button's click event, you can do this:
private void runBtn_Click(object sender, EventArgs e)
{
Shell shell = new Shell();
IShellDispatch sd = (IShellDispatch)shell;
sd.FileRun();
}
And you should see somethin' like this:
Simulating Ctrl-C
"...if I selected copy button in my Application, i want to trigger the Ctrl - C hotkey."
Selman22 mentioned the textbox will lose focus if you click another button. Here's the way around it:
private void copyBtn_Click(object sender, EventArgs e)
{
textBox1.Focus(); // <---- here
SendKeys.Send("^c");
}
So I have a WPF application that opens up a new window. Then on that new window, it creates a webBrowser object:
WebBrowser browser = new WebBrowser();
browser.Source = new System.Uri(chatUrl);
browser.Navigating += new NavigatingCancelEventHandler(browser_Navigating);
this.browserControl.Child = browser;
As you can see, I have created a hook for NavigatingCancelEventHandler. Based on what I've seen, this handler is supposed to intercept links clicked within the webbrowser.
private void browser_Navigating(object sender, NavigatingCancelEventArgs e)
{
e.Cancel = true;
System.Diagnostics.Process.Start(e.Uri.ToString());
}
So I take the navigation, cancel it and use the Process.Start function to open it in my default browser. The problem is, it doesn't do this. It still opens up in IE9. I've seen other threads here on StackOverflow, and they all say to do what I'm doing. But what I'm doing doesn't work. Please help.
You're using the incorrect delegate type for your event handler, you're specifying a NavigatingCancelEventArgs but it should be a WebBrowserNavigatingEventHandler.
Easiest way to get the right one is to just type
browser.Navigating +=
And press TAB twice, it'll generate the correct handler for you.
Edit: Hmm, I'm looking at the S.W.F version, but i'd try the above in either case for WPF.
In my Windows Phone Application when I press a button for navigating to Page1 and before it finishes loading it, I press button to navigate to Page2, my application crashes. Is it correct and what is the best practice to implement this behaviour?
Update
Generally I think, the best way for me will be to block all buttons on the screen after one of them was pressed and make them active after navigation finish, so how can I do this?
The best practice is this case is to disable the Click event after the first click.
If your button was named b and you either assigned event handler in xaml or in codebehind
like
b.Click += new RoutedEventHandler(ButtonOnClick);
void ButtonOnClick(object sender, RoutedEventArgs e)
{
b.Click -= new RoutedEventHandler(ButtonOnClick);
// Navigate away now.
}
Actually my sample will stop double click on current button only but you can disable other buttons as well.
in one of my apps, i maintain a List which i iterate and show / hide, enable / disable a required. all you do is iterate and set the correct property.
foreach(var button in myButtons)
button.IsEnabled = false;
Wrap the call to NavigationService.Navigate(Page2Uri) into Dispatcher.BeginInvoke(()=> {...} ); as follows:
Dispatcher.BeginInvoke (()=> { NavigationService.Navigate(Page2Uri) });