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();
}
Related
**The autopostback-fix does not work! **
I have a GUI that looks like:
I want to show the details of a cat when it's clicked on.
I used this code for it:
private void listBox_Cats_SelectedIndexChanged(object sender, System.EventArgs e)
{
// Get the currently selected item in the ListBox.
Cat cat = listBox_Cats.SelectedItem as Cat;
if (cat != null)
{
tb_ID.Text = cat.ID.ToString();
tb_Name.Text = cat.Name.ToString();
tb_DateArrived.Text = cat.DateArrived.ToString();
tb_IsAdopted.Text = cat.IsAdopted.ToString();
tb_adoptedBy.Text = cat.AdoptedBy.ToString();
}
}
When running however, nothing seems to work. What am I doing wrong?
EDIT
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using IntegrationTool;
namespace IntegrationTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SqlConnection conn = new SqlConnection("Server=.\\SQLEXPRESS;Database=CatShelter;Integrated Security=true");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[cats]", conn);
SqlDataReader reader = cmd.ExecuteReader();
List<Cat> cats = new List<Cat>();
while (reader.Read())
{
Cat c = new Cat();
c.ID = (int)reader["CatID"];
c.Name = (string)reader["Name"];
c.DateArrived = (DateTime)reader["DateArrived"];
c.IsAdopted = (string)reader["IsAdopted"];
c.AdoptedBy = (string)reader["AdoptedBy"];
cats.Add(c);
}
foreach (Cat c in cats)
{
Console.WriteLine(c.Name);
}
reader.Close();
conn.Close();
if (Debugger.IsAttached)
{
Console.ReadLine();
}
listBox_Cats.DataSource = cats;
listBox_Cats.DisplayMember = "Name";
listBox_Cats.ValueMember = "ID";
}
private void listBox_Cats_SelectedIndexChanged(object sender, System.EventArgs e)
{
// Get the currently selected item in the ListBox.
Cat cat = listBox_Cats.SelectedItem as Cat;
if (cat != null)
{
Console.WriteLine(cat.Name);
tb_ID.Text = cat.ID.ToString();
tb_Name.Text = cat.Name.ToString();
tb_DateArrived.Text = cat.DateArrived.ToString();
tb_IsAdopted.Text = cat.IsAdopted.ToString();
tb_adoptedBy.Text = cat.AdoptedBy.ToString();
}
}
}
}
namespace IntegrationTool
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox_Cats = new System.Windows.Forms.ListBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.cb_VetCheck = new System.Windows.Forms.CheckBox();
this.label5 = new System.Windows.Forms.Label();
this.tb_Comment = new System.Windows.Forms.TextBox();
this.tb_ID = new System.Windows.Forms.TextBox();
this.tb_DateArrived = new System.Windows.Forms.TextBox();
this.tb_IsAdopted = new System.Windows.Forms.TextBox();
this.tb_adoptedBy = new System.Windows.Forms.TextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.label6 = new System.Windows.Forms.Label();
this.tb_Name = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// listBox_Cats
//
this.listBox_Cats.FormattingEnabled = true;
this.listBox_Cats.Location = new System.Drawing.Point(16, 35);
this.listBox_Cats.Name = "listBox_Cats";
this.listBox_Cats.Size = new System.Drawing.Size(137, 316);
this.listBox_Cats.TabIndex = 0;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.tb_Name);
this.groupBox1.Controls.Add(this.label6);
this.groupBox1.Controls.Add(this.tb_adoptedBy);
this.groupBox1.Controls.Add(this.tb_IsAdopted);
this.groupBox1.Controls.Add(this.tb_DateArrived);
this.groupBox1.Controls.Add(this.tb_ID);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(252, 31);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(409, 190);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Details";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(6, 35);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(24, 13);
this.label1.TabIndex = 0;
this.label1.Text = "ID: ";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(6, 95);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(69, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Date Arrived:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(6, 123);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(60, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Is adopted:";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(6, 157);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(65, 13);
this.label4.TabIndex = 3;
this.label4.Text = "Adopted By:";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.tb_Comment);
this.groupBox2.Controls.Add(this.label5);
this.groupBox2.Controls.Add(this.cb_VetCheck);
this.groupBox2.Location = new System.Drawing.Point(252, 243);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(409, 157);
this.groupBox2.TabIndex = 2;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Validate";
//
// button2
//
this.button2.Location = new System.Drawing.Point(553, 424);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(107, 31);
this.button2.TabIndex = 4;
this.button2.Text = "Save";
this.button2.UseVisualStyleBackColor = true;
//
// cb_VetCheck
//
this.cb_VetCheck.AutoSize = true;
this.cb_VetCheck.Location = new System.Drawing.Point(9, 30);
this.cb_VetCheck.Name = "cb_VetCheck";
this.cb_VetCheck.Size = new System.Drawing.Size(76, 17);
this.cb_VetCheck.TabIndex = 0;
this.cb_VetCheck.Text = "Vet Check";
this.cb_VetCheck.UseVisualStyleBackColor = true;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(6, 75);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(51, 13);
this.label5.TabIndex = 1;
this.label5.Text = "Comment";
//
// tb_Comment
//
this.tb_Comment.Location = new System.Drawing.Point(9, 91);
this.tb_Comment.Name = "tb_Comment";
this.tb_Comment.Size = new System.Drawing.Size(288, 20);
this.tb_Comment.TabIndex = 2;
//
// tb_ID
//
this.tb_ID.Location = new System.Drawing.Point(81, 35);
this.tb_ID.Name = "tb_ID";
this.tb_ID.ReadOnly = true;
this.tb_ID.Size = new System.Drawing.Size(66, 20);
this.tb_ID.TabIndex = 4;
//
// tb_DateArrived
//
this.tb_DateArrived.Location = new System.Drawing.Point(81, 92);
this.tb_DateArrived.Name = "tb_DateArrived";
this.tb_DateArrived.ReadOnly = true;
this.tb_DateArrived.Size = new System.Drawing.Size(66, 20);
this.tb_DateArrived.TabIndex = 5;
//
// tb_IsAdopted
//
this.tb_IsAdopted.Location = new System.Drawing.Point(81, 120);
this.tb_IsAdopted.Name = "tb_IsAdopted";
this.tb_IsAdopted.ReadOnly = true;
this.tb_IsAdopted.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.tb_IsAdopted.Size = new System.Drawing.Size(66, 20);
this.tb_IsAdopted.TabIndex = 6;
//
// tb_adoptedBy
//
this.tb_adoptedBy.Location = new System.Drawing.Point(81, 154);
this.tb_adoptedBy.Name = "tb_adoptedBy";
this.tb_adoptedBy.ReadOnly = true;
this.tb_adoptedBy.Size = new System.Drawing.Size(66, 20);
this.tb_adoptedBy.TabIndex = 7;
//
// groupBox3
//
this.groupBox3.Controls.Add(this.listBox_Cats);
this.groupBox3.Location = new System.Drawing.Point(12, 31);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(175, 369);
this.groupBox3.TabIndex = 5;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Cats";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(7, 61);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(41, 13);
this.label6.TabIndex = 8;
this.label6.Text = "Name: ";
//
// tb_Name
//
this.tb_Name.Location = new System.Drawing.Point(81, 61);
this.tb_Name.Name = "tb_Name";
this.tb_Name.ReadOnly = true;
this.tb_Name.Size = new System.Drawing.Size(66, 20);
this.tb_Name.TabIndex = 9;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(721, 480);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.button2);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ListBox listBox_Cats;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox tb_adoptedBy;
private System.Windows.Forms.TextBox tb_IsAdopted;
private System.Windows.Forms.TextBox tb_DateArrived;
private System.Windows.Forms.TextBox tb_ID;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.TextBox tb_Comment;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.CheckBox cb_VetCheck;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.TextBox tb_Name;
private System.Windows.Forms.Label label6;
}
}
Your listBox_Cats_SelectedIndexChanged is not bound to the SelectedIndexChanged event of the listBox.
There should be entry in your designer file that looks like:
this.listBox_Cats.SelectedIndexChanged += new System.EventHandler(this.listBox_Cats_SelectedIndexChanged);
You can add it manually if it isn't already there - in InitializeComponent.
Your listbox(listBox_Cats) has Cat as a Complex type so try to get the selected item by below code and see it works.
Cat value = (Cat) this.listBox_Cats.SelectedItem;
This is called file.cs in Visual Studio web application with C#. I want to call the function ReadOnlySetting() in file.designer.cs. I tried calling this after the "initialize component" like this ReadOnlySetting(); I am getting the error "must return type"
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=LENOVO- PC;Integrated Security=True");
con.Open();
SqlCommand sc = new SqlCommand("insert intp Emp values('" + textBox2.Text + "'," + textBox1.Text + ",'" + textBox3.Text + "','" + textBox4.Text + "'," + textBox5.Text + ");", con);
object o = sc.ExecuteNonQuery();
MessageBox.Show(o+ " :Record has been inserted");
con.Close();
}
//sender controls what the action is for
//EventArgs is the argument
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
**private void ReadOnlySetting()**
{
this.textBox2.ReadOnly = true;
}
public static void main(string[] args)
{
Application.Run(new Form1());
}
}
}
This is my file.designer.cs
namespace WindowsFormsApplication7
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.dataSet1 = new WindowsFormsApplication7.DataSet1();
this.dataSet1BindingSource = new System.Windows.Forms.BindingSource(this.components);
this.label1 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet1BindingSource)).BeginInit();
this.SuspendLayout();
this.textBox2.ReadOnly = true;
//
// dataSet1
//
this.dataSet1.DataSetName = "DataSet1";
this.dataSet1.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
//
// dataSet1BindingSource
//
this.dataSet1BindingSource.DataSource = this.dataSet1;
this.dataSet1BindingSource.Position = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(45, 75);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 3;
this.label1.Text = "Name";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(86, 72);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 4;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(45, 156);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(25, 13);
this.label2.TabIndex = 3;
this.label2.Text = "Sex";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(86, 153);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(100, 20);
this.textBox3.TabIndex = 4;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(45, 112);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(26, 13);
this.label3.TabIndex = 3;
this.label3.Text = "Age";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(86, 109);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 4;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(45, 195);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(45, 13);
this.label4.TabIndex = 3;
this.label4.Text = "Address";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(86, 192);
this.textBox4.Name = "textBox4";
this.textBox4.ReadOnly = true;
this.textBox4.Size = new System.Drawing.Size(100, 20);
this.textBox4.TabIndex = 4;
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(86, 228);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(100, 20);
this.textBox5.TabIndex = 6;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(45, 231);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(58, 13);
this.label5.TabIndex = 5;
this.label5.Text = "Phone No.";
//
// button1
//
this.button1.Location = new System.Drawing.Point(163, 278);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 7;
this.button1.Text = "Close";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(35, 278);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 8;
this.button2.Text = "Submit";
this.button2.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(420, 380);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.label5);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label4);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet1BindingSource)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.BindingSource dataSet1BindingSource;
private DataSet1 dataSet1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
I don't where I can include my method or how to call it.
Don't put it in the designer. It will just be erased whenever you make a change to your form's design. Call it straight after InitializeComponent.
public Form1()
{
InitializeComponent();
ReadOnlySetting();
}
![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.
I have created a Form in c# windows. My form contains a button and a text box and it 's name is textbox2 here and it contains code like this in Form1.Designer.cs :
this.textBox2.Location = new System.Drawing.Point(683, 14);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 22;
Now i have created another class called Programss.cs which will do some task and as the task finished i want to print on my form something like this:
textBox2.txt= "Task finished"; //it should print on the textbox of my Form1.cs
How to access this textBox2(which can only be accessed currently in Form1.Designer.cs) in Programss.cs and my other classes ?
EDIT: After Philip Stuyck's comments:
Here is my Form1.cs code :
namespace S
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//please see here
public string TextMessage
{
get { return textBox3.Text; }
set { textBox3.Text = value; }
}
private void btnStart_Click(object sender, EventArgs e)
{
btnStart.Enabled = false;
// StartServer();
}
private void btnClose_Click(object sender, EventArgs e)
{
}
}
}
And Program.cs is :
namespace Senter code here
{
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
/////////////////////////////////////////////////////////////////////////////////////
// OtherImportantClasses.Programs prgms = new OtherImportantClasses.Programs();
Form1 theForm = new Form1();
theForm.TextMessage = "Task finished";
}
}
}
Form1.Designer.cs is:
namespace Shekhar
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
public void InitializeComponent()
{
this.btnClose = new System.Windows.Forms.Button();
this.btnStart = new System.Windows.Forms.Button();
this.txtPort = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnClose
//
this.btnClose.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnClose.Location = new System.Drawing.Point(313, 11);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 23);
this.btnClose.TabIndex = 21;
this.btnClose.Text = "Close";
this.btnClose.UseVisualStyleBackColor = true;
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// btnStart
//
this.btnStart.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnStart.Location = new System.Drawing.Point(227, 12);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 20;
this.btnStart.Text = "Start";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(140, 12);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(79, 20);
this.txtPort.TabIndex = 19;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(78, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 18);
this.label1.TabIndex = 18;
this.label1.Text = "Port : ";
OtherImportantClasses.Programs prog = new OtherImportantClasses.Programs();
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(400, 20);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(276, 18);
this.label2.TabIndex = 18;
this.label2.Text = "Total Number of device connected :";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(683, 14);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 22;
// this.textBox2.Text = "testbox2";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(81, 60);
this.textBox3.Multiline = true;
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(702, 243);
this.textBox3.TabIndex = 23;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(818, 315);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Shekhar\'s GT06";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
/*
public string TextMessage
{
get
{
return textBox2.Text;
}
set
{
textBox2.Text = value;
}
}
*/
#endregion
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
}
}
Form1.Designer.cs is :
namespace S`enter code here`
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
public void InitializeComponent()
{
this.btnClose = new System.Windows.Forms.Button();
this.btnStart = new System.Windows.Forms.Button();
this.txtPort = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnClose
//
this.btnClose.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnClose.Location = new System.Drawing.Point(313, 11);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 23);
this.btnClose.TabIndex = 21;
this.btnClose.Text = "Close";
this.btnClose.UseVisualStyleBackColor = true;
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// btnStart
//
this.btnStart.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnStart.Location = new System.Drawing.Point(227, 12);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 20;
this.btnStart.Text = "Start";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(140, 12);
this.txtPort.Name = "txtPort";
this.txtPort.Size = new System.Drawing.Size(79, 20);
this.txtPort.TabIndex = 19;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(78, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 18);
this.label1.TabIndex = 18;
this.label1.Text = "Port : ";
OtherImportantClasses.Programs prog = new OtherImportantClasses.Programs();
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(400, 20);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(276, 18);
this.label2.TabIndex = 18;
this.label2.Text = "Total Number of device connected :";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(683, 14);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 22;
// this.textBox2.Text = "testbox2";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(81, 60);
this.textBox3.Multiline = true;
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(702, 243);
this.textBox3.TabIndex = 23;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(818, 315);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.txtPort);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Shekhar\'s GT06";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
/*
public string TextMessage
{
get
{
return textBox2.Text;
}
set
{
textBox2.Text = value;
}
}
*/
#endregion
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
}
}
It dont give any error but it do not print anything in textBox3 .
NOTE: Please note that first i run the winform code i click a button and when i click the button some proccess will run which will further print the text in the textbox. (I feel like my textmessage is not intialized by my message passed to it).
Make sure that textBox2 is public.(can be done in the designer)
Keep a reference to Form1 when you create it and access textBox2 via that reference.
Form1 theForm = new Form1();
... //do stuff
theForm.textBox2.Text = "Task finished";
This is not the most elegant approach to this, but it should work.
An alternative is like this :
public partial class Form1{
public string TextMessage{
get{
return textBox2.Text;
}
set{
textBox2.Text = value;
}
}
}
and in program.cs
Form1 theForm = new Form1();
... //do stuff
theForm.TextMessage = "Task finished";
your code will not work because
Form1 theForm = new Form1();
theForm.TextMessage = "Task finished";
is never executed.
Application.Run(new Form1());
returns only when the form is closed and then the application will end.
Following will work:
Form1 theForm = new Form1();
theForm.TextMessage = "Task finished";
Application.Run(theForm);
But I have the feeling you don't quite understand the framework.
Application.run, will make your application enter into a message loop which makes the application receive window messages.
One way you could do this is to have a method accessible in your Form1 that sets the textBox name to whatever you want. For instance:
public static void SetText (string message)
{
TextBox2.Text = message;
}
You would then call this from your Program.cs class:
Form1.SetText("Whatever text you want the text on the Form1 - TextBox2 to say");
This is the poor man's way of changing the text. There is a more detailed explanation on this other post if you need more details.
As I understood you want to print end result on textBox2.txt textbox, right? So you want to execute some methods on Programss.cs and once execution is finished you want to show "Task finished" on textBox2.txt.
If I am not wrong, what you have to do is return a value from a method from Programss.cs class. You have to call this method from Form1.Designer.cs class. Based on return value you can do whatever you want.
Hope this will be helpful.
You are creating two separate forms, setting text on the second, and expecting it to appear on the first.
Do this:
Form1 theForm = new Form1();
theForm.TextMessage = "Task finished";
Application.Run(theForm);
I am currently working on a project that allows the user to design his own WinForm (using DesignSurface). Therefore I wrote an algrotithm to create Designer-Code for the form. Everything works fine, except for one thing: The form, once compiled, won't show up. Everything should be working; I could not figure out what the problem was. Anyways, here's the code:
public partial class mainForm : Form
{
public mainForm()
{
InitializeComponent();
}
private System.Windows.Forms.CheckBox checkBox3;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.CheckBox checkBox1;
private CustomButton button1;
private CustomTextBox textBox3;
private System.Windows.Forms.Label label3;
private CustomTextBox textBox2;
private System.Windows.Forms.Label label2;
private CustomTextBox textBox1;
private System.Windows.Forms.Label label1;
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.checkBox3 = new System.Windows.Forms.CheckBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.button1 = new CustomButton();
this.textBox3 = new CustomTextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox2 = new CustomTextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new CustomTextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
//checkBox3
//
this.checkBox3.AutoCheck = false;
this.checkBox3.AutoSize = true;
this.checkBox3.BackColor = System.Drawing.Color.FromArgb(-986896);
this.checkBox3.Text = #"";
this.checkBox3.ForeColor = System.Drawing.Color.FromName("Black");
this.checkBox3.Location = new System.Drawing.Point(187, 151);
this.checkBox3.Name = #"checkBox3";
this.checkBox3.Size = new System.Drawing.Size(87, 17);
this.checkBox3.TabIndex = 9;
this.checkBox3.Tag = #"XC";
//
//checkBox2
//
this.checkBox2.AutoCheck = false;
this.checkBox2.AutoSize = true;
this.checkBox2.BackColor = System.Drawing.Color.FromArgb(-986896);
this.checkBox2.Text = #"";
this.checkBox2.ForeColor = System.Drawing.Color.FromName("Black");
this.checkBox2.Location = new System.Drawing.Point(101, 151);
this.checkBox2.Name = #"checkBox2";
this.checkBox2.Size = new System.Drawing.Size(60, 17);
this.checkBox2.TabIndex = 8;
this.checkBox2.Tag = #"XC";
//
//checkBox1
//
this.checkBox1.AutoCheck = false;
this.checkBox1.AutoSize = true;
this.checkBox1.BackColor = System.Drawing.Color.FromArgb(-986896);
this.checkBox1.Text = #"";
this.checkBox1.ForeColor = System.Drawing.Color.FromName("Black");
this.checkBox1.Location = new System.Drawing.Point(15, 151);
this.checkBox1.Name = #"checkBox1";
this.checkBox1.Size = new System.Drawing.Size(70, 17);
this.checkBox1.TabIndex = 7;
this.checkBox1.Tag = #"XC";
//
//button1
//
this.button1.BackColor = System.Drawing.Color.FromArgb(-986896);
this.button1.Text = #"";
this.button1.UseVisualStyleBackColor = false;
this.button1.ForeColor = System.Drawing.Color.FromName("Black");
this.button1.Location = new System.Drawing.Point(12, 187);
this.button1.Name = #"button1";
this.button1.Size = new System.Drawing.Size(309, 38);
this.button1.TabIndex = 6;
this.button1.Tag = #"XC";
//
//textBox3
//
this.textBox3.UseForCustoming = true;
this.textBox3.CustomingName = #"";
this.textBox3.BackColor = System.Drawing.Color.FromName("White");
this.textBox3.ForeColor = System.Drawing.Color.FromName("Black");
this.textBox3.Location = new System.Drawing.Point(15, 116);
this.textBox3.Name = #"textBox3";
this.textBox3.Size = new System.Drawing.Size(306, 20);
this.textBox3.TabIndex = 5;
this.textBox3.Tag = #"XC";
//
//label3
//
this.label3.AutoSize = true;
this.label3.Text = #"";
this.label3.BackColor = System.Drawing.Color.FromArgb(-986896);
this.label3.ForeColor = System.Drawing.Color.FromName("Black");
this.label3.Location = new System.Drawing.Point(12, 100);
this.label3.Name = #"label3";
this.label3.Size = new System.Drawing.Size(100, 13);
this.label3.TabIndex = 4;
this.label3.Tag = #"XC";
//
//textBox2
//
this.textBox2.UseForCustoming = true;
this.textBox2.CustomingName = #"";
this.textBox2.BackColor = System.Drawing.Color.FromName("White");
this.textBox2.ForeColor = System.Drawing.Color.FromName("Black");
this.textBox2.Location = new System.Drawing.Point(15, 71);
this.textBox2.Name = #"textBox2";
this.textBox2.Size = new System.Drawing.Size(306, 20);
this.textBox2.TabIndex = 3;
this.textBox2.Tag = #"XC";
//
//label2
//
this.label2.AutoSize = true;
this.label2.Text = #"";
this.label2.BackColor = System.Drawing.Color.FromArgb(-986896);
this.label2.ForeColor = System.Drawing.Color.FromName("Black");
this.label2.Location = new System.Drawing.Point(12, 55);
this.label2.Name = #"label2";
this.label2.Size = new System.Drawing.Size(82, 13);
this.label2.TabIndex = 2;
this.label2.Tag = #"XC";
//
//textBox1
//
this.textBox1.UseForCustoming = true;
this.textBox1.CustomingName = #"";
this.textBox1.BackColor = System.Drawing.Color.FromName("White");
this.textBox1.ForeColor = System.Drawing.Color.FromName("Black");
this.textBox1.Location = new System.Drawing.Point(15, 25);
this.textBox1.Name = #"textBox1";
this.textBox1.Size = new System.Drawing.Size(306, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Tag = #"XC";
//
//label1
//
this.label1.AutoSize = true;
this.label1.Text = #"";
this.label1.BackColor = System.Drawing.Color.FromArgb(-986896);
this.label1.ForeColor = System.Drawing.Color.FromName("Black");
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = #"label1";
this.label1.Size = new System.Drawing.Size(51, 13);
this.label1.Tag = #"XC";
//
//mainForm
//
this.AutoScaleDimensions = new SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(455, 275);
this.BackColor = System.Drawing.Color.FromArgb(-986896);
this.MaximizeBox = false;
this.ShowIcon = false;
this.Size = new System.Drawing.Size(349, 276);
this.Text = #"";
this.TopLevel = false;
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AllowDrop = true;
this.ForeColor = System.Drawing.Color.FromName("Black");
this.Name = #"opForm";
this.Tag = #"";
this.Controls.Add(checkBox3);
this.Controls.Add(checkBox2);
this.Controls.Add(checkBox1);
this.Controls.Add(button1);
this.Controls.Add(textBox3);
this.Controls.Add(label3);
this.Controls.Add(textBox2);
this.Controls.Add(label2);
this.Controls.Add(textBox1);
this.Controls.Add(label1);
this.ResumeLayout(false);
this.PerformLayout();
}
}
And here's the code that creates the form:
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new mainForm());
}
}
I don't get what's wrong with this code. Maybe someone can help me out here.
Chester
For one thing I think the values for ClientSize and Size are clashing. Actually I believe only one should be set and the last one to be set will win.
But the one line that prevents the window from showing up is this:
this.TopLevel = false;
Leave it out or set it to true (the default) and your window will show just fine..
From MSDN:
A top-level form is a window that has no parent form, or whose parent
form is the desktop window. Top-level windows are typically used as
the main form in an application.
So if DesignSurface has set it to false you probably have told it that you are creating something else than a main form.. I believe MDI child windows have it set to false, but I'm not sure, MDI is soo long away..