Add controls to another Winform programatically C# .NET - c#

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

Related

Create a new label if button from second form is pressed

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

C# can't add dynamically panel

I try to add a panel to Form, but it never appears. But When I change its type e.g. on TextBox it apears. Anyone know why?
HidePanel = new Panel();
HidePanel.ForeColor = Color.Red;
HidePanel.BackColor = Color.Green;//Form.BackColor;
HidePanel.Location = new System.Drawing.Point(531, 181);
HidePanel.Name = "HidePanel";
HidePanel.Size = new System.Drawing.Size(200, 100);
HidePanel.Visible = true;
HidePanel.TabIndex = 12;
HidePanel.BringToFront();
Form.Controls.Add(HidePanel);
you used Form and it's not true, you should use this instead of Form, try this code.
HidePanel = new Panel();
HidePanel.ForeColor = Color.Red;
HidePanel.BackColor = Color.Green;//Form.BackColor;
HidePanel.Location = new System.Drawing.Point(531, 181);
HidePanel.Name = "HidePanel";
HidePanel.Size = new System.Drawing.Size(200, 100);
HidePanel.Visible = true;
HidePanel.TabIndex = 12;
HidePanel.BringToFront();
this.Controls.Add(HidePanel);
update:
Form2 frm = new Form2();
Panel HidePanel = new Panel();
HidePanel.ForeColor = Color.Red;
HidePanel.BackColor = Color.Green;//Form.BackColor;
HidePanel.Location = new System.Drawing.Point(531, 181);
HidePanel.Name = "HidePanel";
HidePanel.Size = new System.Drawing.Size(200, 100);
HidePanel.Visible = true;
HidePanel.TabIndex = 12;
HidePanel.BringToFront();
frm.Controls.Add(HidePanel);
frm.Show();
I put this code in click event of button1, which declared in form1.

Menu strip is not shown in form designer, even though its code exists

Last night, I was working on my project in C# by visual studio 2012. Suddenly I encountered a few errors from visual studio and then menu strip went into hiding. Now I haven't menu strip in my form and I lost all it visual option, but I have all it code in my formdesigner.cs file. I can't make all option again because it is hard and Time-consuming and I must create a menu strip by new names.and create all sub items by new names.
How I can resume my lost menu strip to form?
This is a part of my designer code:
this.Main = new System.Windows.Forms.ToolStripMenuItem();
this.userOptionTtm = new System.Windows.Forms.ToolStripMenuItem();
this.changePasswprdTSM = new System.Windows.Forms.ToolStripMenuItem();
this.CalenderOption = new System.Windows.Forms.ToolStripMenuItem();
this.CalenderOption2 = new System.Windows.Forms.ToolStripMenuItem();
this.CalenderOption1 = new System.Windows.Forms.ToolStripMenuItem();
this.hollydays = new System.Windows.Forms.ToolStripMenuItem();
this.ExitTsm = new System.Windows.Forms.ToolStripMenuItem();
this.useroption = new System.Windows.Forms.ToolStripMenuItem();
this.ReportsTSM = new System.Windows.Forms.ToolStripMenuItem();
this.loanListTsm = new System.Windows.Forms.ToolStripMenuItem();
this.FeutureJobsTSM = new System.Windows.Forms.ToolStripMenuItem();
and i have properties for all sub items of menu , that i was created (or defined ) previously. for example :
//
// Main
//
this.Main.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.userOptionTtm,
this.CalenderOption,
this.ExitTsm});
this.Main.Font = new System.Drawing.Font("Segoe UI", 9F);
this.Main.Name = "Main";
this.Main.Size = new System.Drawing.Size(62, 20);
this.Main.Text = "تنظیمات";
//
// userOptionTtm
//
this.userOptionTtm.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.changePasswprdTSM});
this.userOptionTtm.Font = new System.Drawing.Font("Segoe UI", 10F);
this.userOptionTtm.Image = global::TimeManagment.Properties.Resources._0079;
this.userOptionTtm.Name = "userOptionTtm";
this.userOptionTtm.Size = new System.Drawing.Size(165, 24);
this.userOptionTtm.Text = "تنظیمات کاربر";
this.userOptionTtm.Click += new System.EventHandler(this.chengePasswordTtm_Click_1);
and in my form code, I have all code of this menu. for example:
private void FeutureJobsTSM_Click(object sender, EventArgs e)
{
FeutureReportForm.isJobs = true;
FeutureReportForm fr = new FeutureReportForm();
fr.ShowDialog(this);
}
or
private void changePasswprdTSM_Click(object sender, EventArgs e)
{
chengePasswordForm cpf = new chengePasswordForm();
cpf.ShowDialog();
}
thx everybody. I added these lines to Form.Designer.cs file and my problem fixed.
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.menuStrip1.BackColor = System.Drawing.Color.Transparent;
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[]
{ this.Main,this.ReportsTSM });
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.menuStrip1.Size = new System.Drawing.Size(589, 24);
this.menuStrip1.TabIndex = 10;
this.menuStrip1.Text = "menuStrip1";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.Controls.Add(this.menuStrip1);
private System.Windows.Forms.MenuStrip menuStrip1;

Not able to add a System.Windows.Controls.TextBox to groupbox controls in C#

