WPF .GetElementById() - c#

Im programming in WPF (C#) in VS2012
I tried to use this:
How do you click a button in a webbrowser control?
To click button on webbrowser but
.GetElementById gives me an error.
I add: using System.Windows.Forms; and assembly them, but it don't change anything for me.
I think im gonna have a problem with this as well: .InvokeMember("click");
All I've found on the web is that:
http://www.telerik.com/help/wpf/m_telerik_windows_documents_formatproviders_html_parsing_dom_idocument_getelementbyid.html
But I don't know exactly how i can assembly that to VS2012, bcus there are no build in API references in VS2012.

You can use Windows Forms WebBrowser instead of WPF WebBrowser host in WindowsFormHosted control in the WPF app and then you can access those members.

// reference :
/// https://gist.github.com/sphingu/5781036
----------------------------------------------------
// Using WebBrowser for Crowling in WPF
----------------------------------------------------
<WebBrowser Cursor="Arrow" Name="MyBrowser" LoadCompleted="MyBrowser_OnLoadCompleted" />
----------------------------------------------------
// useful methods of WebBrowser
----------------------------------------------------
MyBrowser.Navigate(new Uri("http://google.com"));
private void MyBrowser_OnLoadCompleted(object sender,NavigationEventArgs e)
{
...
// return source of loaded page in WebBrowser
var document=(IHTMLDocument3) MyBrowser.Document;
//Get Element and set its value
document.getElementById("userName").setAttribute("value","myusername");
//get Button on page and fire its click event
document.getElementById("btnSubmit").click();
// Invoke javascript function on page loaded on WebBrowser
MyBrowser.InvokeScript("submitform",param1,param2,...);
//get data from table in page
_innerHtmldata =
((IHTMLDocument3) MyBrowser.Document).getElementById("datatable")
.innerHTML;
}

Related

Excel buttons to call WPF windows

I have a CSharp (.NET) application that has created an add-in with a ribbon in Excel. I have buttons in the ribbon. I want to be able to click on the buttons, and open WPF windows.
The code looks like
private void OnNewButtonAction(object sender, RibbonControlEventArgs e)
{
var window = new View.MyWindow()
{
DataContext = new ViewModel.MyViewModel(),
};
window.Show();
}
Where MyWindow is a class of type System.Windows.Window. MyWindow has its own xaml file, which has radio buttons, text fields etc. When I try to run this - and click on the button, I get an XML parse exception as - "'Provide value on 'System.Windows.StaticResourceExtension' threw an exception".
Is it possible to invoke wpf windows from excel add-ins? What am I doing wrong?
Edit: I have already looked at
Using WPF Controls in Office Solutions and it doesn't work. And it adds a separate pane to excel, which is not what I am looking at.
You should use Excel-DNA. Its a really useful piece of software designed just for things like this, it helps to implement Excel with WPF, and you shouldn't have any problems again.
you can get it Here
If however you don't want that there is a step by step tutorial here on how to do it.
You may try to define a WPF window as a custom control and add it to the custom pane of the word. Have a look at the following link, pleases:
Using WPF Controls in Office Solutions
You may have a look at this link as well:
Office 2007 Excel Addin - WPF ComboBox Collapses when Expanded
The common way we add WPF control to custompane is:
Create an Excel add-in project
Add user control (WPF) name UserControl1 and add reference to System.xaml
Code the WPF control and Build the project successfully
Add User Control from Window Form collection, named the control as UserControl2
Drag and drop a UserControl1 to UserControl2, assign the position as you like
Code the ThisAddIn.cs in this way:
UserControl1 myWPF;
UserControl2 winformControl;
Microsoft.Office.Tools.CustomTaskPane pane;
System.Windows.Forms.Integration.ElementHost myHost;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
myWPF = new UserControl1();
winformControl = new UserControl2();
pane = CustomTaskPanes.Add(winformControl, "WPFControl");
pane.Visible = true;
pane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
}
We can use the button to control the pane's visible property.

How to find out if the loaded page is error page or valid page in System.Windows.Forms.WebBrowser?

