I am trying to make a web browser with multiple tabs. But now, I have a problem with the DocumentTitle for the name of the tabs.
The problem here is that the code to name the tab is performed before it loads the page. I tried to find a way to perform it after, but it doesn't work.
For example:
private void stackoverflowToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser) tabControl1.SelectedTab.Controls[0]) .Navigate("Http://www.stackoverflow.com/");
Browser_Navigated(null, null);
}
void Browser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
tabControl1.SelectedTab.Text = ((WebBrowser)tabControl1.SelectedTab.Controls[0]).DocumentTitle;
}
The WebBrowser class exposes a DocumentTitleChanged event that you can use to update the tab title.
Related
I have a windows webview2 application where we are automating 3rd party website with too many nested iframes and I want to execute javascript in a particular iframe. I want to know how get CoreWebView2Frame instance for the particular iframe using WebView2. Microsoft documentation didn't help.
I am looking for an example or a documentation to work with CoreWebView2Frame Class and shows how to execute javascript in a particular iframe by using CoreWebView2Frame.ExecuteAsync().
I have gone throught the this thread but it's too difficult for me to understand.
You can get a CoreWebView2Frame by handling FrameCreated event and use it later.
Example:
In the following example, the WebView2 browses this Uri, then we find the iframe inside the page, and store it for later use. Later when the user clicks on a button on the WinForms app, using ExecuteScriptAsync method of the iframe, we click on a link inside the iframe programmatically.
To do so, drop a WebView2 and a Button on the form. Handle Load event of the form and Click event of the button and modify the code like this:
//using Microsoft.Web.WebView2.Core;
CoreWebView2Frame sampleFrame;
private async void Form1_Load(object sender, EventArgs e)
{
webView21.Source = new Uri(
"https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_link_test");
await webView21.EnsureCoreWebView2Async();
webView21.CoreWebView2.FrameCreated += CoreWebView2_FrameCreated;
}
private void CoreWebView2_FrameCreated(object? sender,
CoreWebView2FrameCreatedEventArgs e)
{
if (e.Frame.Name == "iframeResult")
{
sampleFrame = e.Frame;
}
}
private async void button1_Click(object sender, EventArgs e)
{
if (sampleFrame != null && sampleFrame.IsDestroyed() == 0)
{
await sampleFrame.ExecuteScriptAsync(
"document.getElementsByTagName('a')[0].click();");
}
}
Now if you click on the button, the link which is inside the iframe will be clicked.
I have an application that needs to click on button located on tab pages. So for example:
Tabcontrol has Tabpage1, Tabpage2, .... TabpageN
and each tabpage has a buttonX that performs a taskX.
Now supposing, I'm on TabPage1 and I want to Click ButtonX on Tabpage3 without changing tabindex/selectedindex. How can I do that?
Generally one would use PerformClick but I've noticed it failing on TabControl. So if not dependent on EventArgs, create a sub method for the click event e.g.
private void button1_Click(object sender, EventArgs e)
{
Button1PerformClick();
}
private void Button1PerformClick()
{
MessageBox.Show("Button1");
}
Than calling
private void button2_Click(object sender, EventArgs e)
{
Button1PerformClick();
}
Doesn't matter in regards to which tab the button is on in this case.
I want to click on one link in one page.
Here is my code:
private void Button1_Click(object sender, EventArgs e)//GO
{
if (!Xpcom.IsInitialized) Xpcom.Initialize("Firefox");
geckoWebBrowser1.Navigate("http://www.tsetmc.com/loader.aspx?ParTree=151311&i=67126881188552864");
}
And I want to click on a link:
حقیقی-حقوقی
can anybody help me?
thanks.
I just looked up a Gecko project where I was clicking on the link and while the following might not be the most elegant way the same technique should work for you:
private void geckoWebBrowser1_DocumentCompleted(object sender, EventArgs e)
{
var elements = geckoWebBrowser1.Document.GetElementsByTagName("a");
foreach (GeckoHtmlElement element in elements)
{
if (element.ClassName == "violet")
{
element.ScrollIntoView(false);
element.Click();
}
}
}
I don't think the ScrollIntoView call is actually required, I just did that because it was an animated button and I wanted to see it was working. But you will need to wait until the document has loaded before clicking so I've put it in the DocumentCompleted event so before the Navigate don't forget to add:
geckoWebBrowser1.DocumentCompleted += geckoWebBrowser1_DocumentCompleted;
I cannot seem to refresh or reload a parent window or page in wpf. I have tried the following:
private void btClear1_Click(object sender, RoutedEventArgs e)
{
//this.LayoutUpdate();
//Refresh();
//this.NavigationService.Refresh();
}
You can achieve complete layout/render pass with InvalidateVisual call (http://msdn.microsoft.com/en-us/library/system.windows.uielement.invalidatevisual.aspx)
I am hoping someone here can help me, i have a Tabless Control on my windows forms application and basically because the tabs are purposely hidden i have added 2 buttons to each tab "Next" and "Back".
This is the code snippet i have for my "Next" button:
private void nextbutton1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage3;
this.toolStripStatusLabel8.Text = System.DateTime.Now.ToString();
}
Which works fine, however when i use the exact same theory on the "Back" button it does not work:
private void backbutton1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabmain;
this.toolStripStatusLabel1.Text = System.DateTime.Now.ToString();
}
So my question is how does one go to a previous tabpage from a button? I have looked through here and tried all of the links that came up but nothing has worked any ideas?
You should use the SelectedIndex property instead of using concrete TabPage instances. This way it will still work when you decide to change the order of the pab pages or add new pages:
private void previousButton_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex > 0)
{
tabControl1.SelectedIndex--;
}
}
private void nextButton_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex < tabControl1.TabCount - 1)
{
tabControl1.SelectedIndex++;
}
}
Since there is no "Tabless" tab control in .NET Framework I can only assume that it works similar to the standard TabControl. If the solution doesn't work you should give us some information about the actual class you use.
BTW: There is no need to repeat the buttons on each page. Why don't you just put the buttons outside the TabControl?
Also: I see that you use a ToolStripStatusLabel to show the current time. Instead of updating it each time the user clicks somewhere add a Timer to your form. Set its Interval to 1000 and handle its Tick event. Update the label there:
private void timer1_Tick(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = DateTime.Now.ToLongTimeString();
}
This way it updates constantly and again there is no need to repeat anything. You need to call timer1.Start() in the form's constructor.