Create a new label if button from second form is pressed - c#

I'm making a program that supposed to create a label on the second form when a button from that second form is pressed, I successfully added the button on the second form now that I'm struggling with making the label to be added into the second form when that button is pressed because I don't know how to do that, this is my code:
void Button1Click(object sender, EventArgs e)
{
Form form2 = new Form();
form2.Size = new Size(350,350);
// Setup
Button finish = new Button();
finish.Text = "Finish";
finish.Location = new Point(x,100);
// Utilize
form2.Controls.Add(finish);
form2.Text = "Second Form";
form2.Show();
}
I have done googling and searching through stackoverflow ended up with no solution.

This works in my small Windows Forms sample:
Form form2 = new Form();
form2.Size = new Size(350, 350);
// Setup
Button finish = new Button();
finish.Text = "Finish";
finish.Location = new Point(100, 100);
finish.Click += (s, e) =>
{
Label label = new Label();
label.Text = "Finish was clicked";
label.Location = new Point(10, 10);
label.Width = 300;
form2.Controls.Add(label);
};
// Utilize
form2.Controls.Add(finish);
form2.Text = "Second Form";
form2.Show();

Related

I need to add a DataGridView to the form dynamically

private void btnShowCausali_Click(object sender, EventArgs e)
{
DataGridView Dati = new DataGridView();
Dati.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
Dati.Location = new System.Drawing.Point(120, 40);
Dati.Name = "Dati";
Dati.RowTemplate.Height = 24;
Dati.Size = new System.Drawing.Size(979, 458);
Dati.TabIndex = 1;
Dati.Visible = true;
Dati.Columns.Add("id", "ID");
Dati.Columns.Add("causaliname", "Nome Causale");
Dati.Columns.Add("Identificationcode", "Codice Identificativo");
Dati.Columns.Add("expired", "Data di Scadenza");
grpDatore.Controls.Add(Dati);
}
When I run this code the DataGridView does not appear on the form. Just in case it could create problem, the button btnShowCausali get created on the form load.
public void Form1_Load(object sender, EventArgs e)
{
Button btnShowCausali = new Button();
btnShowCausali.Text = "Causali";
btnShowCausali.Location = new Point(20, 20);
btnShowCausali.Size = new Size(120, 40);
}
By the way, I don't know why the button actually gets created but the DataGridView does not.
Your event handler btnShowCausali_Click is not attached to the button in Form1_Load. Are you sure it is called?
I also do not see adding this button to any container (Form, Panel, ...)
Your DataGridView will be added to grpDatore control (not form) so have that in mind when you set Location.
Attach event:
btnShowCausali.Click += btnShowCausali_Click;
First of all, if the button is created dynamically, add it to the UI.
Button btnShowCausali = new Button();
btnShowCausali.Text = "Causali";
btnShowCausali.Location = new Point(20, 20); // Make shure there aren't other controls in this point
btnShowCausali.Size = new Size(120, 40);
this.Controls.Add(btnShowCausali); //Adding the button to the form
Then, you have to attach an event handler for the Click event of the button. In short, you have to tell to the button what to do when it is clicked. You have two options to add an event handler for the click event:
Code: add btnShowCausali.Click += btnShowCausali_Click; after the InitializeComponent function call (in the constructor) or in the Load event of the form. Then add the function btnShowCausali_Click:
private void btnShowCausali_Click(object sender, EventArgs e)
{
DataGridView Dati = new DataGridView();
Dati.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
Dati.Location = new System.Drawing.Point(120, 40);
Dati.Name = "Dati";
Dati.RowTemplate.Height = 24;
Dati.Size = new System.Drawing.Size(979, 458);
Dati.TabIndex = 1;
Dati.Visible = true;
Dati.Columns.Add("id", "ID");
Dati.Columns.Add("causaliname", "Nome Causale");
Dati.Columns.Add("Identificationcode", "Codice Identificativo");
Dati.Columns.Add("expired", "Data di Scadenza");
grpDatore.Controls.Add(Dati); // DataGridView added to grpDatore, not form. Make shure grpDatore is visible.
}
Designer: double click on the button. Visual Studio will do the magic for you.

How to create drop down information box in C# Winforms?

