I decided to change a listBox1 item in my form1 designer to public static instead of private to access it on another form. And once I had done that It had errored and not allowed me to changed things on the form itself and just tells me this error at the top
The variable 'listBox1' is either undeclared or was never assigned.
The error is shown on the following line
this.panel1.Controls.Add(listBox1);
My Designer for my form below:
namespace Mercury.Forms
{
partial class Inside
{
/// <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.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
Inside.listBox1 = new System.Windows.Forms.ListBox();
this.label3 = new System.Windows.Forms.Label();
this.panel2 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.White;
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox1.Image = global::Mercury.Properties.Resources.pooltablegodass_500x750;
this.pictureBox1.Location = new System.Drawing.Point(12, 11);
this.pictureBox1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(54, 48);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
this.label1.Location = new System.Drawing.Point(74, 15);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(82, 20);
this.label1.TabIndex = 1;
this.label1.Text = "Ash Smith";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
this.label2.Location = new System.Drawing.Point(76, 37);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(66, 13);
this.label2.TabIndex = 2;
this.label2.Text = "ashleytechie";
this.label2.Click += new System.EventHandler(this.label2_Click);
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.label3);
this.panel1.Controls.Add(listBox1);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 348);
this.panel1.TabIndex = 3;
//
// listBox1
//
listBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
listBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
listBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
listBox1.ForeColor = System.Drawing.SystemColors.Window;
listBox1.FormattingEnabled = true;
listBox1.ItemHeight = 15;
listBox1.Items.AddRange(new object[] {
"Jake Simpson",
"Adam Jones",
"Jane Peters",
"Ashley Smith",
"Sam Smith",
"Joe Phelan",
"Peter Black",
"Tracy Bratford",
"Jamie Harrison",
"Peter Slims"});
listBox1.Location = new System.Drawing.Point(16, 107);
listBox1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
listBox1.Name = "listBox1";
listBox1.Size = new System.Drawing.Size(144, 180);
listBox1.TabIndex = 3;
listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label3.ForeColor = System.Drawing.SystemColors.Window;
this.label3.Location = new System.Drawing.Point(12, 79);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(105, 24);
this.label3.TabIndex = 4;
this.label3.Text = "Contact List";
//
// panel2
//
this.panel2.Location = new System.Drawing.Point(205, 7);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(518, 335);
this.panel2.TabIndex = 4;
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
this.button1.Location = new System.Drawing.Point(16, 304);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(170, 31);
this.button1.TabIndex = 5;
this.button1.Text = "Edit Profile";
this.button1.UseVisualStyleBackColor = true;
//
// Inside
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ButtonHighlight;
this.ClientSize = new System.Drawing.Size(730, 347);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "Inside";
this.Text = "Inside";
this.Load += new System.EventHandler(this.Inside_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Panel panel1;
public static System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Button button1;
}
}
Change this back to what it was previously: (public to private, remove static)
public static System.Windows.Forms.ListBox listBox1;
Add this in the code-behind in your Form: (you don't need a setter since you won't be creating a new ListBox from the other Forms... I hope)
public ListBox MyListBox
{
get { return listBox1; }
}
Now in other Forms (assuming they're being created by some process in your first Form), you'll pass a reference to the current instance of your Form with the ListBox on it, so you can actually access the public property you just created:
public class SecondForm : Form
{
public SecondForm(FirstForm firstForm)
{
// do something with firstForm.MyListBox
}
}
Lastly, I'd say be careful how much you do this. Forms being able to access each other's controls seems messy to me. If you added more details about exactly what you're trying to accomplish with this, we might be able to provide a better alternative.
Your issue is that you changed the code in the designer file so that listBox1 is now static. You can't do this because the designer must be able to instantiate an instance of ListBox.
What it sounds like you are trying to do is you are trying to modify the listbox from another form. The proper way you should do this is through properties. In your code (not the designer code, but the code behind code), have something like this:
public ListBox MyListBox
{
get
{
return listBox1;
}
}
You shouldn't change anything in the .Designer.cs file. If you want to change access modifiers you should do so through the Properties window.
That being said, your public change was fine, you just can't make it static. For the designer to function all the controls in the form must be independent to each instantiation.
// what the code will look like when you change it from the Properties window
public System.Windows.Forms.ListBox listBox1;
If you were to make the ListBox static, you would access it like
ListBox listbox = Inside.listBox1;
which doesn't make much sense if you have more than one Inside form floating around. If you make it non-static you can still access it from wherever you want as long as you have an instance of the Inside class.
Inside inside = new Inside();
ListBox listbox = inside.listBox1;
Related
I've created a form that I call a ListChooser. It looks like so:
It consists of a System.Windows.Forms.Button and .TextBox.
Clicking on the Button (shown above with the text "Fruits") causes a dialog to popup and some selections are made and the TextBox is populated, like so:
My problem is I can't figure out how to set a System.Windows.Forms.Binding to this TextBox. I think the complication is that it's not just a TextBox, it's 2 components in one. (I know how to data bind with just a standalone TextBox). If I go to the TextBox > Properties > DataBindings > Advanced > ..., All entries listed under Property are not selectable:
I've tried this which has no effect:
private System.Windows.Forms.BindingSource myBindingSource;
this.myBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.myBindingSource.DataSource = typeof(Foods) // Fruits is a Property of Foods;
myListChooser.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.myBindingSource, "Fruits", true));
How do I do this?
ListChooser.Designer.cs:
partial class ListChooser: UserControl
{
/// <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 Component 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.btnPopup = new System.Windows.Forms.Button();
this.textBox = new System.Windows.Forms.TextBox();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.SuspendLayout();
//
// btnPopup
//
this.btnPopup.AutoSize = true;
this.btnPopup.Dock = System.Windows.Forms.DockStyle.Fill;
this.btnPopup.Location = new System.Drawing.Point(0, 0);
this.btnPopup.Margin = new System.Windows.Forms.Padding(0);
this.btnPopup.Name = "btnPopup";
this.btnPopup.Size = new System.Drawing.Size(75, 21);
this.btnPopup.TabIndex = 0;
this.btnPopup.Text = "Type";
this.btnPopup.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btnPopup.UseVisualStyleBackColor = true;
this.btnPopup.Click += new System.EventHandler(this.btnPopup_Click);
//
// txtList
//
this.textBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox.BackColor = System.Drawing.SystemColors.Window;
this.textBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBox.Location = new System.Drawing.Point(0, 0);
this.textBox.Name = "txtList";
this.textBox.ReadOnly = true;
this.textBox.Size = new System.Drawing.Size(297, 20);
this.textBox.TabIndex = 1;
this.textBox.TabStop = false;
this.textBox.TextChanged += new System.EventHandler(this.txtList_TextChanged);
//this.textBox.Leave += new System.EventHandler(this.txtList_Leave);
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Margin = new System.Windows.Forms.Padding(0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.btnPopup);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.textBox);
this.splitContainer1.Size = new System.Drawing.Size(375, 21);
this.splitContainer1.SplitterDistance = 75;
this.splitContainer1.SplitterWidth = 3;
this.splitContainer1.TabIndex = 2;
//
// ListChooser
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.WhiteSmoke;
this.Controls.Add(this.splitContainer1);
this.MaximumSize = new System.Drawing.Size(1200, 21);
this.Name = "ListChooser";
this.Size = new System.Drawing.Size(375, 21);
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.Panel2.PerformLayout();
this.splitContainer1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button btnPopup;
private System.Windows.Forms.TextBoxBase textBox;
private SplitContainer splitContainer1;
}
I wrote a GUI Form in c#. When I executed the program, white color becomes transparent.
It's suppose to be like this:
But when I run, it's like that:
You can see that white color became black like the picture behind.
using System;
using System.Drawing;
namespace Taki_Client
{
partial class SignIn
{
/// <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.btnMenu = new System.Windows.Forms.Button();
this.picSignIn = new System.Windows.Forms.PictureBox();
this.btnSubmit = new System.Windows.Forms.Button();
this.tbUsername = new System.Windows.Forms.TextBox();
this.tbPassword = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.picSignIn)).BeginInit();
this.SuspendLayout();
//
// btnMenu
//
this.btnMenu.BackgroundImage = global::Taki_Client.Properties.Resources.Menu_Button;
this.btnMenu.Location = new System.Drawing.Point(314, 476);
this.btnMenu.Name = "btnMenu";
this.btnMenu.Size = new System.Drawing.Size(157, 57);
this.btnMenu.TabIndex = 4;
this.btnMenu.UseVisualStyleBackColor = true;
this.btnMenu.MouseClick += new System.Windows.Forms.MouseEventHandler(this.btnExit_MouseClick);
//
// picSignIn
//
this.picSignIn.BackColor = System.Drawing.Color.Transparent;
this.picSignIn.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.picSignIn.ErrorImage = null;
this.picSignIn.Image = global::Taki_Client.Properties.Resources.SignIn;
this.picSignIn.ImageLocation = "";
this.picSignIn.Location = new System.Drawing.Point(272, 12);
this.picSignIn.Name = "picSignIn";
this.picSignIn.Size = new System.Drawing.Size(288, 161);
this.picSignIn.TabIndex = 0;
this.picSignIn.TabStop = false;
this.picSignIn.Click += new System.EventHandler(this.pictureBox1_Click);
//
// btnSubmit
//
this.btnSubmit.BackgroundImage = global::Taki_Client.Properties.Resources.Submit_Button;
this.btnSubmit.Location = new System.Drawing.Point(314, 347);
this.btnSubmit.Name = "btnSubmit";
this.btnSubmit.Size = new System.Drawing.Size(157, 57);
this.btnSubmit.TabIndex = 5;
this.btnSubmit.UseVisualStyleBackColor = true;
this.btnSubmit.MouseClick += new System.Windows.Forms.MouseEventHandler(this.btnSubmit_MouseClick);
//
// tbUsername
//
this.tbUsername.BackColor = System.Drawing.SystemColors.HotTrack;
this.tbUsername.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
this.tbUsername.ForeColor = System.Drawing.SystemColors.Menu;
this.tbUsername.Location = new System.Drawing.Point(314, 223);
this.tbUsername.Multiline = true;
this.tbUsername.Name = "tbUsername";
this.tbUsername.Size = new System.Drawing.Size(201, 44);
this.tbUsername.TabIndex = 6;
this.tbUsername.Text = "Username";
//
// tbPassword
//
this.tbPassword.BackColor = System.Drawing.SystemColors.HotTrack;
this.tbPassword.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
this.tbPassword.ForeColor = System.Drawing.SystemColors.Menu;
this.tbPassword.Location = new System.Drawing.Point(314, 284);
this.tbPassword.Multiline = true;
this.tbPassword.Name = "tbPassword";
this.tbPassword.Size = new System.Drawing.Size(201, 44);
this.tbPassword.TabIndex = 7;
this.tbPassword.Text = "Password";
//
// SignIn
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.BackgroundImage = global::Taki_Client.Properties.Resources.Taki_main_screen;
this.ClientSize = new System.Drawing.Size(804, 611);
this.Controls.Add(this.tbPassword);
this.Controls.Add(this.tbUsername);
this.Controls.Add(this.btnSubmit);
this.Controls.Add(this.btnMenu);
this.Controls.Add(this.picSignIn);
this.Name = "SignIn";
this.Text = "Taki Client";
this.TransparencyKey = System.Drawing.Color.Transparent;
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.picSignIn)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.PictureBox picSignIn;
private System.Windows.Forms.Button btnMenu;
private System.Windows.Forms.Button btnSubmit;
private System.Windows.Forms.TextBox tbUsername;
private System.Windows.Forms.TextBox tbPassword;
}
}
By setting the TransparencyKey of the form, transparency is enabled. Default color for transparency is white.
Remove this line from the InitializeComponent () function
this.TransparencyKey = System.Drawing.Color.Transparent;
and the white on your form will no longer be transparent.
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 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.
I want to make 2 of columns in DataGrid editable. I tried changing readonly to false, but that didn't help.
So can anyone tell me any other way that I can try or is this something which is not letting it be editable.
Thanks
I went to DataDridView Task and on that particular column I changed ReadOnly 'false'. But this didnt help.
And yes it is a WinForm.
namespace Payroll
{
partial class AttendanceLog
{
/// <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()
{
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
this.label11 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnShow = new System.Windows.Forms.Button();
this.cmbMonth = new System.Windows.Forms.ComboBox();
this.label6 = new System.Windows.Forms.Label();
this.cmbYear = new System.Windows.Forms.ComboBox();
this.btnClose = new System.Windows.Forms.Button();
this.dgAttendanceLog = new System.Windows.Forms.DataGridView();
this.btnSave = new System.Windows.Forms.Button();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
this.slno = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.empId = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.empName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.workedDays = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.absentDays = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.weeklyOff = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.lateCominBy = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.earlyGoingBy = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dgAttendanceLog)).BeginInit();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
//
// label11
//
this.label11.AutoSize = true;
this.label11.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label11.ForeColor = System.Drawing.SystemColors.ControlText;
this.label11.Location = new System.Drawing.Point(14, 42);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(49, 13);
this.label11.TabIndex = 106;
this.label11.Text = "Month :";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnShow);
this.groupBox1.Controls.Add(this.cmbMonth);
this.groupBox1.Controls.Add(this.label11);
this.groupBox1.Controls.Add(this.label6);
this.groupBox1.Controls.Add(this.cmbYear);
this.groupBox1.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.groupBox1.ForeColor = System.Drawing.Color.MidnightBlue;
this.groupBox1.Location = new System.Drawing.Point(12, 12);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(688, 79);
this.groupBox1.TabIndex = 108;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Select";
//
// btnShow
//
this.btnShow.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnShow.ForeColor = System.Drawing.Color.Black;
this.btnShow.Location = new System.Drawing.Point(533, 34);
this.btnShow.Name = "btnShow";
this.btnShow.Size = new System.Drawing.Size(75, 33);
this.btnShow.TabIndex = 108;
this.btnShow.Text = "Show";
this.btnShow.UseVisualStyleBackColor = true;
this.btnShow.Click += new System.EventHandler(this.btnShow_Click);
//
// cmbMonth
//
this.cmbMonth.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.cmbMonth.FormattingEnabled = true;
this.cmbMonth.Items.AddRange(new object[] {
"Month",
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"});
this.cmbMonth.Location = new System.Drawing.Point(83, 34);
this.cmbMonth.Name = "cmbMonth";
this.cmbMonth.Size = new System.Drawing.Size(125, 21);
this.cmbMonth.TabIndex = 104;
//
// label6
//
this.label6.AutoSize = true;
this.label6.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label6.ForeColor = System.Drawing.SystemColors.ControlText;
this.label6.Location = new System.Drawing.Point(283, 42);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(39, 13);
this.label6.TabIndex = 107;
this.label6.Text = "Year :";
//
// cmbYear
//
this.cmbYear.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.cmbYear.FormattingEnabled = true;
this.cmbYear.Location = new System.Drawing.Point(341, 34);
this.cmbYear.Name = "cmbYear";
this.cmbYear.Size = new System.Drawing.Size(125, 21);
this.cmbYear.TabIndex = 105;
//
// btnClose
//
this.btnClose.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnClose.ForeColor = System.Drawing.Color.Black;
this.btnClose.Location = new System.Drawing.Point(625, 501);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 33);
this.btnClose.TabIndex = 9;
this.btnClose.Text = "Close";
this.btnClose.UseVisualStyleBackColor = true;
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// dgAttendanceLog
//
this.dgAttendanceLog.AllowUserToAddRows = false;
dataGridViewCellStyle1.BackColor = System.Drawing.Color.LightCyan;
this.dgAttendanceLog.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
this.dgAttendanceLog.BackgroundColor = System.Drawing.Color.WhiteSmoke;
this.dgAttendanceLog.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgAttendanceLog.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.slno,
this.empId,
this.empName,
this.workedDays,
this.absentDays,
this.weeklyOff,
this.lateCominBy,
this.earlyGoingBy});
this.dgAttendanceLog.Location = new System.Drawing.Point(12, 107);
this.dgAttendanceLog.Name = "dgAttendanceLog";
this.dgAttendanceLog.RowHeadersVisible = false;
this.dgAttendanceLog.Size = new System.Drawing.Size(688, 388);
this.dgAttendanceLog.TabIndex = 109;
this.dgAttendanceLog.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgAttendanceLog_CellDoubleClick);
//
// btnSave
//
this.btnSave.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnSave.ForeColor = System.Drawing.Color.Black;
this.btnSave.Location = new System.Drawing.Point(544, 501);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(75, 33);
this.btnSave.TabIndex = 110;
this.btnSave.Text = "Save";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabel1});
this.statusStrip1.Location = new System.Drawing.Point(0, 545);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(711, 22);
this.statusStrip1.TabIndex = 111;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabel1
//
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel1.Size = new System.Drawing.Size(298, 17);
this.toolStripStatusLabel1.Text = "Note : Please Single click on respective field for manual entry";
//
// slno
//
this.slno.HeaderText = "Sl No.";
this.slno.Name = "slno";
this.slno.Width = 50;
//
// empId
//
this.empId.HeaderText = "Emp. ID";
this.empId.Name = "empId";
this.empId.Width = 75;
//
// empName
//
this.empName.HeaderText = "Emp. Name";
this.empName.Name = "empName";
this.empName.Width = 125;
//
// workedDays
//
this.workedDays.HeaderText = "Worked Days";
this.workedDays.Name = "workedDays";
this.workedDays.ReadOnly = true;
this.workedDays.Width = 75;
//
// absentDays
//
this.absentDays.HeaderText = "Absent Days";
this.absentDays.Name = "absentDays";
this.absentDays.ReadOnly = true;
this.absentDays.Width = 75;
//
// weeklyOff
//
this.weeklyOff.HeaderText = "Weekly Off";
this.weeklyOff.Name = "weeklyOff";
this.weeklyOff.Width = 75;
//
// lateCominBy
//
this.lateCominBy.HeaderText = "Late Going By (in Days)";
this.lateCominBy.Name = "lateCominBy";
//
// earlyGoingBy
//
this.earlyGoingBy.HeaderText = "Early Going By (in Days)";
this.earlyGoingBy.Name = "earlyGoingBy";
//
// AttendanceLog
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.WhiteSmoke;
this.ClientSize = new System.Drawing.Size(711, 567);
this.ControlBox = false;
this.Controls.Add(this.statusStrip1);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.dgAttendanceLog);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.groupBox1);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "AttendanceLog";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Attendance Log";
this.Load += new System.EventHandler(this.AttendanceLog_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.dgAttendanceLog)).EndInit();
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label11;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btnShow;
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.DataGridView dgAttendanceLog;
private System.Windows.Forms.ComboBox cmbMonth;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.ComboBox cmbYear;
private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
private System.Windows.Forms.DataGridViewTextBoxColumn slno;
private System.Windows.Forms.DataGridViewTextBoxColumn empId;
private System.Windows.Forms.DataGridViewTextBoxColumn empName;
private System.Windows.Forms.DataGridViewTextBoxColumn workedDays;
private System.Windows.Forms.DataGridViewTextBoxColumn absentDays;
private System.Windows.Forms.DataGridViewTextBoxColumn weeklyOff;
private System.Windows.Forms.DataGridViewTextBoxColumn lateCominBy;
private System.Windows.Forms.DataGridViewTextBoxColumn earlyGoingBy;
}
}
Ok I found your problem. You most propably are using no DataSource or using a ReadOnlyCollection as Datasource. Then the DataGridView will not allow editing at all.
I have seen the same behaviour as you if I only add Rows to the datagrid. As soon as I use a BindingList as DataSource I am able to edit (clicking once into cell and start to type). You may think about removing your double click handler too as this interferes with the default behaviour.
This works for me:
public class Person
{
public string Name { get; set; }
public string EmpId { get; set; }
}
public MainForm()
{
InitializeComponent();
// bind column to property of person
empName.DataPropertyName = "Name";
empId.DataPropertyName = "EmpId";
// set our bindinglist of persons as datasource
var bindingList = new BindingList<Person>();
bindingList.Add(new Person(){ Name = "John", EmpId = "12345" });
bindingList.Add(new Person(){ Name = "David", EmpId = "12346" });
dgAttendanceLog.DataSource = bindingList;
}
In the WinForms you can set EditMode property of the DataGrid to EditOnEnter.also readonly property of datagrid view should be false. then you can set ReadOnly for each Column.