Making a WebBrowser control in wpf read-only? - c#

I would like to make the wpf webbroswer control read-only in the sense that I still want to be able to give users the ability to copy texts off of textfield etc but I dont want them to be ableto click button or submit forms.
I wrapped the web browser control in a wpf user control. When I set the IsEnabled proerty to false, it works i.e the webbroswer control is disabled but the user cannot select text from the page...

You can handle the WebBrowser's Navigating event and cancel it so that the user can't navigate to another page.
void myBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
{
e.Cancel = true;
}

Related

How can I detect when the user clicks on a hyperlink in a WebView control?

I'm trying to detect when the user clicks on a hyperlink in a WebView control from Windows 10. Is there any way to detect this event?
If you want to catch the click itself you'll need to do that from inside the web page's JavaScript. Depending on where the page comes from you may be able to inject JavaScript code to do so with WebView.InvokeScriptAsync
If you want to detect the navigation triggered by the click then you can handle the WebView.NavigationStarting or NavigationCompleted events.
On Windows 10 I would use the WebView.NewWindowRequested event:
private void WebView1_NewWindowRequested(WebView sender, WebViewNewWindowRequestedEventArgs args)
{
Debug.WriteLine(args.Uri);
args.Handled = true; // Prevent the browser from being launched.
}

Disable back navigation in a WPF WebBrowser control

In my WPF application I have a WebBrowser control. I have a grid containing files, and when I click on an item in the grid the file contents are retrieved from a database to be shown in the WebBrowser.
There are two types of file:
PDF: a temporary file is created, and the web broswer Navigate function is used to load the file.
HTML: The string is passed to the NavigateToString function.
If I view a PDF, then a HTML document, right clicking shows the context menu. I want to keep most things, such as print, but I want to stop the browser from letting the user go back a page, or even forwards.
Without editing the content to add Javascript etc, is there anything on the control I can do to stop the back/forward from happening?
The answer I have come up with is the Navigated event. The user control that contains the web browser has a private boolean that determines if navigation is allowed.
If it is (set through the user control's Navigate method) then the control can navigate to a new page. Once the page has loaded the boolean is set to false, meaning back / forward is disabled.
In my case this adds a bonus: Links cannot be clicked on. I don't want these loading in the browser control - I only want this to view pages selected in the grid.
However, right-click on a link still has the open in a new window option.
This may not be the best solution, but it works for me.
To disable the backspace for navigating back but still keep the link clickable and backspace function for textbox, we need to add event handler for both PreviewKeyDown and Navigating event for WebBrowser:
// member variable to indicate if the window allows navigation to other URL
private bool allowNavigation = false;
private WebBrowser bs;
// triggered every time a key is pressed down in the WebBrowser
this.bs.PreviewKeyDown += (sender, args) =>
{
if (args.Key == Key.Back)
{
// temporarily disable navigation triggered by backspace
this.allowNavigation = false;
}
};
// triggered if the WebBrowser is switching URL (either by backspace or link clicking)
this.bs.Navigating += (sender, args) =>
{
if (!allowNavigate)
{
// if not allowed, cancel navigation and set back allowNavigation
// this will only cancel the navigation triggered by backspace
args.Cancel = true;
this.allowNavigation = true;
}
};

How to refresh user control grid by using another user control event

I am developing windows application using C#. Need solution for below mentioned scenario. I have windows form containing two user control. First user control contains grid and when user click on any row of grid another user control display details of selected cell. when I modify any data from details control I need to refresh data grid in parent control.
I am using below code to load child control.
private void GridInquiry_CellClick(object sender, DataGridViewCellEventArgs e)
{
PanelParentControl.Controls.Clear();
InquiryDetailsCls.InquiryID = Convert.ToInt32(GridInquiry.SelectedRows[0].Cells[0].Value.ToString());
CtrlInqDetails inqDetails = new CtrlInqDetails(InquiryDetailsCls.InquiryID, 1);
inqDetails.Dock = DockStyle.Fill;
PanelParentControl.Controls.Add(inqDetails);
}
I can recommend to you to use the Notification Center for C#,With this you can make a global event, register for this event anywhere in you code and to fire this event from anywhere in your code.

Suppress Automatic Textbox Selection on Windows Forms Form.Show()

I have an application where I am trying to mimic the "soft description" textboxes like those found for the tags and title locations on this site.
The way I've done this is essentially to create my textbox, and depending on what happens when the mouse pointer enters or leaves the control, it updates the content of the textbox to get the effect.
The problem is what when my form is first shown, the mouse cursor immediately jumps into the first textbox, which removes the title telling the user what the textbox is for.
If I turn off AcceptTab on the textbox, then everything works as expected, but the user loses the ability to tab into the textbox.
Is there a way to turn off this automatic selection of the textbox?
Could you this.Focus() on the form itself, or on some label control?
Bit late but a perfect solution is to select the form on load of form.
Adding this line to the constructor will give the expecting result.
this.Select();
But while using multi thread controls like OpenFileDialog if u want to unfocus/deselect text-box this.Select() was not working so I selected a button in the form using.
button1.Select();
The TabIndex property controls what order things will tab in, and on load, focus goes to the first control (ordered by TabIndex) that has AcceptTab as true. You can change the ordering so that the control that you want the user focus to start in is lowest (and have tabs work cycle through controls as you'd expect).
Alternatively, as Jason suggested, you could simply call Focus() on whatever control or the form itself in the FormLoad event.
I used a variant on Jason's technique. First, I created a dummy textbox with tabindex 0. That way, when the form is shown, that textbox will be selected. Next, I made the dummy textbox have zero width, so that it has no visible component.
However, once the form is loaded, I don't want the user to be able to tab over to the "nonexistant" textbox. Therefore, I added these two bits:
//These functions prevent the textboxes from being implicitly selected.
private void dummyBox_Leave(object sender, EventArgs e)
{
dummyBox.TabStop = false;
}
private void Main_Enter(object sender, EventArgs e)
{
dummyBox.TabStop = true;
dummyBox.Select();
}
Where Main is the name of my form.
Hope this helps someone.
Billy3

User Control Click - Windows Forms

I have a custom user control on my windows forms. This control has a few labels on it.
I will be dynamically displaying an array of these controls on my form which will contain different bits of data.
What I am trying to do is know which user control was selected when I click on it.
This works when I click on an empty space on the user control, however, if I click on any label on the user control it will not recognize the user control click.
Any thoughts on how I can do a full user control click, even if a label on the control is being clicked?
If this question is not clear, or you need more info, please leave a comment.
I am doing this in c#.
Thanks!
User control's click event won't fire when another control is clicked on the user control. You need to manually bind each element's click event. You can do this with a simple loop on the user control's codebehind:
foreach (Control control in Controls)
{
// I am assuming MyUserControl_Click handles the click event of the user control.
control.Click += MyUserControl_Click;
}
After this piece of code workd, MyUserControl_Click will fire when any control on the user control is clicked.
foreach (Control c in this.Controls)
{
c.Click += new EventHandler(SameAsForm_Click);
}
Keep in mind that this won't add labels' clickevents in groupboxes, panels etc to the "SameAsForm_Click"-EventHandler.

Categories