LinkLabel doesn't open - c#

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!

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();
}

Rename tab from added form

My problem is that I have inserted a tab to a tabControl and added a Form to it (basically I wanted to display all the forms I was going to open as tabs).
Code for adding form to tabControl as tab in the Main class:
private void new_form_Click(object sender, EventArgs e)
{
add = new Add(null);
add.TopLevel = false;
add.Visible = true;
add.FormBorderStyle = FormBorderStyle.None;
add.Dock = DockStyle.Fill;
var tabIndex = tabControl1.TabCount;
tabControl1.TabPages.Insert(tabIndex, "New Tab");
tabControl1.SelectedIndex = tabIndex - 1;
tabControl1.TabPages[tabIndex - 1].Controls.Add(add);
}
When I was using multiple forms it was easy to rename the title from the form class:
private void surname_Leave(object sender, EventArgs e)
{
this.Text = surname.Text;
}
How can I rename the tab programmatically from inside the added form?
Edit:
I know how to rename a tab from the same class. I need to rename the tab from the form I open in the tab.
What I was trying to accomplish I achieved by passing the reference of one to another form.
in Form2:
public Form2()
{
InitializeComponent();
}
private Form1 mainForm;
public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}
private void surname_Leave(object sender, EventArgs e)
{
mainForm.setTabTitle = surname.Text;
}
in Form1:
private void new_form_Click(object sender, EventArgs e)
{
add = new Add(this); \\this part was missing
add.TopLevel = false;
add.Visible = true;
add.FormBorderStyle = FormBorderStyle.None;
add.Dock = DockStyle.Fill;
var tabIndex = tabControl1.TabCount;
tabControl1.TabPages.Insert(tabIndex, "New Tab");
tabControl1.SelectedIndex = tabIndex - 1;
tabControl1.TabPages[tabIndex - 1].Controls.Add(add);
}
public string setTabTitle
{
set { tabControl1.TabPages[tabControl1.SelectedIndex].Text = value; }
}

Not getting New Tab in WebBrowser Visual Studio 2012

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);

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();
}
}

Winforms Controlling Forms

How can i control all my forms from main ()
Form1 frm1 = new Form1();
Form1 frm2 = new Form1();
Form1 frm3 = new Form1();
Application.Run(frm1); // This way form-control goes to the frm1.
// In frm1 i have to write
frm1.Clicked += ()=>frm2.Show;
// I want the form-controlling style more explicitly
// I dont want to use Application.Run()
frm1.Show();
frm1.Clicked += frm2.Show();
form.ShowDialog () helps much but the execution stack can overflow.
Form.Show and Form.Hide methods runs when an application class has been set.
In Application.Run (Form) way there's always a main form. and i dont want this one. Any other approach you use in this problem
Your problem is, that you have four forms. All of them should exist side by side, but because you made Form1 to the master you got some problems.
To solve this you need another FormMaster above all four of them. This one will be started from Application.Run(). Now this form can be Visible = false, but in its constructor you can create all your four forms and decide how they will be glued together, which one will be shown first and under which circumstances your whole application will be closed.
The usual way is to use event handlers.
All I can understand is that you have several WinForms and you want some main Form to control them? Well, if I understanding/assumption is correct, then about controlling like following?
public partial class Form3 : Form
{
private void Form3_Load(object sender, EventArgs e)
{
Demo();
}
MyMainForm main = new MyMainForm(); //Your actual form
private void Demo()
{
main.Click += new EventHandler(main_Click);
main.ShowDialog();
}
void main_Click(object sender, EventArgs e)
{
MyNotificationForm notify = new MyNotificationForm();//Your notification form
notify.Name = "notify";
notify.Click += new EventHandler(notify_Click);
notify.ShowDialog(main);
}
void notify_Click(object sender, EventArgs e)
{
MyWarningForm warning = new MyWarningForm();//Your warning form
warning.Click += new EventHandler(warning_Click);
warning.ShowDialog(main.ActiveMdiChild);
}
void warning_Click(object sender, EventArgs e)
{
((Form)sender).Close(); //Click on form would close this.
}
}
Following is how I'd implement the classes.
public class CBaseForm : Form
{ public CBaseForm() { this.Text = "Main App"; } }
public class MyWarningForm : CBaseForm
{ public MyWarningForm() { Label lbl = new Label(); lbl.Text = "Warning Form"; this.Controls.Add(lbl); } }
public class MyNotificationForm : CBaseForm
{ public MyNotificationForm() { Label lbl = new Label(); lbl.Text = "Notification Form"; this.Controls.Add(lbl); } }
public class MyMainForm : CBaseForm
{ public MyMainForm() { Label lbl = new Label(); lbl.Text = "Controller Form"; this.Controls.Add(lbl); } }
And you MainForm would start conventionally
Application.Run(new Form3());
Let me know if I dragged your question to 180 degrees!

Categories

Resources