I am using
System.Windows.Forms.WebBrowser
control in C# for displaying some pages. I want to do some custom work when the user clicks on a url of a page which does not exist.
Basically I want to set some values when the browser displays the following message
The page cannot be displayed
The page you are looking for is currently unavailable. The Web site might be experiencing technical difficulties
How can I get the status so that I can differenciate between a page loaded and error page?
If you cast WebBrowser to the underlying ActiveX implementation, you can access the NavigateError event.
Note: You'll need to add a reference to SHDocVw. Confusingly, this is in the COM tab with the name "Microsoft Internet Controls" with a path of c:\windows\system32\ieframe.dll
private void button1_Click(object sender, EventArgs e)
{
//Note: you need to wait until the ActiveXInstance property is initialised.
var axWebBrowser = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
axWebBrowser.NavigateError += axWebBrowser_NavigateError;
webBrowser1.Url = new Uri("http://www.thisisnotavaliddomain.com");
}
void axWebBrowser_NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
{
//handle your error
}
You can use the CreateSink method on the WebBrowser control to access the NavigateError event of the underlying WebBrowser ActiveX control. The System.Windows.Forms.WebBrowser control is a managed wrapper for the WebBrowser ActiveX control, but it does not wrap all of the functionality of that ActiveX control. The NavigateError event is available on the unmanaged ActiveX web browser control. CreateSink will allow you to extend the functionality of the System.Windows.Forms.WebBrowser control so you can handle the NavigateError event.
From the documentation:
This method is useful if you are familiar with OLE development using
the unmanaged WebBrowser ActiveX control and you want to extend the
functionality of the Windows Forms WebBrowser control, which is a
managed wrapper for the ActiveX control. You can use this
extensibility to implement events from the ActiveX control that are
not provided by the wrapper control.
MSDN has a full example here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.createsink.aspx

How do you navigate to a new page in a metro app?

trying to write a metro app in C# right now. Ran into a problem while trying to navigate to a new page.
<HyperLinkButton NavigateUri="foo.xaml"/>
doesn't work, as the NavigateUri field doesn't exist.
The Windows.Navigate namespace isn't available either, so no luck there. What's the proper way to go to a new page in my app in metro?
You can handle the Button control's Click event (in fact, you can use all events with the following code) because Metro's HyperLink button only inherits the ButtonBase class without any special properties or events, such as NavigateUri.
If you want to navigate to another page in your metro app, add a frame in the .xaml page and put this code in the button's event handler:
this.Frame.Navigate("VideoStoryCreator.ComposePage");
There are two ways of Navigating to another page -
Client app way ->
You'll implement this in click event -
var page = new PageName();
Window.Current.Content = page;
Similar to Silverlight Navigation
If you are using Frame/Page Navigation then you can do it like this -
Create a shell page (master page) with the element declared. Then instead of creating new pages with create .
The easiest way to do this is to replace UserControl with Page
This is nicely explained in this tutorial -
http://www.markermetro.com/2011/11/technical/windows-8-metro-style-apps-implementing-navigation-with-xaml/
Hope it helps!
Regards,
Bhavik
Handle the Click event on the button and then call the Navigate method on your Frame
just type the following code in your click event
this->Frame->Navigate(TargetPageName::typeid,this);

How to open a window then load my XAML file to it? (XAML and C#)

I am new to WPF and C# and i need some help - sorry about the newbie question!
Anyway I have a control panel 'window' as the file thats loaded when i run my project, and i then have a button on this control panel, that when clicked triggers an event. Inside this 'event function' I am trying to load a new window that has its own XAML code behind, how do i go about doing this? I have googled but to no avail.
Please explain in laymens terms, I am still getting the hang of this all.
Thanks!
private void btnCustomers_clicked(object sender, RoutedEventArgs e)
{
//load in Customers.xaml file here - in a new window
}
You need to declare an instance of the class that is your other window then call Show() on it. So if your other window is call MySecondWindow you write the following in your event handler.
MySecondWindow otherWindow = new MySecondWindow();
otherWindow.Show();
A basic explination of how windows work can be found on the MSDN Site.

how to launch a default browser instance within a program window?

I am creating console app, involving lots of services. I want to display a webpage within my program window. I know how to launch a new window ( http://dotnetpulse.blogspot.com/2006/04/opening-url-from-within-c-program.html )but I was wondering can I ask a browser to render a webpage withing my program window (in C#) ?
The best solution would likely be to use the WebBrowser control.
This can be placed on a form and allows the web page to appear inside your application.
Here is a nice example of how to go about implementing it http://ryanfarley.com/blog/archive/2004/12/23/1330.aspx
Hope this helps
Use the WebBrowser control and pass it the URL of the web page to render.
In your Windows form, something like (off the top of my head so may need tweaking to get it to compile):
WebBrowser browser = new WebBrowser();
browser.Dock = DockStyle.Fill;
this.Add(browser);
browser.Navigate("http://www.myurl.com");
In a Form you can use WebBrowser Control... in a Console Application there is no way wihout opening a new form.... but you could:
design a form with a Webbrowser Control
hide its border and showintask = false
Open it at a position in your Console-Window

Categories

Resources