Preventing navigation to ChildWindow in Silverlight - c#

When we try to navigate to ChildWIndow using AddressString of the Browser the method
private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
does not executing.
And instead of the error happens here
private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
e.Handled = true;
// ErrorWindow.CreateNew(e.Exception);
}
How to prevent to navigate to the ChildWindow?
Thank you!

Related

Raise an Event Except when something happpen

Scenario: Only If the user follow the path Click on ListView > Click on Button the Button1 do something.
In other word I want to check in Button1_Click(object sender, EventArgs e) if the previous focus was on ListView.
So I tried this:
private void ListView_Test_Leave(object sender, EventArgs e)
{
_focusedControl = null;
}
I want raise previous event except when this event is raised:
private void Button1_Click(object sender, EventArgs e)
{
if(_focusedControl == listView_Test)
{
// ...
}
}
Edit: I have a variable that holds a reference to the currently focused control:
private Control _focusedControl;
and I update it in this way:
private void ListView_Test_GotFocus(object sender, EventArgs e)
{
_focusedControl = (Control)sender;
}
If the user follow the path Click on ListView > Click on Button I want raise only the Button1_Click event, in all other case I want normal raise.
You could use a helper variable.
bool wasRaised=false;
private void Button1_Click(object sender, EventArgs e) { wasRaised=true;}
Then you can check that variable in your event, and only run if it is false.

Error in Webbrowser when I waiting for ajax changes

I have a project that reads content from web and saves it to the database.
I use Timer control to wait for ajax changes and it has been working for a long time without any problem.
But unexpectedly it is not working any more. I get the following error :
Exception thrown: 'System.Runtime.InteropServices.COMException' in
SnapshotInfo.exe
Additional information: Error HRESULT E_FAIL has been returned from a
call to a COM component.
Part of the code on this problem :
WebBrowser _browser = new WebBrowser();
private void Form1_Load(object sender, EventArgs e)
{
_browser.Dock = DockStyle.Fill;
_browser.DocumentCompleted += _browser_DocumentCompleted;
Controls.Add(_browser);
}
private void _browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (!timer1.Enabled)
{
// do any thing
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
_browser.Dispose();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (_browser.Document==null || _browser.Document.Body==null) return;
var body = _browser.Document.Body.InnerHtml; // ----> error here
if (body.Contains("word"))
{
// code when word is exist
timer1.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
_browser.Navigate(textBox1.Text);
timer1.Enabled = true;
}
I do not know where is the problem?
Thanks for any help
Make sure you are using latest version of web browser control , check below link
Use latest version of Internet Explorer in the webbrowser control
or you can replace web browser control with another web browser engine like http://www.awesomium.com/ it's free and easier to use
The problem solved by deleting the security update KB3087040!

Wait until page loads in webbrowser control

I'm using the C# WebBrowswer control and I have a problem that when a button like "Next" is pressed when the page is not loaded yet the program tries to proceed but instead gives me a null error.
Is there is a function to make the program wait until the page has completed loading?
I tried to put a while loop in the program that checks the title of the html page but then the program freezes. Something like will freeze the program:
while(!webbrowser1.Document.Title.ToString().Equals("NextPageTitle"))
{
}
::NextCommands::
It Doesnt work, i tried that and the button "fblqf" is not clicked. but its not returns null error..
public void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("q").SetAttribute("value", "חחח");
webBrowser1.Document.GetElementById("btnK").InvokeMember("Click");
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// do the work you need to do now that that page has completed loading
webBrowser1.Document.GetElementById("fblqf").InvokeMember("Click");
}
Solution:
public void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("q").SetAttribute("value", "חחח");
webBrowser1.Document.GetElementById("btnK").InvokeMember("Click");
int x=0;
while (x==0)
{
System.Windows.Forms.Application.DoEvents();
if(webBrowser1.Document.GetElementById("pnnext") != null)
break;
}
webBrowser1.Document.GetElementById("pnnext").InvokeMember("Click");
webBrowser1.Document.GetElementById("q").Focus();
}
You need to hook the WebBrowswer.DocumentCompleted event:
Perhaps in your constructor or your OnLoad:
webBrowser1.Document.GetElementById("q").SetAttribute("value", "חחח");
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
webBrowser1.Document.GetElementById("btnK").InvokeMember("Click");
Then you event looks like this:
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// do the work you need to do now that that page has completed loading
}
I Found an easy Solution!!!
public void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("q").SetAttribute("value", "חחח");
webBrowser1.Document.GetElementById("btnK").InvokeMember("Click");
while (true)
{
System.Windows.Forms.Application.DoEvents();
if(webBrowser1.Document.GetElementById("pnnext") != null)
break;
}
webBrowser1.Document.GetElementById("pnnext").InvokeMember("Click");
webBrowser1.Document.GetElementById("q").Focus();
}

How can I use Enter to call event handler?

I have a textBox and search button, I would ask about how can I make the user can click Enter to start searching without need to go and click the search button?
This would be best practice
private void txtSearch_Enter(object sender, EventArgs e)
{
AcceptButton = btnSearch;
}
private void txtSearch_Leave(object sender, EventArgs e)
{
AcceptButton = null;
}
The Form has a property called "AcceptButton" that identifies a button that should be associated to the "Enter" keypress. Its considered the "default action" for the form.
More info here:
Windows Form - AcceptButton property
If you want to use something other than Enter/Return, you could also try:
private void EnterKeyAction()
{
// Search...
}
private void btnEnter_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
EnterKeyAction();
}
private void btnEnter_Click(object sender, EventArgs e)
{
EnterKeyAction();
}

SplitterLocation doesn't save in the settings

i have created a splitterLocation setting, Type system.Drawing.Point and Scope : User. to save the splitter location when the user change it. and load again with the new location when the form load.
private void splitter1_LocationChanged(object sender, EventArgs e)
{
MailSystem.Properties.Settings.Default.splitterLocation = splitter1.Location;
MailSystem.Properties.Settings.Default.Save();
}
private void Form1_Load(object sender, EventArgs e)
{
splitter1.Location = MailSystem.Properties.Settings.Default.splitterLocation;
}
but it doesnt work i dont know why ?.
Try moving the saving code to the FormClosing event.
private void Form1_FormClosing(object sender, EventArgs e)
{
MailSystem.Properties.Settings.Default.splitterLocation = splitter1.Location;
MailSystem.Properties.Settings.Default.Save();
}
and make sure your splitter1 control isn't being docked, DockStyle.None.

Categories