Is there a way to prevent users in clicking and typing in WPF WebBrowser? It appears to me that it is only possible to do this in WinForms.
Things that I've tried:
browser.IsEnabled = false; - didn't work, can still click (navigate) and type in text
browser.Focusable = false; - same
having overlay button, which would consume clicks and focus - WebBrowser is a special element, that is always on top of other elements
having another WebBrowser on top of the main one with blank page loaded and opacity set to 0% as an alternative to overlay button (3.) - WPF WebBrowsers do not properly handle opacity, didn't work
browser_MouseDown event with e.Handled = true; - the event is for some reason not called on mouse down
Is there something that I've missed or did wrong in my attempts?
Three events and Boolean did it for me.
bool BrowserIsLoaded = false;
private void Browser_LoadCompleted(object sender, NavigationEventArgs e)
{
BrowserIsLoaded = true;
}
private void Browser_Navigating(object sender, NavigatingCancelEventArgs e)
{
if(BrowserIsLoaded)
e.Cancel = true;
}
private void Browser_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (BrowserIsLoaded)
e.Handled = true;
}
When the browser has finished loading it triggers the LoadCompleted event. Set a Boolean then check that when trying to navigate to a new page when they try to type in a box.
If you don't want to use your own Boolean (I used it for other things so it made sense to me) you can just ask the browser if it's loaded when ever you need it:
private void Browser_Navigating(object sender, NavigatingCancelEventArgs e)
{
WebBrowser wb = (WebBrowser)sender;
if(wb.IsLoaded)
{
e.Cancel = true;
}
}
Related
Basically I want a textbox to open browser but it doesn't work:
private void TextBox1_Click(object sender, EventArgs e)
{
webBrowser1.Visible = true;
button3.Visible = true;
textBox1.ForeColor = Color.Purple;
}
Like the comments are hinting, it's not really clear what you're asking.
WebBrowser is a control element on WinForms which you can make visible, as you're doing correctly.
If however you're trying to open an actual browser, you should open it as so:
System.Diagnostics.Process.Start("IExplore.exe");
I have problem with richBox1 text disabling.
I've tryed richTextBox1.readonly = true; and richTextBox1.Enabled = false;
My code:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.ReadOnly = !richTextBox1.ReadOnly;
}
Its disabling after one letter.
EDIT: And if disable I can still copy text but cant write there.
Honestly, disabling expected functionality is not something you should be doing. It is not good UI design.
The event TextChanged is fired every time the text changes (including writing or removing one letter). You can use Form's Load event (by double clicking the form on design time) :
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.ReadOnly = true;
richTextBox1.Enabled = false;
}
I'm writing a simple application with several controls on a Windows form. I need to monitor the state of buttons (enabled/disabled) according to the state of a textbox and a listbox.
For example, when the listbox is empty, buttons Delete, Delete All and Edit are to be disabled, or when either the textbox or the listbox is empty button Forward is disabled, and so on.
So, I put the change of these properties on Application.Idle event, so it goes something like this:
private void MainForm_Load(object sender, EventArgs e)
{
Application.Idle += new EventHandler(Application_Idle);
}
public void Application_Idle(object sender, EventArgs e)
{
CheckFillingFields(forwardBtn);
CheckFillingList(deleteBtn);
CheckFillingList(deleteAllBtn);
CheckFillingList(editBtn);
}
private void CheckFillingFields(object sender)
{
if (questionTxt.Text == "" || answersLst.Items.Count == 0)
(sender as Button).Enabled = false;
else
(sender as Button).Enabled = true;
}
private void CheckFillingList(object sender)
{
if (answersLst.Items.Count == 0)
(sender as Button).Enabled = false;
else
(sender as Button).Enabled = true;
}
So, the question is - is it acceptable to use Application.Idle in this case? Or should I make these properties dependable on user actions? (For example, when the user deletes an item from the listbox, I should check if it was the last one, and disable the corresponding buttons.)
Thanks a lot in advance, I really appreciate your help!
The simple answer is that, yes, the idle checking is bad and you should re-check the state of your controls on their change events, not "whenever possible".
Is it possible to disable the option to get into "Rename" mode when clicking on a tree-node?
I don't want to disable renaming completely, only to not allow doing it by clicking on the node.
I don't know why would you change the default behavior, but anyway here's a possible solution to edit the nodes with LabelEdit set to true.
Just catch BeforeLabelEdit event and cancel it, unless your specific action occurred. The following code does this for F2 key press:
bool _allowNodeRenaming;
private void treeView1_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
{
if (!_allowNodeRenaming)
{
e.CancelEdit = true;
}
_allowNodeRenaming = false;
}
private void treeView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F2)
{
_allowNodeRenaming = true;
treeView1.SelectedNode.BeginEdit();
}
}
You'll have to turn the LabelEdit property on and off as needed:
private void startLabelEdit() {
treeView1.LabelEdit = true;
treeView1.SelectedNode.BeginEdit();
}
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) {
treeView1.LabelEdit = false;
}
Beware that this has side effects, the LabelEdit property is a style flag for the native Windows control. Changing it requires completely destroying the window and re-creating it from scratch. The most visible side-effect is a small flicker when the window redraws itself after getting created. There could be other ones, I didn't see anything go wrong myself.
I have a tab control and 3 tabpages in it. ( C#)
if i am in tab 2, and edit a text box value
and then click tab 3, i need to validate what was enetered in the text box.
if correct i should allow to to switch to tab 3 else should remain in tab 2 it self
how do i achieve this?
iam curently handling the "leave" event of the tabpage2,
i validate the text box value there and if found invalid
i set as tabcontrol.Selectedtab = tabpage2; this does
the validation but switches to new tab! how could i restrict the navigation.
I am a novice to C#, so may be i am handling a wrong event!
Here is the relevant code:
private void tabpage2_Leave(object sender, EventArgs e)
{
if (Validatetabpage2() == -1)
{
this.tabcontrol.SelectedTab =this.tabpage2;
}
}
While the other approaches may work, the Validating event is designed specifically for this.
Here's how it works. When the SelectedIndex of the tab control changes, set the focus to the newly selected page and as well as CausesValidation = true. This ensures the Validating event will called if the user tries to leave the tab in any way.
Then do your normal validation in a page specific Validating event and cancel if required.
You need to make sure to set the initial selected tab page in the Form Shown event (Form_Load will not work) and also wire up the tab page specific validating events.
Here's an example:
private void Form_Shown(object sender, System.EventArgs e)
{
// Focus on the first tab page
tabControl1.TabPages[0].Focus();
tabControl1.TabPages[0].CausesValidation = true;
tabControl1.TabPages[0].Validating += new CancelEventHandler(Page1_Validating);
tabControl1.TabPages[1].Validating += new CancelEventHandler(Page2_Validating);
}
void Page1_Validating(object sender, CancelEventArgs e)
{
if (textBox1.Text == "")
{
e.Cancel = true;
}
}
void Page2_Validating(object sender, CancelEventArgs e)
{
if (checkBox1.Checked == false)
{
e.Cancel = true;
}
}
private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
{
// Whenever the current tab page changes
tabControl1.TabPages[tabControl1.SelectedIndex].Focus();
tabControl1.TabPages[tabControl1.SelectedIndex].CausesValidation = true;
}
You can use the TabControl Selecting event to cancel switching pages. Setting e.Cancel to true in the event stops the tabcontrol from selecting a different tab.
private bool _cancelLeaving = false;
private void tabpage2_Leave(object sender, EventArgs e)
{
_cancelLeaving = Validatetabpage2() == -1;
}
private void tabcontrol_Selecting(object sender, TabControlCancelEventArgs e)
{
e.Cancel = _cancelLeaving;
_cancelLeaving = false;
}