I'm trying to make an C# windows form application, with an webbrowser.
I'm using the webkit browser: Link to the browser
The webbrowser did i put in an class file, so i can acces it through all the forms i'm going to use.
The code that's generate the webbrowser:
public static WebKit.WebKitBrowser mainBrowser = new WebKitBrowser();
I'm having this piece of code that give's some problems:
globalVars.mainBrowser.Navigate("http://www.somesite.com/");
while (globalVars.mainBrowser.IsBusy)
{
System.Threading.Thread.Sleep(500);
}
globalVars.mainBrowser.Document.GetElementById("user").TextContent = "User Name";
But it's not working. If i do an message box after the while, it shows up before it's possible to render the page...
So what is the best way to wait until the site is fully loaded?
UPDATE 1
In an standalone class file, am i making the webkit controll like this:
public static WebKit.WebKitBrowser mainBrowser = new WebKitBrowser();
And in an form, i've got now this code (thanks to Tearsdontfalls):
public void loginthen()
{
globalVars.mainBrowser.DocumentCompleted += mainBrowser_DocumentCompleted;
globalVars.mainBrowser.Navigate("http://www.somesite.com/");
}
void mainBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var send = sender as WebKit.WebKitBrowser;
if (send.Url == e.Url)
{
MessageBox.Show("Inloggen");
globalVars.mainBrowser.Document.GetElementById("user").TextContent = "User Name";
}
}
But no messagebox shows up. But if i use an local (on the same form) webkit browser, i'm getting te MessageBox. But then the user field isn't filled in.
Even an breakpoint in the documentCompleted event, isn't triggerd. So it looks like the event listner isn't working...
So why is it not working?
You can simply create an event listener on the Document Completed Event on your Webbrowser, or you can create it dynamically like that:
globalVars.mainbrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(mainbrowser_DocumentCompleted);
Where mainbrowser_DocumentCompleted is the name of the void where you can do sth like this(I used the names of your provided code):
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
var send = sender as WebKit.WebKitBrowser;
if (send.Url == e.Url) {
globalVars.mainBrowser.Document.GetElementById("user").TextContent = "User Name";
}
}
Adding the following piece of code let the events fire when the browser is in invisible mode.
using (Bitmap bmp = new Bitmap(webKitBrowser.Width, webKitBrowser.Height))
{
webKitBrowser.DrawToBitmap(
bmp,
new Rectangle(
webKitBrowser.Location.X,
webKitBrowser.Location.Y,
webKitBrowser.Width,
webKitBrowser.Height
)
);
}
Related
Im currently trying to create a messaging application from client to server. When the server sends the client a message it will open up an new form and add the text to a text box inside the form.
However the client is defiantly reviving the message, I tested this with a message box displaying the message before trying to update the text box. The text box is displaying the message perfectly.
However, when i try to edit the textboxt nothing happens. However my other method on a button click works perfectly. really unsure why this is happening.
I've also written an invoke method to check if the textbox need's to be invoked as i am using differen't threads for some methods.
Below is an example of my code with some screenshots the method below is being opened from another form "MAIN" i have reference to it in the code. Not sure if that is causing an issue perhaps?
public ChatWindow()
{
InitializeComponent();
Thread timerThread = new Thread(Main.ReceiveLoop);
timerThread.Start();
}
private void txtChatLog_TextChanged(object sender, EventArgs e)
{
}
private void btnSendMessage_Click(object sender, EventArgs e)
{
string clientReply = txtReply.Text;
string Message = "ClientMsg§" + clientReply;
var time = DateTime.Now;
txtChatWindow.AppendText($"{time} client: {clientReply}");
txtChatWindow.AppendText(Environment.NewLine);
Main main = new Main();
main.ChatResponse(Message);
txtReply.Text = "";
}
delegate void setTextCallBack(string message);
public void UpdateChatLog(string message)
{
if (txtChatWindow.InvokeRequired)
{
setTextCallBack d = new setTextCallBack(UpdateChatLog);
this.Invoke(d, new object[] { message });
}
else
{
var time = DateTime.Now;
string newMessage = message.Split('$')[1];
string messageToDisplay = $"{time} Server: {newMessage}";
MessageBox.Show(messageToDisplay);
this.txtChatWindow.AppendText(messageToDisplay);
this.txtChatWindow.AppendText(Environment.NewLine);
}
}
Below are some images of my server and the code running on the client:
as you can see the server is recieving what the client replies with, the textbox is also being appended for when the client presses the SEND button. However it isn't being changed when the method is called. You know the method is being called because the MessageBox.Show is being called and displaying what should be in the text box.
Really unsure what the issue is here. If anyone could help me It would be very appreciated! This is a project for my university degree that is due in soon!
Thankyou in advance!
I made a plugin for Revit (Autodesk) that starts an external Form using: System.Windows.Forms; in c# from the assembly PresentationCore. Know i want to fill this window with a Zendesk Chat (Zopim). Unfortunately i have no idea how to use REST APIs in c#. I just looke into the topic today.
So detailed what i want: I want that when the Form is opening that the programm loads the Zopim chat form and puts it into the my c# Form.
I already know that i somehow need to get the Form of the chat and that i need to parse it into my form.
Problems are: I can't find the zopim chat form.
I don't know how to convert it into my UI.
Zendesk API
A Screenshot of my Form i created
My Code of the Form:
[Transaction(TransactionMode.Manual)]
class DoSomething : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
this.StartForm();
return Result.Succeeded;
}
public void StartForm()
{
//EXECUTE AN EXTERNAL WINDOW
System.Windows.Forms.Form myF = new System.Windows.Forms.Form();
myF.FormBorderStyle = FormBorderStyle.SizableToolWindow;
myF.StartPosition = FormStartPosition.CenterScreen;
myF.Width = 400;
myF.Height = 600;
myF.HelpButton = true;
Button cButton = new Button();
cButton.Text = "Cancel";
myF.CancelButton = cButton;
myF.FormClosing += delegate (object sender, FormClosingEventArgs e)
{
e.Cancel = true;
myF.WindowState = FormWindowState.Minimized;
};
myF.Show();
}
}
I found an alternative way.
Zendesk Chat (Zopim) does offer a JavaScript add-in. The best way is to start a System.Windows.Forms.WebBrowser and implement the JavaScript Code from Zopim inside your HTML File that refers to the apllication by the WebBrowser.
I am trying to use cefshar browser in C# winforms and need to know how I know when page completely loaded and how I can get browser document and get html elements,
I just Initialize the browser and don't know what I should do next:
public Form1()
{
InitializeComponent();
Cef.Initialize(new CefSettings());
browser = new ChromiumWebBrowser("http://google.com");
BrowserContainer.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
CefSharp has a LoadingStateChanged event with LoadingStateChangedArgs.
LoadingStateChangedArgs has a property called IsLoading which indicates if the page is still loading.
You should be able to subscribe to it like this:
browser.LoadingStateChanged += OnLoadingStateChanged;
The method would look like this:
private void OnLoadingStateChanged(object sender, LoadingStateChangedEventArgs args)
{
if (!args.IsLoading)
{
// Page has finished loading, do whatever you want here
}
}
I believe you can get the page source like this:
string HTML = await browser.GetSourceAsync();
You'd probably need to get to grips with something like HtmlAgility to parse it, I'm not going to cover that as it's off topic.
I ended up using:
using CefSharp;
wbAuthorization.AddressChanged += OnAddressChanged;
and
private void OnAddressChanged(
object s,
AddressChangedEventArgs e)
{
if (e.Address.StartsWith(EndUri))
{
ResultUri = new Uri(e.Address);
this.DialogResult = DialogResult.OK;
}
}
EndUri is the final page I want to examine and ResultUri contains a string I want to extract later. Just some example code from a larger class.
Am new to CefSharp
I have created a class library project and referenced the CefSharp library to render the Web browser, However I am facing some issues showing the web Browser. Please find the exact code
WebBrowser_test1:
public partial class ChildWidget : Window
{
public CefSharp.Wpf.ChromiumWebBrowser webView;
public Widget()
{
InitializeComponent();
CefSharp.CefSettings settings = new CefSharp.CefSettings();
settings.PackLoadingDisabled = true;
if (CefSharp.Cef.Initialize(settings))
{
webView = new CefSharp.Wpf.ChromiumWebBrowser();
main_grid.Children.Add(webView);
webView.Address = "http://www.google.co.uk";
}
}
}
and I am referencing this library (dll) in another project
public MainWindow()
{
InitializeComponent();
Button newbutton = new Button();
newbutton.Width = 50;
main_grid.Children.Add(newbutton);
newbutton.Click += ButtonClick;
}
private void ButtonClick(object sender, RoutedEventArgs e)
{
try
{
Webbrowser_test1.ChildWidget childWidget = new Widget();
childWidget.Show();
}
catch (Exception)
{
throw;
}
}
Now on the Button click I will open the (WebBrowser_test1) child widget in which I will show the web browser .. when the window opens it is showing blank.
Please let me know if I missing anything
Subscribe to IsBrowserInitializedChanged after creating a ChromiumWebBrowser. Then once the browser is initialized you can call Load and your control will be displayed.
...
_browser = new ChromiumWebBrowser();
mainGrid.Children.Add(_browser);
_browser.IsBrowserInitializedChanged += OnIsBrowserInitializedChanged;
...
void OnIsBrowserInitializedChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (_browser.IsBrowserInitialized)
{
_browser.Load("https://www.google.com/");
}
}
I can think of the first three potential issues. But it's hard to tell what the real issue is from your code alone as it strays off a bit from the official examples
Move Cef.Initialize() to your MainWindow constructor. It should only be called once to launch the CefSharp.BrowserSubprocess.exe renderer process.
See my answer to CefSharp 3 always failing Cef.Initialize() for a few things to check regarding binaries and their placement. Really, the recommended approach is to start having the WPF example in the CefSharp.MinimalExample repo running first and then adjust to your use case from there.
I'm not sure a ChromiumWebBrowser() without explicitly setting a width and height works. A 0x0 window might not receive any rendered content. I haven't tried with recent code.
Have you tried replacing
webView.Address = "http://www.google.co.uk";
with
webView.Load("http://www.google.co.uk");
Like jornh mentions, you may have to explicitly set the height and width of the ChromiumWebBrowser. If you don't know the exact size, setting HorizontalAlignment and VerticalAlignment to Stretch (to fill the parent container) will probably also work.
Have you checked if the Cef.Initialize() actually returns true? You could be missing some files, and CefSharp doesn't always give clear error messages when this is the case.
I am developing a c# winform app which controls a website using web browser. I need to perform many events on click of a single button, but many events do not take place until I use a message box to give some sort of lag between events. Below is the code
private void button5_Click(object sender, EventArgs e)
{
try
{
HtmlDocument webDoc = this.webBrowser1.Document;
HtmlElementCollection aTags = webDoc.GetElementsByTagName("a");
string selectedIssue;
selectedIssue = AcknowledgeList.SelectedItem.ToString();
foreach (HtmlElement aElement in aTags)
{
if (aElement.InnerText.Contains(selectedIssue))
{
aElement.InvokeMember("click");
break;
}
}
MessageBox.Show("Device Acknowledged");
this.finalAcknowledge();
}
catch (NullReferenceException connectionError)
{
MessageBox.Show("Connection Error , try again");
}
}
private void finalAcknowledge()
{
try
{
HtmlDocument webDoc = this.webBrowser1.Document;
HtmlElement changeNote = webDoc.GetElementById("#note");
string comment = textBox1.Text;
changeNote.SetAttribute("value", comment);
HtmlElementCollection selectTags
= webDoc.GetElementsByTagName("select");
foreach (HtmlElement selectElement in selectTags)
{
if (selectElement.GetAttribute("name").Equals("status"))
{
selectElement.SetAttribute("value", "6");
}
}
HtmlElement submitButton = webDoc.GetElementById("submit_button");
submitButton.InvokeMember("click");
this.button3.PerformClick();
string selectedIssue;
selectedIssue = AcknowledgeList.SelectedItem.ToString();
AcknowledgeList.Items.Remove(AcknowledgeList.SelectedItem);
AssignToList.Items.Add(selectedIssue);
MessageBox.Show("Device Acknowledged");
this.callShowAssigned();
}
catch (NullReferenceException connectionError)
{
MessageBox.Show("Connection Error , try again");
}
}
Here I have used two message boxes to give some lag between events. I want to get rid of these message boxes and want some other method which can perfrom all the events and I do not have to interrupt the user with some message box or something that is visible
Lag can be done with:
Thread.Sleep(time_to_sleep);
But in this case better to use event:
http://msdn.microsoft.com/en-us/library/5d67hf8a.aspx
webBrowser1.DocumentCompleted+=OnPageLoaded;
When you aElement.InvokeMember("click"), you tell to webBrowser1 make some action: post/get to some page. So webbrowser begins job: it makes call to remote server, gets page, renders it. This take time, which can be longer or shorter. This call is made asynchronous, which means, that your code runs forward without waiting for webbrowser to finish. So what can you do? You can subscribe to webbrowser object events, which will hit when browser control finish work.
//Somewhere before InvokeMember
webBrowser1.DocumentCompleted+=OnPageLoaded;
private void OnPageLoaded(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
//Make your final acknowledgement
//This method will be executed every time, when your page is loaded
}