Display a webpage via an iframe - c#

I'm looking to create a very simple program that's only function is to display my website. So basically the program would be like an iframe of the website. What is the best way to achieve this?

You can't use iFrame since it is a browser technology. You must use a web browser as follows, I believe:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// When the form loads, open this web page.
webBrowser1.Navigate("http://www.dotnetperls.com/");
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
// While the page has not yet loaded, set the text.
this.Text = "Navigating";
}
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
// Better use the e parameter to get the url.
// ... This makes the method more generic and reusable.
this.Text = e.Url.ToString() + " loaded";
}
}
}
Ref. http://www.dotnetperls.com/webbrowser
Note: Example is in C#.
Check this out for C++ What embedded browser for C++ project?

Related

How to take text from .txt and put it into text box?

I'm very new at this and I am attempting to take text from a .txt file and input it into a text box.
I have tried to read text from a file that has been located on my computer
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = (File.ReadAllText("F:\\Example"));
}
I need textBox1 to display the text that is in "F:\Example"
This example adds a handler to the form's OnLoad event:
using System;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Text = File.ReadAllText(#"F:\Example");
}
}
}
As #John said, if you want to display a text after form load you can use Form.Load event directly or you can override it like so:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
textBox1.Text = File.ReadAllText("F:\\Example");
}
You can also load text by button click.
if you use Form_Load then you should read the file asynchronously because any file loading time will freeze your form from being displayed.
For instance, if your file takes 5 seconds to load then the form will not be visible for 5 seconds.
Here's an example that uses Task.Run to load the data asynchronously then display it. If first shows the form with message "Loading data...", then the text box is updated once the data has been loaded.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Text = "Loading data...";
LoadData();
}
private async void LoadData()
{
string text = null;
await Task.Run(() =>
{
text = File.ReadAllText("z:\\very_large_file.txt");
});
this.textBox1.Text = text;
}
}
There are of course many other ways to load a file asynchronously (e.g. using streams) but I think this sample code is easier to understand.
Hope this helps :)

Web Browser will not navigate to link

My WebBrowser object will not Navigate() to the link I give it.
The object remains empty when I run it.
When I put the url into the url property though, it successfully loads the link.
namespace Program
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webbMain.Navigate("https://twitter.com/login");
}
}
}
You can try the following:
You can try the following in Page_Load:
WebBrowser wbbMain= new WebBrowser();
wbbMain.AllowNavigation = true;
wbbMain.Navigate("http://www.stackoverflow.com");
Also, you can try to provide override for DocumentCompleted event.
If nothing works, then see if Internet explorer is isntalled. If its not installed then web brower may not work.

C# Chromium browser whatsapp web send message using textbox

I already follow the Chromium user guide, still doesn't work. Using my C# program, I want to send message or image via whatsapp web by cascading the textbox in my program with the message box of whatsapp. This is my code
using CefSharp;
using CefSharp.WinForms;
using System;
using System.Windows.Forms;
namespace ChromiumwithEditorWsWeb
{
public partial class Form1 : Form
{
ChromiumWebBrowser browser;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
browser = new ChromiumWebBrowser("http://web.whatsapp.com");
browser.Dock = DockStyle.Fill;
panel1.Controls.Add(browser);
}
private void buttonBack_Click(object sender, EventArgs e)
{
browser.Back();
}
private void buttonForward_Click(object sender, EventArgs e)
{
browser.Forward();
}
private void buttonReload_Click(object sender, EventArgs e)
{
browser.ShowDevTools();
}
private void buttonGo_Click(object sender, EventArgs e)
{
browser.Load(textBoxUrl.Text);
}
private void buttonPhoneNo_Click(object sender, EventArgs e)
{
browser.EvaluateScriptAsync("document.GetElementsByClass('jN-F5 copyable-text selectable-text').value = textBoxPhone.Text");
browser.EvaluateScriptAsync("document.GetElementsByClass('_2S1VP copyable-text selectable-text').value = textBoxMessage.Text");
}
}
}
Help it! Give suggestions for the best step.
Clipboard.SetText(msg);
Thread.Sleep(TimeSpan.FromSeconds(2));
SendKeys.Send(msg); //SendKeys.SendWait("^{V}");
Thread.Sleep(TimeSpan.FromSeconds(1));
SendKeys.SendWait("{ENTER}");
Not a good solution though, as it requires the windows to be on focus and this uses the clipboard. But it works !

winform webbrowser not replaced

