Adding C# form into another form - c#

I need to add the form into the panel of the another parent form, but that is not working. When I try to add within the main parent form. I have a dashboard form and when I click the button a sidebar for gets open into one of the panels and again I need to load the form into the second panel of the dashboard form when I click on the button of the sidebar form.
public void btn_add_Click(object sender, EventArgs e)
{
Admin_Dashboard frm = new Admin_Dashboard();
Brand.Add_Brand myform = new Brand.Add_Brand();
myform.FormBorderStyle = FormBorderStyle.None;
myform.TopLevel = false;
myform.AutoScroll = true;
frm.content.Controls.Clear();
frm.content.Controls.Add(myform);
myform.Show();
}

This is what you do:
Make a custom control, which everything that the form has.
Change the form so it only has the custom control.
In the panel where you want the form... you put the custom control.
Bonus chatter: you can create a class to represent all the information that the custom control has to show. Then you can pass it around to show it both in the form and in the panel. Oh, and that class is a we call a view model.

I'm not sure what frm.content.Controls is, but this works...
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
class Form1 : Form
{
private Panel panel1;
private Button button1;
public Form1()
{
this.panel1 = new Panel();
this.button1 = new Button();
this.SuspendLayout();
this.panel1.Location = new System.Drawing.Point(305, 51);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(457, 338);
this.panel1.TabIndex = 0;
this.button1.Location = new System.Drawing.Point(321, 9);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.FormBorderStyle = FormBorderStyle.None;
form2.TopLevel = false;
form2.AutoScroll = true;
panel1.Controls.Clear();
panel1.Controls.Add(form2);
form2.Show();
}
}
class Form2 : Form
{
private Button button1;
public Form2()
{
this.button1 = new Button();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(321, 9);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Yellow;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
}
}
Am I missing something?

Related

Form bigger than it supposed to be

I'm just trying to create a very small form with an "X" button.
when I execute the whole thing, it's bigger than it supposed to be:
On the visual studio design editor:
But when running, this shows up:
The designer code:
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.Color.Black;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button1.ForeColor = System.Drawing.Color.Gray;
this.button1.Location = new System.Drawing.Point(6, 7);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(27, 26);
this.button1.TabIndex = 0;
this.button1.Text = "X";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form3
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.ClientSize = new System.Drawing.Size(40, 40);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Form3";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.ResumeLayout(false);
}
What have I done wrong?
You can see the below designer code as you were missing to set some of the properties for the button as well as the form.
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.Color.Black;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("Arial", 12F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((byte)(0)));
this.button1.ForeColor = System.Drawing.Color.Gray;
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.AutoSize = true; //you were missing this for the button
this.button1.Size = new System.Drawing.Size(27, 26);
this.button1.TabIndex = 0;
this.button1.Text = "X";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true; //you were missing this for the form
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;//also this
this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.ClientSize = new System.Drawing.Size(40, 40);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.ResumeLayout(false);
}
Please check the designer code above and it works.
I think you should try to set the Size.Width and Size.Height of the form manually to whatever you want it to be.
Hope it helps!

can't insert webbrowser inside panel in c#winapp

I am unable to add webbrowser control to panel in winapp,getting this error"Unable to get the window handle for the 'WebBrowser' control. Windowless ActiveX controls are not supported."i also tried to do it using STAthread like
Thread uy = new Thread(me_p);
uy.SetApartmentState(ApartmentState.STA);
uy.Start();
private void me_p(object obj)
{
//throw new NotImplementedException();
this.p_bottom.Controls.Add(this.webBrowser2);
}
It works fine when done this way
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.webBrowser1);
this.panel1.Location = new System.Drawing.Point(47, 149);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 100);
this.panel1.TabIndex = 0;
//
// webBrowser1
//
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.Location = new System.Drawing.Point(0, 0);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(200, 100);
this.webBrowser1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.WebBrowser webBrowser1;
You can have a browser pre initialized and later use it in your threads.

How to open a C# windows form in another correctly by clicking a button?

