Focusing WebBrowser control in a C# application - c#

I have a WebBrowser control hosted in a windows Form. The control is used to display hyperlinks which get created at runtime. These links point to some HTML pages and PDF documents.
The problem is that when the form hosting the browser control is loaded, the focus is on the form. When the TAB key is pressed, the focus does not shift to the first hyperlink. However, if I perform a mouse click on the control and then hit the TAB key, the tab focus is now on the first hyper link. I tried using Select() on the WebBrowser control and then I called Focus(), but it doesn't solve the problem.
Any ideas on how to set the tab focus on the first hyperlink at load? Thanks.
Cheers,
Harish

I guess it might be because the focus is set before the page is fully loaded. Try this:
private void Go(string url)
{
webBrowser1.Navigate(url);
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.Body.Focus();
}
You could also automatically select the focus on the first link directly by getting the HtmlElement of that first link.
If the above doesn't work, you might want to check other parts of your code to see if anything else is capturing the focus. Try searching for Select, Focus and ActiveControl in your code.

Use form.ShowDialog(form) instead on form.Show(), then it will work !
where form is the running instance of your windows Form

This is my solution
private void txtAdres_KeyPress(object sender, KeyPressEventArgs e)
{
int licznik = 1;
if (e.KeyChar == (char)13)
{
string adres = txtAdres.Text;
webBrowser1.Navigate(adres);
licznik = 0;
}
if (licznik == 0)
{
webBrowser1.Focus();
}
}

In a normal scenario it should be sufficient for you to set the TabIndex of the WebBrowser control to zero. This way, when the form loads the control will be focused and pressing TAB will iterate through the links.
Note that you should also change the TabIndex of the other controls on the form.
If this does not solve your problem, you need to add more detail on the complexity of the form hosting the control.

Related

How can I disable (grey out) specific tabpage(s) of a tab control programatically in my C# form app?

I can't believe that disabling specific tabpages (visible but greyed out that means not clickable) is not possible in Visual Studio C# Form app. I have found just a workaround that I couldn't bring to work.(See link below)
I wanted to ask this question again, because perhaps there is another solution/workaround in the meantime.
How to highlight/grey-out Tabcontrol Tabpage
For just making the tab nonclickable (what is also OK for me) I have used this code, but not working
private void tabControl1_Selecting(object sender,TabControlCancelEventArgs e)
{
if (!e.TabPage.Enabled)
{
e.Cancel = true;
}
}
For example if you want to disable tabPage2 use this code in form load event. It will not show in intellisense but you can type.
tabPage2.Enabled = false;
Then use tab control selecting event like this
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
if (e.TabPageIndex < 0) return;
e.Cancel = !e.TabPage.Enabled;
}

How to trigger event when clicking on a selected tab page header of a tab control

I am doing a Winform application in C# and I have some tab pages, say tabPage1, tabPage2 and tabPage3, in a tab control, and tabPage1 is selected.
I want to trigger event when any tab page header is clicked, but I could do it only for page change (by using SelectedIndexChanged) but not click on a selected tab page header.
I tried with Selecting and Selected events but both of them didn't not work. I searched on MSDN but didn't find any Click event defined on a page header. So how should I achieve this?
One further question, is it possible, and how, to detect DoubleClick on a selected tab page?
Just use the tabcontrol's MouseDoubleClick event. You'll have to iterate the tabs to find out what specific tab was clicked:
private void tabControl1_MouseDoubleClick(object sender, MouseEventArgs e) {
for (int ix = 0; ix < tabControl1.TabCount; ++ix) {
if (tabControl1.GetTabRect(ix).Contains(e.Location)) {
// Found it, do something
//...
break;
}
}
}
Do keep in mind that this is completely undiscoverable to the user, he'll never think to double-click the tab. You'll have to write a manual.
You should be doing your stuff on
TabIndexChanged
If you want to load the contents on your 3rd tab, for example, handle the TabIndexChanged on your tab control, do a switch for each tabPage.Index and then do whatever is needed when user clicks on that tab.
private void tabPage1_Layout(object sender, LayoutEventArgs e)
{
//do something
}

how can closed the TAB page by click?