From my Form1 I initialize a class scraper. In the scraper class is an function login. The idea is that that class log's the user in on an website, and returned the web browser so that an logged in webbrowser control is available in Form1.
I've got this code so far: Form1
private void button1_Click(object sender, EventArgs e)
{
Scraper scraper = new Scraper(this);
scraper.login(conf._webLogin);
}
public void updateLoginWeb(WebBrowser web)
{
webBrowser1 = web;
MessageBox.Show("DONE");
}
The conf class:
public WebBrowser _webLogin = new WebBrowser();
The scraper class:
private Form1 parent;
private WebBrowser _web_Login = new WebBrowser();
public Scraper()
{
}
public Scraper(Form1 parent)
: this()
{
this.parent = parent;
}
public void login(WebBrowser web)
{
_web_Login = web;
_web_Login.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(login_DocumentCompleted);
_web_Login.Navigate("http://www.google.com/");
}
private void login_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//This line is so you only do the event once
if (e.Url != _web_Login.Url)
return;
parent.updateLoginWeb(_web_Login);
}
I use google as test, but nothing works (not even an another site).
The problem is that the webbrowser in the Form isn't updated. It's still an white screen.
What do you guys think of this? Do you know what the problem is or do you guys know an better way to handle this?
I think your problem is that you can not simply assign the webBrowser variable:
webBrowser1 = web;
You are changing the Form1.webBrower1 variable, but the Forms.Controls collection is still pointing to the original webBrowser control.
Can't you just pass Form1.webBrower1 to scraper.login function?:
private void button1_Click(object sender, EventArgs e)
{
Scraper scraper = new Scraper(this);
scraper.login(webBrowser1);
}
public void updateLoginWeb(WebBrowser web)
{
//webBrowser1 = web; // you don't need this anymore
MessageBox.Show("DONE");
}
If you really need to replace your control you can do something like:
public void updateLoginWeb(WebBrowser web)
{
Controls.Remove(webBrowser1);
Controls.Add(web);
webBrowser1 = web; // you don't need this anymore
MessageBox.Show("DONE");
}
But you will probably to set the new webbrowser layout properties manually.

Copying data from form1.richTextBox to form2.richTextbox

I am making a Time clock for fun and to learn C#.
I have the time down, and the start, stop, clear.
However I am having issues with a "Notes" section. Ideally I'd like to be able to write notes into a field, and have an "Edit" button to allow the user to open a window for more options relating to text editing. (with the text from the Form1 rich text box)
My issue comes from copying the data from one form to another.
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PunchOut
{
public partial class PunchOut : Form
{
public PunchOut()
{
InitializeComponent();
}
int i = 0;
private void button3_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
i++;
TimeSpan t = TimeSpan.FromSeconds(i);
textBox2.Text = string.Format("{0:D2}:{1:D2}:{2:D2}",
t.Hours,
t.Minutes,
t.Seconds);
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Clear();
textBox2.Text = ("00:00:00").ToString();
}
private void button6_Click(object sender, EventArgs e)
{
}
public void button4_Click(object sender, EventArgs e)
{
new Form2().Show();
richTextBox1.Text = Form2.richTextBox1.Text;
}
}
}
Here is the Form2 code:
namespace PunchOut
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.Text = PunchOut.richTextBox1.Text;
}
}
}
Currently, I get an error that states:
an object reference is required for the non-static field, method, or property 'PunchOut.PunchOut.richTextBox1'
and
an object reference is required for the non-static field, method, or property 'PunchOut.Form2.richTextBox1'
Why do I get these errors?
lots of unneeded work going on there. I hope I explained this well enough
Breakdown:
We add a String Member Variable so that we can put the contents of a RichTextBox into a string and pass that instead of using the RichTextbox control.
We change the constructor to take a string parameter which is the RTF text that you want to change. Now Form2 can change any RTF text and not be specifically tied to just the richTextbox1 on the punchoutForm.
We then change the updating of the member variable to when the form is closing otherwise you are changing it with each keystroke which is a lot of unnecessary method calls.
namespace PunchOut
{
public partial class Form2 : Form
{
public String richText;
public Form2(String rText)
{
InitializeComponent();
this.richTextBox1.Rtf = rText;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
richText = this.richTextBox1.Rtf;
}
}
}
Then in your button4_Click. Use Rtf to include the codes otherwise just use a plain textbox
Now in your button click event handler we create a new form and assign it to a variable. We then call showdialog. The reason for showdialog is that this will make the form the top most form so that a user cannot go back to the punchout form and make a change to the richtextbox which would then make the text in the Form2 obsolete as it would no longer represent the correct RTF text in the punchout form. When the user is done editing the text and closes the form we then request the edited rtf text by accessing the richText Member Variable of Form2. The reason you can access this after the form has closed is that the form is not disposed until the method returns, your local variable lives within the scope of the method.
public void button4_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(richTextBox1.Rtf);
f2.ShowDialog();
richTextBox1.Rtf= f2.richText;
}
You didn't make a new reference to PunchOut in Form2. In your form 2 add this under the class declaration:
PunchOut punchOut;
And you get in Form2:
namespace PunchOut
{
public partial class Form2 : Form
{
PunchOut punchOut;
public Form2(PunchOut PUNCHOUT)
{
punchOut = PUNCHOUT;
InitializeComponent();
}
public void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.Text = punchOut.richTextBox1.Text;
}
}
}
In the original replace button4_CLick with:
public void button4_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
richTextBox1.Text = form2.richTextBox1.Text;
}
EDITED: You should pass the old PunchOut instead of creating a new one. I've updated the code.
You could store the text in a more accessible string. Then have your other form call that string.
public string yourText;
// down further
yourText = textBox1.Text;

Categories