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.
Related
Currently i have a winform where i need to open report in WebBrowser Control. I am using impersonation method to view as different user. But somehow the WebBrowser will pop up a windows security authentication for me to enter my credential. When i input the credential as someone that has the right to view the report (not my credential) it will just show a blank page. Not even a message that says:
The permissions granted to user 'Domain\first.last' are insufficient for performing this operation. (rsAccessDenied)
The First Page:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (new ImpersonateUser("TestUsername", Environment.UserDomainName, "TestPassword"))
{
Form2 reportForm = new Form2();
reportForm.Text = "Test";
reportForm.GetReportUrl("http://url/");
reportForm.ShowDialog();
}
}
}
The Second Page:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
label1.Text = Environment.UserName;
}
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.Refresh();
}
public void GetReportUrl(string repUrl)
{
webBrowser1.Url = new Uri(repUrl);
}
}
Can someone explains to me why is this happening? And how i can fix this? Thank you.
But i didn't get why you have to do impersonation to launch a form, from the above code you are not doing any by doing impersonation.
if you are facing the problem in launching a application under user profile then the below link will be helpful.
WTSQueryUserToken returning FALSE
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.
I have Main Form which contains a Panel which loads different Usercontrol into the panel.
Now i need to access functions in the Main Form from the UserControl.
Below i have given my code;
This is my main Windows form class:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
loadlogin();
}
private void loadlogin()
{
login log = new login();
mainPannel.Controls.Clear();
mainPannel.Controls.Add(log);
}
public void mytest()
{
}
}
As you can see i am loading a usercontrol to the mainPannel.
Now lets look at the usercontrol:
public partial class login : UserControl
{
string MyConString = "";
public login()
{
InitializeComponent();
}
public void button1_Click_1(object sender, EventArgs e)
{
//I want to call the parent function mytest(); HOW????
}
}
I want to access mytest() function when button1 us clicked. I have tried alot of other solution but i am still confused.
I have used:
Form my = this.FindForm();
my.Text = "asdasfasf";
This gives me reference to parent and i can change the form text but how can i access its functions???
Ok, the above answer will work but it is not good coding practice to pass in a "Parent Form" to a user control. UserControls should be located in a separate project from your forms project and your forms project should reference your control project in order to gain visiblity to them. Lets say for instance that you want this UserControl to be nested in another UserControl at some point. Your code no longer works without overloading the constructor. The better solution would be to use an event. I have provided an implementation using a CustomEventArg. By doing this, your login UserControl can sit on anything and still work. If the functionality to change the parents text is not requried, simply do not register with the event. Here is the code, hope this helps someone.
Here is the form code:
public Form1()
{
InitializeComponent();
loadlogin();
}
private void loadlogin()
{
login log = new login();
//Registers the UserControl event
log.changeParentTextWithCustomEvent += new EventHandler<TextChangedEventArgs>(log_changeParentTextWithCustomEvent);
mainPannel.Controls.Clear();
mainPannel.Controls.Add(log);
}
private void log_changeParentTextWithCustomEvent(object sender, TextChangedEventArgs e)
{
this.Text = e.Text;
}
Here is the "login" UserControl code (in my test solution, just a userControl with a button)
public partial class login : UserControl
{
//Declare Event with CustomArgs
public event EventHandler<TextChangedEventArgs> changeParentTextWithCustomEvent;
private String customEventText = "Custom Events FTW!!!";
public login()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Check to see if the event is registered...
//If not then do not fire event
if (changeParentTextWithCustomEvent != null)
{
changeParentTextWithCustomEvent(sender, new TextChangedEventArgs(customEventText));
}
}
}
And finally, the CustomEventArgs class:
public class TextChangedEventArgs : EventArgs
{
private String text;
//Did not implement a "Set" so that the only way to give it the Text value is in the constructor
public String Text
{
get { return text; }
}
public TextChangedEventArgs(String theText)
: base()
{
text = theText;
}
}
Writing your controls in this fashion, with no visibility to Forms will allow your UserControls to be completely reusable and not bound to any type or kind of parent. Simply define the behaviors a UserControl can trigger and register them when necessary. I know this is an old post but hopefully this will help someone write better UserControls.
This may helps:
public partial class Form1 : Form
{
//Other codes
private void loadlogin()
{
login log = new login(this); //CHANGE HERE
mainPannel.Controls.Clear();
mainPannel.Controls.Add(log);
}
//Other codes
}
And
public partial class login : UserControl
{
Form1 _parent; //ADD THIS
public login(Form1 parent)
{
InitializeComponent();
this._parent = parent; //ADD THIS
}
public void button1_Click_1(object sender, EventArgs e)
{
this._parent.mytest(); //CALL WHAT YOU WANT
}
}
From your UserControl:
((Form1)Parent).mytest();
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?
Before I used MDI and it worked fine, I could show my ListForm in MainForm. Now that I don't want to use MDI, it didn't work.
Before, with Mdi:
public partial class Le_MainForm : DevExpress.XtraEditors.XtraForm
{
public Le_MainForm()
{
InitializeComponent();
this.IsMdiContainer = true;
this.Name = "MainUSER";
if (Program.IsFA) barButtonItem_OrdList.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
Liste_Ordres f_Liste = new Liste_Ordres();
f_Liste.MdiParent = this;
f_Liste.Show();
}
private void barButtonItem_ListeOrdres_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Close_AllForm();
Liste_Ordres f_Liste = new Liste_Ordres();
f_Liste.MdiParent = this;
f_Liste.Show();
}
private void barButtonItem_CreatOrdreAller_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Close_AllForm();
Program.AllerRetour = "Ordre Aller";
Fiche_Ordre f_Fiche = new Fiche_Ordre();
f_Fiche.MdiParent = this;
f_Fiche.Show();
}
Now, after I eliminated the Mdi //this.IsMdiContainer = true;
and all Forms inherited from MainForm:
public partial class Liste_Ordres : Le_MainForm
{
.....
I cannot show my ListeForm in MainFrom
public partial class Le_MainForm : DevExpress.XtraEditors.XtraForm
{
public Le_MainForm()
{
InitializeComponent();
//this.IsMdiContainer = true;
this.Name = "MainUSER";
if (Program.IsFA) barButtonItem_OrdList.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
Liste_Ordres f_Liste = new Liste_Ordres();
// f_Liste.MdiParent = this;
f_Liste.Show();
}
Someone has any idea ?
If you want your MainForm to be something like a master page, you can use only the MainForm and design all other masks not as Forms but as Controls you put on that MainForm.
Okay, I think I figured a way to open an inheriting form on Initialization.
First of all, in my MainForm I created an integer outside of any functions:
private int a = 1;
Then for my mainform I created a protected virtual on_Load event:
protected virtual void Le_MainForm_Load(object sender, EventArgs e)
{
if (a == 1)
{
Liste_Ordres frm = new Liste_Ordres();
frm.Show();
a = 0;
}
}
Next in my inheriting form, I overrided the on_Load event:
protected override void Form1_Load(object sender, EventArgs e)
{
}
That at least got both forms open without using Mdi (though in a rather round about way), but now another problem remains:
When both forms open, MainForm opens in front of inheriting (no matter if you try using functions BringToFront() and SendToBack()).
Perhaps when I figure that problem out (If I do), then I will edit this answer, but for now this is the end.
Hope this works!