Is there any way to add a System.Windows.Controls.TextBox to GroupBox controls in C#?
I tried the following but it doesn't show up in the groupbox:
public System.Windows.Controls.TextBox textBox6 = new System.Windows.Controls.TextBox();
public System.Windows.Controls.TextBox textBox7 = new System.Windows.Controls.TextBox();
public ElementHost sumtext = new ElementHost();
public ElementHost loctext = new ElementHost();
private void Form1_Load(object sender, EventArgs e)
{
textBox6.Name = "Summary";
textBox7.Name = "Location";
textBox6.FontFamily = new System.Windows.Media.FontFamily("Microsoft Sans Serif");
textBox6.FontSize = 12;
textBox6.SpellCheck.IsEnabled = true;
textBox7.FontFamily = new System.Windows.Media.FontFamily("Microsoft Sans Serif");
textBox7.FontSize = 12;
textBox7.SpellCheck.IsEnabled = true;
groupBox4.Controls.Add(sumtext);
sumtext.Dock = DockStyle.None;
sumtext.Width = 246;
sumtext.Height = 35;
sumtext.Child = textBox6;
sumtext.Location = new Point(3, 33);
sumtext.Visible = true;
sumtext.Enabled = false;
groupBox4.Controls.Add(sumtext);
groupBox4.Controls.Add(loctext);
loctext.Dock = DockStyle.None;
loctext.Width = 246;
loctext.Height = 35;
loctext.Child = textBox7;
loctext.Location = new Point(3, 90);
loctext.Visible = true;
loctext.Enabled = false;
this.Controls.Add(sumtext);
this.Controls.Add(loctext);
}
I need to use System.Windows.Controls.TextBox rather than Form.TextBox as I need it for spell check.
Any help would be greatly appreciated!
I changed the Enabled property of the sumtext, and removed the other box to shorten it:
This code works for me:
public Form1()
{
this.Load += new System.EventHandler(this.Form1_Load);
}
public System.Windows.Controls.TextBox textBox6 = new System.Windows.Controls.TextBox();
public ElementHost sumtext = new ElementHost();
private System.Windows.Forms.GroupBox groupBox4;
private void Form1_Load(object sender, EventArgs e)
{
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.SuspendLayout();
//
// groupBox4
//
this.groupBox4.Location = new System.Drawing.Point(57, 63);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(591, 238);
this.groupBox4.TabIndex = 0;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "groupBox1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(706, 478);
this.Controls.Add(this.groupBox4);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
textBox6.Name = "Summary";
textBox6.FontFamily = new System.Windows.Media.FontFamily("Microsoft Sans Serif");
textBox6.FontSize = 12;
textBox6.SpellCheck.IsEnabled = true;
groupBox4.Controls.Add(sumtext);
sumtext.Dock = DockStyle.None;
sumtext.Width = 246;
sumtext.Height = 35;
sumtext.Child = textBox6;
sumtext.Location = new Point(3, 33);
sumtext.Visible = true;
sumtext.Enabled = true;
groupBox4.Controls.Add(sumtext);
}
Is this code actually getting called? Has groupbox 4 been added to the form yet?
You should not be adding your ElementHost controls to your Form AND your GroupBox, it appears to be confusing .NET. Keeping your original code exactly as-is but commenting out these two lines makes it work:
//this.Controls.Add(sumtext);
//this.Controls.Add(loctext);
Also... I don't think it's hurting anything, but you don't need to do this twice:
//groupBox4.Controls.Add(sumtext);

Webbrowser doesn't show scrollbars and can I add touch?

In WM6.5 a have a simple form with a webbrowser control. I open a local file and fill this page data to the document.text
1.
On some phones you see the page but no scrollbar?
2.
On my phone and in the simulators I have a scrollbar. If you put your finger on the screen and move vertically you only highlight the text. What I would like is the page to scroll down/up with this touch.
Is there a trick for this? Below is my init and the filling of the html.
public partial class FormLicense : Form
{
bool _CanClose = false;
private const string _html =
"<html><body><br><div style='top:20px;left:50px;border:solid 1px red'><a href='http://www.cnn.com'>CNN</a></div><br><font size='14'>xx</font><a href='http://stackoverflow.com'>stackoverflow</a></body></html>";
public FormLicense()
{
InitializeComponent();
this.Load += new System.EventHandler(this.FormLicense_Load);
this.Closed += new System.EventHandler(this.FormLicense_Closed);
this.Closing += new System.ComponentModel.CancelEventHandler(this.FormLicense_Closing);
this.SuspendLayout();
this.menu1L.Text = "I Accept";
this.menu1R.Text = "Don't Accept";
this.Text = "License";
using (StreamReader sr = new StreamReader(GlobalLicense.FileName, System.Text.Encoding.Default))
{
webBrowser1.DocumentText = sr.ReadToEnd();
}
.....
....
...
..
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menu1L = new System.Windows.Forms.MenuItem();
this.menu1R = new System.Windows.Forms.MenuItem();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.Add(this.menu1L);
this.mainMenu1.MenuItems.Add(this.menu1R);
//
// menu1L
//
this.menu1L.Text = "1L";
this.menu1L.Click += new System.EventHandler(this.menu1L_Click);
//
// menu1R
//
this.menu1R.Text = "1R";
this.menu1R.Click += new System.EventHandler(this.menu1R_Click);
//
// webBrowser1
//
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.Location = new System.Drawing.Point(0, 0);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.ScriptErrorsSuppressed = true;
this.webBrowser1.Size = new System.Drawing.Size(240, 268);
//
// FormLicense
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoScroll = true;
this.AutoScrollMargin = new System.Drawing.Size(10, 10);
this.ClientSize = new System.Drawing.Size(240, 268);
this.Controls.Add(this.webBrowser1);
this.Menu = this.mainMenu1;
this.Name = "FormLicense";
this.Text = "FormLicense";
this.ResumeLayout(false);
The scrollbar problem is a bug:
Link

Categories