![enter image description here][1]I want to design a c# windows form which when user clicks a button, a new form opens and gets some values. Then I use that values in parent form.
But when I start the program and click the button, Visual Studio opens a blank win form, while I expected it opens the child form that I designed before.
So what is the reason? I can't find any solutions. What is your ideas?
Here are the codes:
Form1
private void button1__Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Date_Time
{
public partial class Form2 : Form
{
private Label label1;
private Label label2;
private Label label3;
private TextBox txtYear;
private TextBox txtMonth;
private Button btnOk;
private TextBox txtDay;
public void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.txtYear = new System.Windows.Forms.TextBox();
this.txtMonth = new System.Windows.Forms.TextBox();
this.txtDay = new System.Windows.Forms.TextBox();
this.btnOk = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(91, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Change in Years: ";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(13, 36);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(99, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Change in Months: ";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(13, 62);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(88, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Change in Days: ";
//
// txtYear
//
this.txtYear.Location = new System.Drawing.Point(109, 6);
this.txtYear.Name = "txtYear";
this.txtYear.Size = new System.Drawing.Size(100, 20);
this.txtYear.TabIndex = 3;
//
// txtMonth
//
this.txtMonth.Location = new System.Drawing.Point(109, 33);
this.txtMonth.Name = "txtMonth";
this.txtMonth.Size = new System.Drawing.Size(100, 20);
this.txtMonth.TabIndex = 4;
//
// txtDay
//
this.txtDay.Location = new System.Drawing.Point(109, 59);
this.txtDay.Name = "txtDay";
this.txtDay.Size = new System.Drawing.Size(100, 20);
this.txtDay.TabIndex = 5;
//
// btnOk
//
this.btnOk.ImageKey = "(none)";
this.btnOk.Location = new System.Drawing.Point(73, 85);
this.btnOk.Name = "btnOk";
this.btnOk.Size = new System.Drawing.Size(75, 23);
this.btnOk.TabIndex = 6;
this.btnOk.Tag = "";
this.btnOk.Text = "&Ok";
this.btnOk.UseVisualStyleBackColor = true;
//
// Options
//
this.ClientSize = new System.Drawing.Size(238, 120);
this.Controls.Add(this.btnOk);
this.Controls.Add(this.txtDay);
this.Controls.Add(this.txtMonth);
this.Controls.Add(this.txtYear);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Options";
this.Text = "Options";
this.ResumeLayout(false);
this.PerformLayout();
}
}
}
From a glance, the thing that stands out to me is your second form is defined as so:
public partial class Options : Form
{
//code
}
But when you try to show it to the user, you are using a Form2 class instead of an Options class. Try changing your button1_Click to the following:
private void button1__Click(object sender, EventArgs e)
{
Options opt = new Options();
opt.Show();
}
You might also want to make sure that the constructor for the Options form is calling the InitializeComponent method:
public partial class Options : Form
{
public Options()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
//Coding for your Options' Form ok button
}
}
private void button1__Click(object sender, EventArgs e)
{
Form2 newForm = new Form2();
newForm.ShowDialog();
// here you can take your parameters
int x = newForm.x;
int y = newForm.y;
}
// you should have definitions for x and y in your child form
public partial class Form2: Form
{
public int x{get; set;}
public int y{get; set;}
public Form2(){}
// do stuff
}
Sorry for taking your time friends. I found the reason finally by #user3189142's and Yorye's help. :) Thank you!
The child form(Options) was not complete. It was missing following code:
public Options()
{
InitializeComponent();
}
Thanks all.

How to compile and add the form designed by user to a project

I am going to let users create Forms during RunTime and add them to a project. I have done the designing and the UI of the form with the help of an open source Form Designer.
Here is the image of the Form Designer:
Lets assume I have the Form1.cs and Form1.cs[Designer] files which are enough for a WinForm. But how do I compile it to a DLL or an EXE and add it to the project? Any ideas? Any clues?
Thanks!!
EDIT
It creates this code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.CheckBox checkBox1;
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(71, 49);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseCompatibleTextRendering = true;
this.button1.UseVisualStyleBackColor = true;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(71, 94);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 1;
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(38, 184);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(104, 24);
this.checkBox1.TabIndex = 2;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseCompatibleTextRendering = true;
this.checkBox1.UseVisualStyleBackColor = true;
//
// form1
//
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "form1";
this.ResumeLayout(false);
this.PerformLayout();
}
}
I will do some extra coding to the rest of the code myself. but I dont know how to compile it and add it to the main exe?
The clue is System.CodeDom.Compiler
i think Form1.cs will contain no code (other then InitializeComponents call in constructor). so if you merge it with the designer code with simple string operations your job will be easier.

My C# Windows Form Application is blank?

I'm Making A windows form application , And everytime I Excute it , All i see is a blank form , My project is named frmMain.cs and this is the header that i use :
using System;
using System.Windows.Forms;
public class frmMain : Form
{
private TextBox txtYear;
private Button btnCheck;
private Button btnClose;
private Label label1;
#region windows code
private void IntitializeComonent()
{
}
#endregion
public frmMain()
{
IntitializeComonent();
}
[STAThread]
public static void Main()
{
frmMain main = new frmMain();
Application.Run(main);
}
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtYear = new System.Windows.Forms.TextBox();
this.btnCheck = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(25, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(98, 20);
this.label1.TabIndex = 0;
this.label1.Text = "Year To Test: ";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtYear
//
this.txtYear.Location = new System.Drawing.Point(129, 24);
this.txtYear.Name = "txtYear";
this.txtYear.Size = new System.Drawing.Size(100, 20);
this.txtYear.TabIndex = 1;
//
// btnCheck
//
this.btnCheck.Location = new System.Drawing.Point(25, 93);
this.btnCheck.Name = "btnCheck";
this.btnCheck.Size = new System.Drawing.Size(75, 23);
this.btnCheck.TabIndex = 2;
this.btnCheck.Text = "Leap Year?";
this.btnCheck.UseVisualStyleBackColor = true;
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(154, 93);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 23);
this.btnClose.TabIndex = 3;
this.btnClose.Text = "&Close";
this.btnClose.UseVisualStyleBackColor = true;
//
// frmMain
//
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnCheck);
this.Controls.Add(this.txtYear);
this.Controls.Add(this.label1);
this.Name = "frmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Determain a Leap Year";
this.Load += new System.EventHandler(this.frmMain_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
private void frmMain_Load(object sender, EventArgs e)
{
}
}
I also have a habit that i delete program.cs and frmMain.Designer.cs because i dont need them in my program , Does that have anything to do with my problem ? Help Please!
In frmMain() you're calling InitializeComonent() (which is empty) instead of calling InitializeComponent(). Probably just a typo.
Your code is fine. Just a small typo
public frmMain()
{
// In your constructor change 'IntitializeComonent' to 'InitializeComponent'
IntitializeComonent();
}

Categories

Resources