Not getting New Tab in WebBrowser Visual Studio 2012 - c#

I have two forms Form1 and Form2.
Form1 has a WebBrowser and Form2 contains tab code.
In Form1 on load I have kept the following code to get first tab:
public void Form1_Load(object sender, EventArgs e)
{
TabPage t = new TabPage();
Form2 newtab = new Form2();
newtab.Show();
newtab.TopLevel = false;
newtab.Dock = DockStyle.Fill;
t.Controls.Add(newtab);
tabControl1.TabPages.Add(t);
}
This is working fine.
Now in Form2 I have a menu item which when clicked will open new tab. Its code is as follows:
public void addTabToolStripMenuItem_Click(object sender, EventArgs e)
{
TabPage t = new TabPage();
Form2 newtab = new Form2();
newtab.Show();
newtab.TopLevel = false;
newtab.Dock = DockStyle.Fill;
t.Controls.Add(newtab);
Form1 b = new Form1();
b.tabControl1.TabPages.Add(t);
}
But I don't get a new tab. i.e. When I click the menu item nothing happens.

You are creating a new Form1 in your click Handler, it is not the Form1 that you used to create the instance of Form2 in. Your best bet would be to use the Show method that allows you to set the owning form and as long as your TabControl's Modifier is public it will work. This is IMHO really not the way to go. I would look into creating an event and subscribe to that.
public void Form1_Load(object sender, EventArgs e)
{
TabPage t = new TabPage();
Form2 newtab = new Form2();
newtab.Show(this); //note the change here
newtab.TopLevel = false;
newtab.Dock = DockStyle.Fill;
t.Controls.Add(newtab);
tabControl1.TabPages.Add(t);
}
This will enable you to do this:
((Form1)this.Owner).tabControl1.TabPages.Add(t);

Related

Not able to show a winform after removing from splitter panel

I have 2 forms (Form1 and Form2). Form1 has a splitter with two panels. I have added Form2 in the panel2 of the splitter control. I want to pop in and pop out Form2 without creating a new instance of Form2. Please find the code snippet below:
public partial class Form1 : Form
{
private Form2 form2 = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
form2 = new Form2();
form2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form2.Dock = DockStyle.Fill;
form2.TopLevel = false;
splitContainer1.Panel2.Controls.Add(form2);
form2.Pop += new EventHandler(PopForm);
form2.Show();
}
//button click event handler from Form2
private void PopForm(object sender, EventArgs e)
{
Button b = sender as Button;
if(b.Text.ToUpper() == "POPOUT")
{
splitContainer1.Panel2Collapsed = true;
splitContainer1.Panel2.Controls.Remove(form2);
//need to show the form without creating a new instance to maintain state
form2 = new Form2();
form2.SelectedMailId = 1;
form2.Pop += new EventHandler(PopForm);
form2.SetButtonText = "PopIn";
form2.Show();
}
else
{
//this works fine
splitContainer1.Panel2Collapsed = false;
form2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form2.Dock = DockStyle.Fill;
form2.TopLevel = false;
splitContainer1.Panel2.Controls.Add(form2);
}
}
}
How can I show the Form2 without creating a new instance when popping out?
While popout setup the form border styled and toplevel
if(b.Text.ToUpper() == "POPOUT")
{
splitContainer1.Panel2Collapsed = true;
splitContainer1.Panel2.Controls.Remove(form2);
//need to show the form without creating a new instance to maintain state
form2.TopLevel = true;
form2.FormBorderStyle = FormBorderStyle.Sizable;
// setup your settings
form2.Show();
}

How to change button image?