I want to make a button that can drop down a multi-line label or form which contains help documentation for the user.
I have searched and I can't find anything that is for C# Winforms. Do any free controls out there exist for this or will I have to create it myself?
Many thanks,
Richard
Using ToolStripControlHost and ToolStripDropDown controls can provide this for you:
private void button1_Click(object sender, EventArgs e) {
var helpInfo = new StringBuilder();
helpInfo.AppendLine("This is line one.");
helpInfo.AppendLine("This is line two.");
var textHelp = new TextBox() { Multiline = true,
ReadOnly = true,
Text = helpInfo.ToString(),
MinimumSize = new Size(100, 100)
};
var toolHost = new ToolStripControlHost(textHelp);
toolHost.Margin = new Padding(0);
var toolDrop = new ToolStripDropDown();
toolDrop.Padding = new Padding(0);
toolDrop.Items.Add(toolHost);
toolDrop.Show(button1, button1.Width, 0);
}
Result:
I think it will be a bad user experience to see a tooltip on click of a button. However, you can use this if you really want to
var b = new Button();
b.Click += (sender, args) => new ToolTip().Show("Help documentation", b.Parent, new Point(b.Location.X, b.Location.X + 10));

Add controls to another Winform programatically C# .NET

I am having mother form now, I want to create a new form programatically. I created the new form but I couldn't add controls to the form.
private void CreateWindows()
{
newWindow = new Form();
Application.Run(newWindow);
newWindow.Activate();
newWindow.Size = new System.Drawing.Size(40, 40);
Label label1 = new Label();
newWindow.Controls.Add(label1);
label1.Text = "HI";
label1.Visible = true;
label1.Size = new System.Drawing.Size(24, 24);
label1.Location = new System.Drawing.Point(24, 24);
}
I have tried the codes above, the new form showed but I couldn't see the label1.
I appreciate any helps.
Try putting the add controls after setting up the properties of label and then Show the new window.
private void CreateWindows()
{
newWindow = new Form();
newWindow.Activate();
newWindow.Size = new System.Drawing.Size(40, 40);
Label label1 = new Label();
label1.Text = "HI";
label1.Visible = true;
label1.Size = new System.Drawing.Size(24, 24);
label1.Location = new System.Drawing.Point(24, 24);
newWindow.Controls.Add(label1);
newWindow.Show();
//use this if you want to wait for the form to be closed
//newWindow.ShowDialog();
}
First: add the controls to newWindow.Controls.
Second: Do it before Application.Run because it will show the form and then wait for it to close (note: the way the designer does it is to add them at the constructor of a class that derived from Form).
private void CreateWindows()
{
newWindow = new Form();
//Application.Run(newWindow); //Not here
//newWindow.Activate(); //Wont do anything
newWindow.Size = new System.Drawing.Size(40, 40);
Label label1 = new Label();
newWindow.Controls.Add(label1); //Good
label1.Text = "HI";
label1.Visible = true;
label1.Size = new System.Drawing.Size(24, 24);
label1.Location = new System.Drawing.Point(24, 24);
Application.Run(newWindow); //Here instead
}
Third: if you have already used Application.Run in the current thread (say because you are doing this from a form), then there is no point to call it here. Use Show or ShowDialog instead.
Also consider adding controls this way:
private void CreateWindows()
{
newWindow = new Form();
newWindow.Size = new System.Drawing.Size(40, 40);
newWindow.Controls.Add
(
new Label()
{
Text = "HI",
Visible = true,
Size = new System.Drawing.Size(24, 24),
Location = new System.Drawing.Point(24, 24)
}
);
Application.Run(newWindow);
}

Show a link to a path in a message form in c#

