I want to display the current page-name that's on my WebBrowser control on my Label, but when the Document.Title updates, my label disappears instead of showing the current website title. How can i fix this?
public partial class StreamViewer : Form
{
public StreamViewer()
{
InitializeComponent();
webBrowserViewer.DocumentTitleChanged += new EventHandler(webBrowserViewer_DocumentTitleChanged);
}
private void webBrowserViewer_DocumentTitleChanged(object sender, EventArgs e)
{
label2.Text = webBrowserViewer.DocumentTitle;
}
Use the DocumentTitleChanged event
public Form1()
{
InitializeComponent();
webBrowserViewer.DocumentTitleChanged += new EventHandler(webBrowserViewer_DocumentTitleChanged);
}
private void webBrowserViewer_DocumentTitleChanged(object sender, EventArgs e)
{
label2.Text = webBrowser1.DocumentTitle;
}
https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documenttitlechanged(v=vs.110).aspx
Related
i have a form with a panel in it and when a button is presed a usercontrol showes and the panel hides, now i need to hide a button if the textbox in the form1 contains "admin" in it.
this is the form1 code
public partial class Form1 : Form
{
public string a;
public Form1()
{
InitializeComponent();
a = textBox1.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
afterlogin v = new afterlogin();
v.Dock = DockStyle.Fill;
panel1.Controls.Add(v);
v.BringToFront();
}
}
and this is the usercontrol code
public partial class afterlogin : UserControl
{
public afterlogin()
{
InitializeComponent();
Form1 f = new Form1();
if (f.a.Contains("admin"))
{
button1.Hide();
}
}
}
You're creating a new form in the user control, it will not have the same values as the original form you created your user control in.
If you wish to take in values from the form, add a constructor parameter to the "afterlogin" class with text of the textbox, such as:
public afterlogin(string text)
{
InitializeComponent();
if (text.Contains("admin"))
{
button1.Hide();
}
}
and pass the text value to the constructor of the "afterLogin" class:
afterlogin v = new afterlogin(a);
Since Form1 creates the UserControl, just have the Form itself turn on or off the Button?
You can make a method in your UserControl that allows you to change the visibility of the control:
public partial class afterlogin : UserControl
{
public void setButton(bool state)
{
button1.Visible = state;
}
}
Now you can call setButton when you create the UserControl:
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
afterlogin v = new afterlogin();
v.Dock = DockStyle.Fill;
panel1.Controls.Add(v);
v.BringToFront();
v.setButton(!textBox1.Text.Contains("admin"));
}
I have two user controls usercontrol_1 and usercontrol_2, using a click event i am bring in usercontrol_1 to a panel called panel_screen on my main form.
private void btn_Click(object sender, EventArgs e)
{
if (!panel_screen.Controls.Contains(usercontrol_1.Instance))
{
panel_screen.Controls.Add(usercontrol_1.Instance);
usercontrol_1.Instance.Dock = DockStyle.Fill;
usercontrol_1.Instance.BringToFront();
}
else
usercontrol_1.Instance.BringToFront();
}
Similarly i wanna bring usercontrol_2 to the same panel on the main form using a button (click event) on usercontrol_1.
How do i do this? any help would be appreciated.
I'm not sure where you are having problem, I think the way you are using usercontrol_1.Instance might cause the issue.
I have working example here, you can try it.
private void btn_Click(object sender, EventArgs e)
{
bool userControlIsAlreadyInPanel = false;
//assuming you are cheking if usercontrol_1 is already there on panel
// you don't want to create new usercontrol, but just bring existing control to the front
foreach(UserControl control in panel_screen.Controls)
{
if (control.GetType() == typeof(usercontrol_1))
{
userControlIsAlreadyInPanel = true;
control.BringToFront();
}
}
if(!userControlIsAlreadyInPanel)
{
usercontrol_1 instane = new usercontrol_1();
panel_screen.Controls.Add(instane);
instane.Dock = DockStyle.Fill;
instane.BringToFront();
}
}
output
namespace WindowsFormsApp1
{
public delegate void MyEventDelegate(object sender, string name);
public partial class Form1 : Form
{
usercontrol_1 _ctrl1 = null;
usercontrol_2 _ctrl2 = null;
public Form1()
{
InitializeComponent();
_ctrl1 = new usercontrol_1();
_ctrl1.Dock = DockStyle.Fill;
_ctrl1.userControlButtonClicked += userControlButtonClicked;
_ctrl2 = new usercontrol_2();
_ctrl2.Dock = DockStyle.Fill;
_ctrl2.userControlButtonClicked += userControlButtonClicked;
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
userControlButtonClicked(_ctrl1, "1");
}
private void userControlButtonClicked(object sender, string name)
{
panel1.Controls.Clear();
if (sender.Equals(_ctrl1))
{
panel1.Controls.Add(_ctrl2);
}
else if (sender.Equals(_ctrl2))
{
panel1.Controls.Add(_ctrl1);
}
}
}
}
namespace WindowsFormsApp1
{
public partial class usercontrol_1 : UserControl
{
public event MyEventDelegate userControlButtonClicked;
public usercontrol_1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyEventDelegate med = userControlButtonClicked;
if (med != null)
{
med(this, "1");
}
}
}
}
namespace WindowsFormsApp1
{
public partial class usercontrol_2 : UserControl
{
public event MyEventDelegate userControlButtonClicked;
public usercontrol_2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyEventDelegate med = userControlButtonClicked;
if (med != null)
{
med(this, "2");
}
}
}
}
I've created a new form, in which I have a toolbox. When I press a button in that form, it should relay that information that has been entered by the user(toolboxbox value) to the main form, in which it should say that piece of information in a label.
Since the method to create that username from the toolbox is private, I cannot access it from any other way. Making it public does not seem to make a difference, neither does get,set (from the way I've been trying to atleast).
Picture that may help explaining it:
Code (in which to create user):
namespace WindowsFormsApplication3
{
public partial class Newuserform : Form
{
public Newuserform()
{
InitializeComponent();
}
private void buttonCreateUser_Click(object sender, EventArgs e)
{
string uname = textboxUsername.ToString();
}
public void Unamecreate()
{
}
}
}
Form1 Code (To receive created user):
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
Aboutform form2 = new Aboutform();
form2.Show();
}
private void newLocalUserToolStripMenuItem_Click(object sender, EventArgs e)
{
Newuserform formnewuser = new Newuserform();
formnewuser.Show();
}
}
}
you have a lot of options.
One way is to create an event and handle it in the main form.
public partial class Newuserform : Form
{
//the public property
public event EventHandler<string> UnameChanged;
public Newuserform()
{
InitializeComponent();
}
private void buttonCreateUser_Click(object sender, EventArgs e)
{
if (UnameChanged != null)
UnameChanged(textboxUsername.ToString()); //fire the event
}
}
Now, to "handle" the event, do the following in your main form:
private void newLocalUserToolStripMenuItem_Click(object sender, EventArgs e)
{
Newuserform formnewuser = new Newuserform();
formnewuser.UnameChanged += Handler;
formnewuser.Show();
}
private void Handler (object sender, string Uname)
{
// do something wit the new Uname.
}
note: recreating the Newuserform will require to cleanup previous attached resources.
I have a windows custom control with a picturebox and a button on it's default panel. This windows custom control will be added to a windows form. There is another windows form called form2. when the user double clicks on the custom control, it should load form2. in designer when i double clicked the custom control on the form, it creates a load() event for that custom control. But i need double click event, how this can be done?
here is the graphical view of what happens
Here is code in the control
[DefaultEvent("DoubleClick")]
public partial class cntrlImageLoader : UserControl
{
public cntrlImageLoader()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = Environment.SpecialFolder.MyPictures.ToString();
if (ofd.ShowDialog() == DialogResult.OK)
{
pbImage.Image = Image.FromFile(ofd.FileName);
}
}
private void pbImage_Click(object sender, EventArgs e)
{
this.cntrlImageLoader_DoubleClick(pbImage, e);
}
private void cntrlImageLoader_Load(object sender, EventArgs e)
{
}
private void cntrlImageLoader_DoubleClick(object sender, EventArgs e)
{
}
}
Here is the calling code on form1
private void cntrImLdrFront_DoubleClick(object sender, EventArgs e)
{
//this.cntrImLdrFront.pbImage.DoubleClick += new EventHandler(pbImage_DoubleClick);
}
FrmImageViewer f; // this is form2
private void pbImage_DoubleClick(object sender, EventArgs e)
{
f= new FrmImageViewer();
f.MdiParent = this.MdiParent;
f.Show();
}
Assign the attribute [DefaultEvent("DoubleClick")] at the class declaration.
[DefaultEvent("DoubleClick")]
public partial class MyControl : UserControl
{
}
This will creates event which you have set by default on double click of control at design time where you have placed your user control.
EDITED:
[DefaultEvent("LoadPicture")]
public partial class cntrlImageLoader : UserControl
{
public delegate void LoadPictureEventHandler(object sender, LoadPictureEventArgs e);
public event LoadPictureEventHandler LoadPicture;
private void pbImage_DoubleClick(object sender, EventArgs e)
{
if (LoadPicture != null)
{
LoadPictureEventArgs ev = new LoadPictureEventArgs();
LoadPicture(this, ev);
if (ev.Picture != null)
{
pbImage.Image = ev.Picture;
}
}
}
}
Create another class and give that class name to LoadPictureEventArgs
public class LoadPictureEventArgs : EventArgs
{
public Image Picture {get; set;}
public LoadPictureEventArgs(Image _picture)
{
Picture = _picture
}
public LoadPictureEventArgs()
: base()
{
}
}
HOW TO USE IT?
//FORM1
private void cntrImLdrFron_LoadPicture(object sender, LoadPictureEventArgs e)
{
Image img = null;
//LOAD YOUR IMAGE HERE
e.Picture = img;
}
I am a beginner programmer and I want to develop a small game project for practice, and for so I am making some tests first.
In this test, my program generates a label programmatically, and I want the button to delete it when it is pressed. The problem is, it says that there is no definition for myLabel, so I suppose I should make myLabel a "global" control, but I don't know how. Any ideas? Thanks for the help!
Here's my code so far:
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Label myLabel = new Label();
this.Controls.Add(myLabel);
myLabel.Location = new Point(50, 50);
myLabel.Text = "Yay!";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Controls.Remove(Label.myLabel);
}
}
}
make it a class private field
public partial class Form1 : Form
{
private Label myLabel;
public Form1()
{
InitializeComponent();
myLabel = new Label();
this.Controls.Add(myLabel);
myLabel.Location = new Point(50, 50);
myLabel.Text = "Yay!";
}
private void button1_Click(object sender, EventArgs e)
{
this.Controls.Remove(myLabel);
}