How can I change the image of a button from form 3 to form 2? I use those buttons to navigate between forms, and want to, when I click on button in form 3, to get back to form 2, to have changed the picture of button in form 2.
This is how I navigate between forms.
FORM2 BUTTON
private void button1_Click(object sender, EventArgs e)
{
Form3 m = new Form3();
m.Show();
this.Visible = false;
this.Hide();
}
FORM3 BUTTON
private void button14_Click(object sender, EventArgs e)
{
Form2 m = new Form2();
m.Show();
this.Visible = false;
this.Hide();
}
In your button on FORM 3, do something like this:
private void button14_Click(object sender, EventArgs e)
{
Form2 m = new Form2();
m.button1.BackgroundImage = Image.FromFile(#"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg");
m.Show();
this.Visible = false;
this.Hide();
}
In order for this to work, the button1 on your FORM 3 must be accessible. So go to FORM 3 and mark button1 as public.

Create new form on same screen position

I want to create a new form on the same location. When I call this code a new form opens but on a different screen position.
private void BtnAddForm_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Tag = this;
form2.Location = this.Location;
form2.Show(this);
Hide();
}
I used this.Location to get the location from my first form but this has no effect.
You need to set StartPosition property to Manual for this to work.
private void BtnAddForm_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Tag = this;
form2.StartPosition = FormStartPosition.Manual;
form2.Location = this.Location;
form2.Show(this);
Hide();
}
Use this. Hope helps
private void BtnAddForm_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show(this);
form2.Tag = this;
form2.Location = this.Location;
Hide();
}
Use form2.StartPosition = FormStartPosition.Manual;, You should also use form2.Closed += (s, args) => this.Close(); to close parent form after hide it Try this:
Hide();
Form2 form2 = new Form2();
form2.Tag = this;
form2.StartPosition = FormStartPosition.Manual;
form2.Location = this.Location;
form2.Closed += (s, args) => this.Close();
form2.Show();
The easiest way is by using the the StartPosition property of the form. This property should be set before the form is shown. You can set this property before you call the Show or ShowDialog method or in your form's constructor.
For example:
form2.StartPosition = FormStartPosition.CenterParent
private void button5_Click(object sender, EventArgs e)
{
Form1 NewForm = new Form1();
NewForm.Show();
NewForm.Location = this.Location;
this.Dispose(false);
}
This is a pretty simple one.

Closing three forms form button

I have the following code:
in form1
private void button6_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
Form Form5 = new Form5();
Form Form6 = new Form6();
Form4.Show();
Form5.Show();
Form6.Show();
}
in form5 i have a button that must close form4, form5 and form6. as following:
private void button2_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
Form Form6 = new Form6();
Form4.Close();
Form6.Close();
this.Close();
}
but Form4 and Form6 are still open!!!
private void button2_Click(object sender, EventArgs e)
{
Form4 form4 = (Form4) Application.OpenForms["Form4"];
Form5 form5 = (Form5) Application.OpenForms["Form5"];
form4.Close();
form5.Close();
}
To close in proper way you have to know what do you want to close. In your case you are creating new Forms on beggining, and another ones with the same names visible locally before trying to destroy them.
Form Form6 = new Form6(); are totally different in both of your clicked buttons. Then, if you want to make it more visible generate interesting forms in constructor method and put definition as class fields like.
public partial class Form1 : Form
{
Form form2; // be sure all componentes see all forms
Form form3;
Form form4;
public Form1()
{
InitializeComponent();
form2 = new Form(); // create new Forms
form3 = new Form();
var button = new Button();
button.Click += new EventHandler(button_Click); // tell the button what should be called when click
form4 = new Form();
form4.Controls.Add(button); // add button progrimicaly to form
}
void button_Click(object sender, EventArgs e)
{
form2.Hide(); // hide on click
form3.Hide();
form4.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
form2.Show(); // show on load
form3.Show();
form4.Show();
}
}

LinkLabel doesn't open

I'm creating LinkLabel in Form2 and Form3 with the same code. Form2 and Form3 are separate classes so the names don't interferer. They are both created, but in the Form 3 links open, in the Form2 nothing happens.
This is the code for Form2
public partial class Form2 : Form
{
public void createFormEntry(List<string> videoId)
{
LinkLabel link = new LinkLabel();
link.Name = "link";
link.AutoSize = true;
link.Location = new Point(76, 8);
link.Text = "www.example.com";
link.LinkClicked += new LinkLabelLinkClickedEventHandler(link_LinkClicked);
this.Controls.Add(link);
}
private void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://www.example.com");
}
}
And this is for the Form3
public partial class Form3 : Form
{
private void createFormEntry(Feed<Video> videoFeed)
{
LinkLabel link = new LinkLabel();
link.Name = "link";
link.AutoSize = true;
link.Location = new Point(76, 8);
link.Text = "www.example.com";
link.LinkClicked += new LinkLabelLinkClickedEventHandler(link_LinkClicked);
this.Controls.Add(link);
}
private void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://www.example.com");
}
}
They are in different classes. Form2 opens before Form3.
What could be wrong?
EDIT: now when I added more code, I see in Form2 createFormEntry is public and in Form3 it is set as private.
Could that be a reason?
Your trying to open a link without telling the program how or what to open that link in. You should tell it to search it in a program or something!

Categories

Resources