I have a desktop application in C# .In this I have a form in which I show different messages .
I have one message who say : "The output file was generated in : C:\Work\result.txt".
How can I show this path to the file as a link and when the form with this message is shown to see the path as a link and when the user click the link to open the specified path/file?
I tried :
The output file was generated in : C:\Work\result.txt
But doesn't work.
Thanks !
You can have an event for on-onclick, and then you can open file using the below code.
System.Diagnostics.Process.Start(#"C:\Work\result.txt"); //or like
System.Diagnostics.Process.Start(#"C:\Work\result.docx");
Here, the default program must be there for the file. Then only shell will run associated program reading it from the registry, like usual double click does in explorer.
MessageBox.Show() method takes the caption, text, icon, buttons and default button of the dialog.However there's nothing mentioned in the .NET Framework documentation that says anything about adding links to a MessageBox
However, you can do the effect you want by creating a new class inherited from System.Windows.Forms.Form and add a button (or more if you like), an icon, a label and a LinkButton. Then use the ShowDialog() Method of the Form class to display the message box in modal form. You may also create a class called MyErrorBox (static class in C# 2 or just sealed in C# 1) that contains only one static method called Show() which creates a form, adds the needed controls and displays the form in modal mode. A demonstration of the last method is shown below. Then you can use this class whenever you want and wherever you please!
using System;
using System.Windows.Forms;
using System.Drawing;
namespace MessageBoxes{
public sealed class MyErrorBox{
private MyErrorBox(){}
private static Form frm;
private static string detailsStore;
private static TextBox txt;
public static DialogResult Show(string caption, string text, string details, Icon icon){
frm = new Form(); frm.Size = new Size(510, 195);
frm.Text = caption; frm.ShowInTaskbar = false; frm.ControlBox = false;
frm.FormBorderStyle = FormBorderStyle.FixedDialog;
PictureBox icon1 = new PictureBox(); icon1.Location = new Point(8,16);
icon1.Size = new Size(icon.Width, icon.Height);
icon1.Image = icon.ToBitmap();
frm.Controls.Add(icon1);
Label lbl = new Label(); lbl.Text = text; lbl.Location = new Point(88,8);
lbl.Size = new Size(400,88); frm.Controls.Add(lbl);
LinkLabel btn1 = new LinkLabel(); btn1.Text = "View Details";
btn1.Size = new Size(72,23); btn1.Location = new Point(416,96);
btn1.Click += new EventHandler(btn1_Click); frm.Controls.Add(btn1);
//Ofcourse you can add more buttons than just the ok with more DialogResults
Button btn2 = new Button(); btn2.Text = "&Ok";
btn2.Size = new Size(72,23); btn2.Location = new Point(224,130);
btn2.Anchor = AnchorStyles.Bottom; frm.Controls.Add(btn2);
frm.AcceptButton = btn2; btn2.Click += new EventHandler(btn2_Click);
btn2.DialogResult = DialogResult.OK; detailsStore = details;
return frm.ShowDialog();
}
private static void btn1_Click(object sender, EventArgs e) {
frm.Size = new Size(510,320);
txt = new TextBox(); txt.Multiline = true;
txt.ScrollBars = ScrollBars.Both; txt.Text = detailsStore;
txt.Size = new Size(488,128); txt.Location = new Point(8,120);
txt.ReadOnly = true; frm.Controls.Add(txt);
LinkLabel lnk = (LinkLabel)(sender); lnk.Text = "Hide Details";
lnk.Click -= new EventHandler(btn1_Click);
lnk.Click += new EventHandler(btn1_ReClick);
}
private static void btn2_Click(object sender, EventArgs e) {
frm.Close();
}
private static void btn1_ReClick(object sender, EventArgs e) {
frm.Controls.Remove(txt); frm.Size = new Size(510, 195);
LinkLabel lnk = (LinkLabel)(sender); lnk.Text = "View Details";
lnk.Click -= new EventHandler(btn1_ReClick);
lnk.Click += new EventHandler(btn1_Click);
}
}
}
There's no standard MessageBox functionality that'll be able to do this via link label. What I suggest is you use the Yes/No Messagebox buttons and from the option selected you then apply an event
Something like this:
if (MessageBox.Show(
"The file is saved at the following link: link here", "Success", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk
) == DialogResult.Yes)
{
System.Diagnostics.Process.Start(#"C:\TestLocation\SavedFiles");
}

Create a button which creates button

I am a begginer in C# and I want to create a button which creates button.
but these buttons never appear...
please find my code :
private void addstrat3_i_Click(object sender, EventArgs e)
{
panel3strat.Width += 200;
Button addstrat3_2 = new Button();
addstrat3_2.Size = new Size(210, 41);
addstrat3_2.Location = new Point(50,50);
addstrat3_2.Visible = true;
}
Thanks a lot
You have to add the button (or any other controls) on the form using the Controls property, for sample:
private void addstrat3_i_Click(object sender, EventArgs e)
{
panel3strat.Width += 200;
Button addstrat3_2 = new Button();
addstrat3_2.Size = new Size(210, 41);
addstrat3_2.Location = new Point(50,50);
addstrat3_2.Visible = true;
// add control
this.Controls.Add(addstrat3_2);
}
You need to add the button to the form.
this.Controls.Add(addstrat3_2);

Categories