I am making a Web Browser , I have done some parts like that new tab Page,Bookmark,home page,Default Search Engine and so on. I'm confused about how to closed the TAB PAGE. I have tried Double click , Mouse down, up and many more but I can't solve the problem.
I have create the TABPAGE like that . Thanks in advance waiting reply.....
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab.Text == "+")
{
AddNewTab();
}
foreach (Control item in tabControl1.SelectedTab.Controls)
{
if (item.GetType() == typeof(WebBrowser))
{
WebBrowser wb = (WebBrowser)item;
toolStripButton1.Enabled = wb.CanGoBack;
toolStripButton2.Enabled = wb.CanGoForward;
}
}
Snapshot my Window form application
Just try disposing of the TabPage. Assuming a button called "Close Tab" on the form:
private void closeTab_Click(object sender, EventArgs e) {
if (tabControl1.SelectedTab != null) {
tabControl1.SelectedTab.Dispose();
}
}
You will need Javascript to do this. Use window.close():
close();
Note: the current window is implied. This is equivalent:
window.close();
or you can specify a different window.
So:
function close_window() {
if (confirm("Close Window?")) {
close();
}
}
with HTML:
close
or:
close
You return false here to prevent the default behavior for the event. Otherwise the browser will attempt to go to that URL (which it obviously isn't).
Now the options on the window.confirm() dialog box will be OK and Cancel (not Yes and No). If you really want Yes and No you'll need to create some kind of modal Javascript dialog box.
Note: there is browser-specific differences with the above. If you opened the window with Javascript (via window.open()) then you are allowed to close the window with javascript. Firefox disallows you from closing other windows. I believe IE will ask the user for confirmation. Other browsers may vary.
If you have more than one tab, changing tabs should fire the code. Does your markup have runat="server" in the tag declaration?

How do I specify the default button of a TextBox that is referred to by AcceptsReturn?

I have a windows form with a tool strip on it containing a text box and some buttons (think browser url bar with a go button, back, forward)
I want pressing enter to activate the goButton just as clicking it would, which I believe is what TextBox.AcceptsReturn = false is for.
I don't see anything that seems to fit the bill for "tell me what button on the form is the one that we will activate".
What am I missing?
A Form has a default button, but a specific control does not (out of the box anyway).
In your scenario, I would probably handle invoking the goButton.Click event by monitoring the keys pressed waiting for the Enter key to be pressed.
The easiest way is to set the forms "Accept Button" to the button control you want. This can be done in the designer.
I know this is an Old Question, but for someone who might to to lazy or just a beginner ,
handler might look like too much work ( though it isn't really )
But there is an easier work around for this,
you can make a panel for each of them ( 1 Textbox and 1 Button for Example ) , and set the Defaultbutton for Each panel as you need.
I used this for my site, where I had several Ajax panel , and I Wanted Each to have their own search box on different subjects and work with Enter Button.
Looks like an old question, but I will provide my solution.
private void ChangeDefaultButton()
{
if (this.TextBox.Focused)
{
this.AcceptButton = button;
}
else
{
this.AcceptButton = button1;
}
}
And then add this method to the Focus Events of the text boxes. Like...
private void TextBox_Enter(object sender, EventArgs e)
{
ChangeDefaultButton();
}
And
private void TextBox_Leave(object sender, EventArgs e)
{
ChangeDefaultButton();
}

Scrolling problem with a WebBrowser control contained in a Panel control

I have a .Net Panel control that contains a single child control that is a WebBrowser. I won't go into the reasons for me doing that, but it is related to printing out the control. The panel control has its AutoScroll property set to "true" and I am sizing the WebBrowser to fit its own content (by using the .Document.Body.ScrollRectangle.Size property of the WebBrowser when the NavigateComplete2 event fires). In this way, the scrollbar on the panel appears and you can scroll the panel up and down in order to be able to see the content of the WebBrowser.
The problem is that when you scroll down to see what's at the bottom of the WebBrowser and then click on it (perhaps you click on a link in the html), the panel jumps back to the top and the link doesn't get actioned.
Please can anyone help me to understand what's going on and how to get around this problem?
I had the same problem, also with a WebBrowser inside a Panel. Here's the solution I'm using (which I found somewhere else on stackoverflow):
class AutoScrollPanel : Panel
{
public AutoScrollPanel()
{
Enter += PanelNoScrollOnFocus_Enter;
Leave += PanelNoScrollOnFocus_Leave;
}
private System.Drawing.Point scrollLocation;
void PanelNoScrollOnFocus_Enter(object sender, System.EventArgs e)
{
// Set the scroll location back when the control regains focus.
HorizontalScroll.Value = scrollLocation.X;
VerticalScroll.Value = scrollLocation.Y;
}
void PanelNoScrollOnFocus_Leave(object sender, System.EventArgs e)
{
// Remember the scroll location when the control loses focus.
scrollLocation.X = HorizontalScroll.Value;
scrollLocation.Y = VerticalScroll.Value;
}
protected override System.Drawing.Point ScrollToControl(Control activeControl)
{
// When the user clicks on the webbrowser, .NET tries to scroll to
// the control. Since it's the only control in the panel it will
// scroll up. This little hack prevents that.
return DisplayRectangle.Location;
}
}
try setting TabStop to false on both the containing panel as well as the WebBrowser control. That did the trick for me. The reason this works is that if it's set as a wanting to be a tabstop, it will take the first click event to mean that it's receiving focus. This then resets the scroll bar positions... not sure why it does that...
However, on navigating to a new page you'll need to manually reset the scroll bar positions...
Here is what I used. Yeah it's a hack.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
webBrowser1.TabStop = true;
webBrowser1.Focus();
webBrowser1.TabStop = false;
}

Categories

Resources