I have a main form called mainForm - this runs my entire app.
In this form I create other forms like this:
......
......
Form[] formMessage = new Form[10];
int formNumber = 0;
System.Windows.Forms.Button btnCancel;
System.Windows.Forms.Button btnClose;
System.Windows.Forms.Label lblTimer;
System.Windows.Forms.Button btnOK;
System.Windows.Forms.Panel panel1;
System.Windows.Forms.Label lblMessage;
System.Windows.Forms.PictureBox pictureBox1;
System.Windows.Forms.Label lblTitle;
......
......
public void CreateForm(Form form2)
{
this.btnCancel = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.lblTimer = new System.Windows.Forms.Label();
this.btnOK = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.lblMessage = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.lblTitle = new System.Windows.Forms.Label();
//
........
//
// lblTimer
//
this.lblTimer.AutoSize = true;
this.lblTimer.BackColor = System.Drawing.Color.Transparent;
this.lblTimer.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblTimer.Location = new System.Drawing.Point(9, 120);
this.lblTimer.Name = "lblTimer";
this.lblTimer.Size = new System.Drawing.Size(0, 16);
this.lblTimer.Visible = Show_Timer;
this.lblTimer.TabIndex = 4;
form2.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
form2.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
form2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
form2.ClientSize = new System.Drawing.Size(400, 142);
form2.ControlBox = false;
form2.Controls.Add(this.pictureBox1);
form2.Controls.Add(this.panel1);
form2.Controls.Add(this.btnOK);
form2.Controls.Add(this.lblTimer);
form2.Controls.Add(this.btnCancel);
form2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form2.Opacity = 0.98;
form2.ShowInTaskbar = false;
form2.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
form2.Tag = formNumber;
form2.Paint += new System.Windows.Forms.PaintEventHandler(this.MyMessageBox_Paint);
}
private void FNeventTrigger(System.Object sender, System.EventArgs e)
{
formMessage[formNumber] = new Form();
CreateForm(formMessage[formNumber]);
formMessage[formNumber].Show();
formNumber++;
if (formNumber == 10)
formNumber = 0;
}
public void lbl_timer_UpdateText()
{
this.lblTimer.Text = newText
}
I use the FNeventRigger to create my form, and I can have upto 10 of them open at each given time - I use this for showing count down timers.
The problem I have is how do I show the count down timer of each form?
If I use : this.lblTimer.Text = newText, then only the newest form that was opened displays the correct timer.... the other forms lblTimer.Text stop functioning.
Is there a way to address all the lblTimer.Text on all forms opened on the array?
Thanks,
Create your own form class which defines the Label an a method to update this method.
and then initiate all your forms using this new MyBaseForm
public MyBaseForm : Form
{
private Label lblTimer;
public MyBaseForm()
{
lblTimer = new Label();
Controls.Add(lblTimer);
}
public void UpdateTimerText(string text)
{
lblTimer.Text = text;
}
}
Related
When I try to execute my windows form application, all I see is a completely white blank form. When I look in my form1.cs[Design] everything looks normal, but not when I run it? I can't see the labels, or the buttons or anything. I've never had this issue before and my InitializeComponent I think is where it should be? Here is my Form1.Designer.Cs, or maybe there's an issue in my form1.cs?
partial class Identity
{
/// <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.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.label5 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label6 = new System.Windows.Forms.Label();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(278, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(116, 87);
this.label1.TabIndex = 0;
this.label1.Text = "VÄLKOMMEN!";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.Location = new System.Drawing.Point(67, 87);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(222, 23);
this.label2.TabIndex = 1;
this.label2.Text = "Skriv in ditt namn och personnummer nedan.:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(67, 181);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(51, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Förnamn:";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(63, 217);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(58, 13);
this.label4.TabIndex = 3;
this.label4.Text = "Efternamn:";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(30, 269);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(161, 13);
this.label5.TabIndex = 4;
this.label5.Text = "Personummer:(ååååmmdd - xxxx)";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(127, 174);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 5;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(127, 210);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 6;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(197, 266);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(164, 20);
this.textBox3.TabIndex = 7;
//
// button1
//
this.button1.Location = new System.Drawing.Point(307, 329);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(87, 51);
this.button1.TabIndex = 8;
this.button1.Text = "Kör!";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(337, 395);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(35, 13);
this.label6.TabIndex = 9;
this.label6.Text = "label6";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(33, 423);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(492, 296);
this.richTextBox1.TabIndex = 10;
this.richTextBox1.Text = "\"\"";
//
// button2
//
this.button2.Location = new System.Drawing.Point(662, 650);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(116, 69);
this.button2.TabIndex = 11;
this.button2.Text = "Avsluta:";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Identity
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1207, 755);
this.Controls.Add(this.button2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.label6);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Identity";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Button button2;
private EventHandler label1_Click;
}
}
Here I am calling the IntializeComponent in my Form1.cs, I haven't moved it since I started this new program at all.
public partial class Identity : Form
{
public Identity()
{
InitializeComponent();
}
public struct Person
{
public double idok;
public string fname;
public string lname;
public string id;
public string sex;
public string year;
public string fyear;
}
Per the comment trail, you made a new solution, copied some select lines over from the problematic form's designer code, commented out some event handlers that were missing in the designer code, and then were able to load the designer and add the missing event handlers and their body code. The resulting form worked OK
I've posted my code. Every single time I change anything on my this form in my Windows Forms App in C#, it changes my "public static" changes at the end to just "public" and then it adds "this." to every single reference to them. My code seems to run fine after I edit it again, but this is really annoying. I just wanted to make sure I'm not missing something or taking a poor approach to this.
The reason I am using public static statements is because I am modifying some Textboxes and a DataGridView object on my main form from other forms - i.e. looking up customer info from a database in one form and then entering the data on the main form to write up an order, and taking shopping cart information from a WebBrowser control on one form and putting it into a DataGridView object on another form. If this isn't how to accomplish populating forms from another form please tell me how and be as specific as possible as I am quite new to C#. Sorry if I've posted too much code - this is almost everything from my form1.cs file and I didn't know what (if anything) that I have here is causing the problem. The relevant "public static" statements are at the end.
(AFTER I WROTE THIS - I looked up another thread where someone said that forms are supposed to be a "specific instantiation" and that is why the developer environment keeps taking out the "static"). That being the case... HELP!! I do not understand how to change the values on one form from another form and I absolutely need to do so. - this code works, but I guess it's bad practice? - Completely lost and need very specific instructions to make this work)
Thanks in advance!!!
namespace WindowsFormsApp1
{
public partial class frmMain
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.cmdSendEmail = new System.Windows.Forms.Button();
this.cmdExit = new System.Windows.Forms.Button();
this.cmdLookup = new System.Windows.Forms.Button();
this.txtTransactionNumber = new System.Windows.Forms.TextBox();
this.cmdEdit = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.cmdBrowse = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.cmdCustomer = new System.Windows.Forms.Button();
this.txtNotes = new System.Windows.Forms.TextBox();
txtZip = new System.Windows.Forms.TextBox();
txtCountry = new System.Windows.Forms.TextBox();
txtState = new System.Windows.Forms.TextBox();
txtCity = new System.Windows.Forms.TextBox();
txtAddress = new System.Windows.Forms.TextBox();
txtName = new System.Windows.Forms.TextBox();
txtPayPalEmail = new System.Windows.Forms.TextBox();
txtEmail = new System.Windows.Forms.TextBox();
txtPhone = new System.Windows.Forms.TextBox();
txtLast = new System.Windows.Forms.TextBox();
txtFirst = new System.Windows.Forms.TextBox();
grdOrderItems = new System.Windows.Forms.DataGridView();
this.Qty = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Description = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.UnitPrice = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.ExtPrice = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.SCode = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Options1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Options2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.lblSubTotal = new System.Windows.Forms.Label();
this.lblTotal = new System.Windows.Forms.Label();
this.txtTax = new System.Windows.Forms.TextBox();
this.txtShipping = new System.Windows.Forms.TextBox();
this.txtShippingMethod = new System.Windows.Forms.TextBox();
this.cmdTaxLookup = new System.Windows.Forms.Button();
this.lstShipping = new System.Windows.Forms.ListBox();
this.cmdBrowser = new System.Windows.Forms.Button();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.cmdSendRequest = new System.Windows.Forms.Button();
this.cmdClear = new System.Windows.Forms.Button();
this.cmdSave = new System.Windows.Forms.Button();
this.cmdPrint = new System.Windows.Forms.Button();
this.cmdSavePrintEmail = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(grdOrderItems)).BeginInit();
this.groupBox3.SuspendLayout();
this.groupBox4.SuspendLayout();
this.SuspendLayout();
//
// cmdLookup
//
this.cmdLookup.Location = new System.Drawing.Point(88, 15);
this.cmdLookup.Name = "cmdLookup";
this.cmdLookup.Size = new System.Drawing.Size(197, 33);
this.cmdLookup.TabIndex = 3;
this.cmdLookup.Text = "Look up order to attach request to";
this.cmdLookup.UseVisualStyleBackColor = true;
//
// txtTransactionNumber
//
this.txtTransactionNumber.Location = new System.Drawing.Point(91, 57);
this.txtTransactionNumber.Name = "txtTransactionNumber";
this.txtTransactionNumber.Size = new System.Drawing.Size(158, 20);
this.txtTransactionNumber.TabIndex = 5;
//
// cmdEdit
//
this.cmdEdit.Location = new System.Drawing.Point(260, 57);
this.cmdEdit.Name = "cmdEdit";
this.cmdEdit.Size = new System.Drawing.Size(87, 30);
this.cmdEdit.TabIndex = 6;
this.cmdEdit.Text = "Edit";
this.cmdEdit.UseVisualStyleBackColor = true;
//
//
// groupBox1
//
this.groupBox1.Location = new System.Drawing.Point(438, 125);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(345, 56);
this.groupBox1.TabIndex = 10;
this.groupBox1.TabStop = false;
//
// cmdBrowse
//
this.cmdBrowse.Location = new System.Drawing.Point(14, 139);
this.cmdBrowse.Name = "cmdBrowse";
this.cmdBrowse.Size = new System.Drawing.Size(220, 22);
this.cmdBrowse.TabIndex = 11;
this.cmdBrowse.Text = "Browse Unpaid Payment Requests";
this.cmdBrowse.UseVisualStyleBackColor = true;
//
// groupBox2
//
this.groupBox2.BackColor = System.Drawing.Color.WhiteSmoke;
this.groupBox2.Controls.Add(this.cmdCustomer);
this.groupBox2.Controls.Add(this.txtNotes);
this.groupBox2.Controls.Add(txtZip);
this.groupBox2.Controls.Add(txtCountry);
this.groupBox2.Controls.Add(txtState);
this.groupBox2.Controls.Add(txtCity);
this.groupBox2.Controls.Add(txtAddress);
this.groupBox2.Controls.Add(txtName);
this.groupBox2.Controls.Add(txtPayPalEmail);
this.groupBox2.Controls.Add(txtEmail);
this.groupBox2.Controls.Add(txtPhone);
this.groupBox2.Controls.Add(txtLast);
this.groupBox2.Controls.Add(txtFirst);
this.groupBox2.Location = new System.Drawing.Point(14, 168);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(749, 264);
this.groupBox2.TabIndex = 12;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Customer Information";
this.groupBox2.Enter += new System.EventHandler(this.groupBox2_Enter);
//
// label21
//
this.label21.AutoSize = true;
this.label21.Location = new System.Drawing.Point(186, 215);
this.label21.Name = "label21";
this.label21.Size = new System.Drawing.Size(25, 13);
this.label21.TabIndex = 24;
this.label21.Text = "Zip:";
//
// cmdCustomer
//
this.cmdCustomer.Location = new System.Drawing.Point(657, 136);
this.cmdCustomer.Name = "cmdCustomer";
this.cmdCustomer.Size = new System.Drawing.Size(86, 69);
this.cmdCustomer.TabIndex = 23;
this.cmdCustomer.Text = "Load Customer Info from Prior Order";
this.cmdCustomer.UseVisualStyleBackColor = true;
this.cmdCustomer.Click += new System.EventHandler(this.cmdCustomer_Click);
//
// txtNotes
//
this.txtNotes.AcceptsReturn = true;
this.txtNotes.Location = new System.Drawing.Point(404, 136);
this.txtNotes.Multiline = true;
this.txtNotes.Name = "txtNotes";
this.txtNotes.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtNotes.Size = new System.Drawing.Size(247, 122);
this.txtNotes.TabIndex = 19;
//
// txtZip
//
txtZip.Location = new System.Drawing.Point(211, 212);
txtZip.Name = "txtZip";
txtZip.Size = new System.Drawing.Size(99, 20);
txtZip.TabIndex = 25;
txtZip.TextChanged += new System.EventHandler(this.txtZip_TextChanged);
//
// txtCountry
//
txtCountry.Location = new System.Drawing.Point(72, 237);
txtCountry.Name = "txtCountry";
txtCountry.Size = new System.Drawing.Size(238, 20);
txtCountry.TabIndex = 22;
txtCountry.TextChanged += new System.EventHandler(this.txtCountry_TextChanged);
//
// txtState
//
txtState.Location = new System.Drawing.Point(72, 210);
txtState.Name = "txtState";
txtState.Size = new System.Drawing.Size(108, 20);
txtState.TabIndex = 21;
txtState.TextChanged += new System.EventHandler(this.txtState_TextChanged);
//
// txtCity
//
txtCity.Location = new System.Drawing.Point(72, 185);
txtCity.Name = "txtCity";
txtCity.Size = new System.Drawing.Size(238, 20);
txtCity.TabIndex = 20;
txtCity.TextChanged += new System.EventHandler(this.txtCity_TextChanged);
//
// txtAddress
//
txtAddress.AcceptsReturn = true;
txtAddress.Location = new System.Drawing.Point(72, 92);
txtAddress.Multiline = true;
txtAddress.Name = "txtAddress";
txtAddress.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
txtAddress.Size = new System.Drawing.Size(236, 87);
txtAddress.TabIndex = 18;
txtAddress.TextChanged += new System.EventHandler(this.txtAddress_TextChanged);
//
// txtName
//
txtName.Location = new System.Drawing.Point(72, 68);
txtName.Name = "txtName";
txtName.Size = new System.Drawing.Size(238, 20);
txtName.TabIndex = 17;
txtName.TextChanged += new System.EventHandler(this.txtName_TextChanged);
//
// txtPayPalEmail
//
txtPayPalEmail.Location = new System.Drawing.Point(405, 98);
txtPayPalEmail.Name = "txtPayPalEmail";
txtPayPalEmail.Size = new System.Drawing.Size(203, 20);
txtPayPalEmail.TabIndex = 16;
//
// txtEmail
//
txtEmail.Location = new System.Drawing.Point(404, 65);
txtEmail.Name = "txtEmail";
txtEmail.Size = new System.Drawing.Size(204, 20);
txtEmail.TabIndex = 15;
//
// txtPhone
//
txtPhone.Location = new System.Drawing.Point(405, 39);
txtPhone.Name = "txtPhone";
txtPhone.Size = new System.Drawing.Size(203, 20);
txtPhone.TabIndex = 14;
txtPhone.TextChanged += new System.EventHandler(this.txtPhone_TextChanged);
//
// txtLast
//
txtLast.Location = new System.Drawing.Point(189, 39);
txtLast.Name = "txtLast";
txtLast.Size = new System.Drawing.Size(121, 20);
txtLast.TabIndex = 13;
txtLast.TextChanged += new System.EventHandler(this.txtLast_TextChanged);
//
// txtFirst
//
txtFirst.Location = new System.Drawing.Point(72, 39);
txtFirst.Name = "txtFirst";
txtFirst.Size = new System.Drawing.Size(84, 20);
txtFirst.TabIndex = 12;
txtFirst.TextChanged += new System.EventHandler(this.txtFirst_TextChanged);
// grdOrderItems
//
grdOrderItems.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
grdOrderItems.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
grdOrderItems.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Qty,
this.Description,
this.UnitPrice,
this.ExtPrice,
this.SCode,
this.Options1,
this.Options2});
grdOrderItems.Location = new System.Drawing.Point(10, 19);
grdOrderItems.Name = "grdOrderItems";
grdOrderItems.Size = new System.Drawing.Size(732, 81);
grdOrderItems.TabIndex = 0;
grdOrderItems.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(grdOrderItems_CellValueChanged);
grdOrderItems.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this.grdOrderItems_RowsAdded);
//
// Qty
//
this.Qty.HeaderText = "Qty";
this.Qty.Name = "Qty";
this.Qty.Width = 48;
//
// Description
//
this.Description.HeaderText = "Description";
this.Description.Name = "Description";
this.Description.Width = 85;
//
// UnitPrice
//
this.UnitPrice.HeaderText = "Unit Price";
this.UnitPrice.Name = "UnitPrice";
this.UnitPrice.Width = 78;
//
// ExtPrice
//
this.ExtPrice.HeaderText = "Ext. Price3";
this.ExtPrice.Name = "ExtPrice";
this.ExtPrice.ReadOnly = true;
this.ExtPrice.Width = 83;
//
// SCode
//
this.SCode.HeaderText = "S-Code";
this.SCode.Name = "SCode";
this.SCode.Width = 67;
//
// Options1
//
this.Options1.HeaderText = "Options1";
this.Options1.Name = "Options1";
this.Options1.Width = 74;
//
// Options2
//
this.Options2.HeaderText = "Options2";
this.Options2.Name = "Options2";
this.Options2.Width = 74;
//
// groupBox3
//
this.groupBox3.BackColor = System.Drawing.Color.WhiteSmoke;
this.groupBox3.Controls.Add(this.checkBox1);
this.groupBox3.Controls.Add(this.cmdApplyTaxRate);
this.groupBox3.Controls.Add(this.txtTaxRate);
this.groupBox3.Controls.Add(this.lblSubTotal);
this.groupBox3.Controls.Add(this.lblTotal);
this.groupBox3.Controls.Add(this.txtTax);
this.groupBox3.Controls.Add(this.txtShipping);
this.groupBox3.Controls.Add(this.txtShippingMethod);
this.groupBox3.Controls.Add(this.cmdTaxLookup);
this.groupBox3.Controls.Add(this.lstShipping);
this.groupBox3.Controls.Add(this.cmdBrowser);
this.groupBox3.Controls.Add(this.groupBox4);
this.groupBox3.Controls.Add(grdOrderItems);
this.groupBox3.Location = new System.Drawing.Point(14, 443);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(749, 242);
this.groupBox3.TabIndex = 13;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Order Information";
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Checked = true;
this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox1.Location = new System.Drawing.Point(253, 224);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(257, 17);
this.checkBox1.TabIndex = 21;
this.checkBox1.Text = "Automatically Apply Tax Rate with order changes";
this.checkBox1.UseVisualStyleBackColor = true;
//
// cmdApplyTaxRate
//
this.cmdApplyTaxRate.Location = new System.Drawing.Point(313, 194);
this.cmdApplyTaxRate.Name = "cmdApplyTaxRate";
this.cmdApplyTaxRate.Size = new System.Drawing.Size(126, 24);
this.cmdApplyTaxRate.TabIndex = 20;
this.cmdApplyTaxRate.Text = "Apply Tax Rate";
this.cmdApplyTaxRate.UseVisualStyleBackColor = true;
//
// txtTaxRate
//
this.txtTaxRate.Location = new System.Drawing.Point(186, 198);
this.txtTaxRate.Name = "txtTaxRate";
this.txtTaxRate.Size = new System.Drawing.Size(101, 20);
this.txtTaxRate.TabIndex = 19;
//
// lblSubTotal
//
this.lblSubTotal.AutoSize = true;
this.lblSubTotal.Location = new System.Drawing.Point(571, 128);
this.lblSubTotal.Name = "lblSubTotal";
this.lblSubTotal.Size = new System.Drawing.Size(13, 13);
this.lblSubTotal.TabIndex = 18;
this.lblSubTotal.Text = "$";
//
// lblTotal
//
this.lblTotal.AutoSize = true;
this.lblTotal.Location = new System.Drawing.Point(571, 210);
this.lblTotal.Name = "lblTotal";
this.lblTotal.Size = new System.Drawing.Size(13, 13);
this.lblTotal.TabIndex = 17;
this.lblTotal.Text = "$";
//
// txtTax
//
this.txtTax.Location = new System.Drawing.Point(561, 177);
this.txtTax.Name = "txtTax";
this.txtTax.Size = new System.Drawing.Size(182, 20);
this.txtTax.TabIndex = 16;
//
// txtShipping
//
this.txtShipping.Location = new System.Drawing.Point(561, 149);
this.txtShipping.Name = "txtShipping";
this.txtShipping.Size = new System.Drawing.Size(182, 20);
this.txtShipping.TabIndex = 15;
//
// txtShippingMethod
//
this.txtShippingMethod.Location = new System.Drawing.Point(559, 105);
this.txtShippingMethod.Name = "txtShippingMethod";
this.txtShippingMethod.Size = new System.Drawing.Size(182, 20);
this.txtShippingMethod.TabIndex = 14;
//
// cmdTaxLookup
//
this.cmdTaxLookup.Location = new System.Drawing.Point(138, 218);
this.cmdTaxLookup.Name = "cmdTaxLookup";
this.cmdTaxLookup.Size = new System.Drawing.Size(99, 21);
this.cmdTaxLookup.TabIndex = 13;
this.cmdTaxLookup.Text = "Lookup Tax Rate";
this.cmdTaxLookup.UseVisualStyleBackColor = true;
//
// lstShipping
//
this.lstShipping.FormattingEnabled = true;
this.lstShipping.Location = new System.Drawing.Point(304, 106);
this.lstShipping.Name = "lstShipping";
this.lstShipping.Size = new System.Drawing.Size(150, 69);
this.lstShipping.TabIndex = 5;
//
// cmdBrowser
//
this.cmdBrowser.Location = new System.Drawing.Point(16, 106);
this.cmdBrowser.Name = "cmdBrowser";
this.cmdBrowser.Size = new System.Drawing.Size(164, 21);
this.cmdBrowser.TabIndex = 2;
this.cmdBrowser.Text = "Launch Browser Form";
this.cmdBrowser.UseVisualStyleBackColor = true;
this.cmdBrowser.Click += new System.EventHandler(this.cmdBrowser_Click);
//
// groupBox4
//
this.groupBox4.Controls.Add(this.cmdSendRequest);
this.groupBox4.Location = new System.Drawing.Point(18, 133);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(162, 57);
this.groupBox4.TabIndex = 1;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "PayPal";
//
// cmdSendRequest
//
this.cmdSendRequest.Location = new System.Drawing.Point(7, 19);
this.cmdSendRequest.Name = "cmdSendRequest";
this.cmdSendRequest.Size = new System.Drawing.Size(131, 32);
this.cmdSendRequest.TabIndex = 2;
this.cmdSendRequest.Text = "Send Payment Request";
this.cmdSendRequest.UseVisualStyleBackColor = true;
this.cmdSendRequest.Click += new System.EventHandler(this.cmdSendRequest_Click_1);
//
// cmdClear
//
this.cmdClear.Location = new System.Drawing.Point(16, 696);
this.cmdClear.Name = "cmdClear";
this.cmdClear.Size = new System.Drawing.Size(67, 25);
this.cmdClear.TabIndex = 14;
this.cmdClear.Text = "Clear";
this.cmdClear.UseVisualStyleBackColor = true;
//
// cmdSave
//
this.cmdSave.Location = new System.Drawing.Point(91, 697);
this.cmdSave.Name = "cmdSave";
this.cmdSave.Size = new System.Drawing.Size(67, 25);
this.cmdSave.TabIndex = 15;
this.cmdSave.Text = "Save";
this.cmdSave.UseVisualStyleBackColor = true;
//
// cmdPrint
//
this.cmdPrint.Location = new System.Drawing.Point(164, 697);
this.cmdPrint.Name = "cmdPrint";
this.cmdPrint.Size = new System.Drawing.Size(67, 25);
this.cmdPrint.TabIndex = 16;
this.cmdPrint.Text = "Print";
this.cmdPrint.UseVisualStyleBackColor = true;
//
// cmdSavePrintEmail
//
this.cmdSavePrintEmail.Location = new System.Drawing.Point(393, 694);
this.cmdSavePrintEmail.Name = "cmdSavePrintEmail";
this.cmdSavePrintEmail.Size = new System.Drawing.Size(131, 31);
this.cmdSavePrintEmail.TabIndex = 17;
this.cmdSavePrintEmail.Text = "Save, Print, and Email";
this.cmdSavePrintEmail.UseVisualStyleBackColor = true;
//
// frmMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Aqua;
this.ClientSize = new System.Drawing.Size(790, 725);
this.Controls.Add(this.cmdSavePrintEmail);
this.Controls.Add(this.cmdPrint);
this.Controls.Add(this.cmdSave);
this.Controls.Add(this.cmdClear);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.cmdBrowse);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.optRequestNew);
this.Controls.Add(this.optRequestShipping);
this.Controls.Add(this.optRequestAdd);
this.Controls.Add(this.cmdEdit);
this.Controls.Add(this.txtTransactionNumber);
this.Controls.Add(this.cmdLookup);
this.Controls.Add(this.cmdExit);
this.Controls.Add(this.cmdSendEmail);
this.Name = "frmMain";
this.Text = "PayPal Payment Request Program version C1";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(grdOrderItems)).EndInit();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.groupBox4.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button cmdSendEmail;
private System.Windows.Forms.Button cmdExit;
private System.Windows.Forms.Button cmdLookup;
private System.Windows.Forms.TextBox txtTransactionNumber;
private System.Windows.Forms.Button cmdEdit;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button cmdBrowse;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button cmdCustomer;
private System.Windows.Forms.TextBox txtNotes;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.Button cmdSendRequest;
private System.Windows.Forms.Button cmdBrowser;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Button cmdApplyTaxRate;
private System.Windows.Forms.TextBox txtTaxRate;
private System.Windows.Forms.TextBox txtTax;
private System.Windows.Forms.TextBox txtShipping;
private System.Windows.Forms.TextBox txtShippingMethod;
private System.Windows.Forms.Button cmdTaxLookup;
private System.Windows.Forms.ListBox lstShipping;
private System.Windows.Forms.Button cmdClear;
private System.Windows.Forms.Button cmdSave;
private System.Windows.Forms.Button cmdPrint;
private System.Windows.Forms.Button cmdSavePrintEmail;
private System.Windows.Forms.DataGridViewTextBoxColumn Qty;
private System.Windows.Forms.DataGridViewTextBoxColumn Description;
private System.Windows.Forms.DataGridViewTextBoxColumn UnitPrice;
private System.Windows.Forms.DataGridViewTextBoxColumn ExtPrice;
private System.Windows.Forms.DataGridViewTextBoxColumn SCode;
private System.Windows.Forms.DataGridViewTextBoxColumn Options1;
private System.Windows.Forms.DataGridViewTextBoxColumn Options2;
public static System.Windows.Forms.TextBox txtEmail;
public static System.Windows.Forms.TextBox txtPhone;
public static System.Windows.Forms.TextBox txtLast;
public static System.Windows.Forms.TextBox txtCountry;
public static System.Windows.Forms.TextBox txtState;
public static System.Windows.Forms.TextBox txtAddress;
public static System.Windows.Forms.TextBox txtPayPalEmail;
public static System.Windows.Forms.DataGridView grdOrderItems;
public static System.Windows.Forms.TextBox txtZip;
public static System.Windows.Forms.TextBox txtCity;
public static System.Windows.Forms.TextBox txtName;
public static System.Windows.Forms.TextBox txtFirst;
}
}
If you look into the .designer.cs file carefully you will notice this little comment
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
Basically that part of the code are auto generated by the designer and should never be modified manually.
And yes this is considered bad practice. Firstly the control should be self contained inside the form instead of being a static one. Static just means there will only be one instance across all the forms of the same class.
Direct modification of UI from one form to another is considered anti-pattern as well. You should be making a Data model and have the UI reads the updated data when they are changed. I know this might be a bit challenging for beginners as well for winform.
So if you want to take the shortcut you can expose a public property on the other form and by modifying the property your other form will update the appropriate control.
Edit:
In the other form's code behind (.cs file)
you can do like
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string SelectedUser { get; private set; }
}
and then in your main form you would listen to the close event
Form1 other = new Form1();
other.FormClosed += Other_FormClosed;
other.Show();
...
private void Other_FormClosed(object sender, FormClosedEventArgs e)
{
Form1 other = (Form1)sender;
string selectedUser = other.SelectedUser;
//do something
}
Why is the code time and again returning me the following error:
Error 1 The call is ambiguous between the following methods or properties: 'reClientOnly_winforms.Form1.InitializeComponent()' and 'reClientOnly_winforms.Form1.InitializeComponent()'
Code:
namespace reClientOnly_winforms
{
public partial class Form1 : Form
{
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.ClientSize = new System.Drawing.Size(284, 261);
this.Name = "Form1";
this.Load += new System.EventHandler(this.Form1_Load_1);
this.ResumeLayout(false);
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
msg("Client Started");
clientSocket.Connect("127.0.0.1", 8888);
label1.Text = "Client Socket Program - Server Connected ...";
}
private void button1_Click(object sender, EventArgs e)
{
NetworkStream serverStream = clientSocket.GetStream();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + "$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
byte[] inStream = new byte[10025];
serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
msg(returndata);
textBox2.Text = "";
textBox2.Focus();
}
public void msg(string mesg)
{
textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
}
}
}
EDIT : Taken from Form1.Designer.cs
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(85, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 68);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(297, 20);
this.textBox1.TabIndex = 1;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(88, 197);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(297, 20);
this.textBox2.TabIndex = 2;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(85, 154);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 3;
this.label2.Text = "label2";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(96, 42);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(35, 13);
this.label3.TabIndex = 4;
this.label3.Text = "label3";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(273, 272);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(100, 20);
this.textBox3.TabIndex = 5;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(432, 316);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Load += new System.EventHandler(this.Form1_Load_1);
this.ResumeLayout(false);
this.PerformLayout();
}
I was having the same issue so i created a new project to get resolve the issue ( previously was matching in Form1.Designer.cs) .
How to get around this?I saw this but wasn't conclusive
You cannot have TWO InitializeComponent() methods
InitializeComponent is a method automatically written for you by the Form Designer when you create/change your forms.
So you cannot write a method having the name InitializeComponent()and call it so compiler won't understand "what method" to select
What you can do
public Form1()
{
InitializeComponent();
}
private void Re_InitializeComponent()
{
InitializeComponent();
this.SuspendLayout();
//
// Form1
//
this.ClientSize = new System.Drawing.Size(284, 261);
this.Name = "Form1";
this.Load += new System.EventHandler(this.Form1_Load_1);
this.ResumeLayout(false);
}
And when you want to Do the thing you want call Re_InitializeComponent() in that function. As follows
public void YourCalling(){
Re_InitializeComponent();
}
p.s- I tested this in on one of my projects. it gives a minimized version of same layout and I guess that's what you are expecting
Your Form1 class is partial, another part of the class is in Form1.Designer.cs file. You have InitializeComponent() in your Form1 and there is another in Form1.Designer.cs. Try to remove one in Form1 and put all of his content to Form1.Designer.cs
I'm trying to do something that i would think to be rather simple. I have 2 toolstrip items.
test1: ToolStripMenuItem which should show panel -> test_panel_1
test2: ToolStripMenuItem which should show panel -> panel1
test_panel_1 contains button -> panel_1_button1 which should hide test_panel_1 and show test_panel_2
panel1 contains button -> button1 which should hide test_panel2 and then show panel2
However, when I run the code and click on test1ToolStripMenuItem it shows test_panel_1 like it's supposed to, then when i click on panel_1_button_1 it just clears test_panel_1 and doesn't show test_panel_2. And regardless of what I click first, test2ToolStripMenuItem doesn't show panel1 at all.
Here's my code...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace panel_test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel_1_button1_Click(object sender, EventArgs e)
{
test_panel2.Visible = true;
test_panel_1.Visible = false;
}
private void test1ToolStripMenuItem_Click(object sender, EventArgs e)
{
test_panel_1.Visible = true;
panel1.Visible = false;
}
private void button1_click(object sender, EventArgs e)
{
test_panel_1.Visible = false;
test_panel2.Visible = true;
}
private void test2ToolStripMenuItem_Click(object sender, EventArgs e)
{
test_panel2.Visible = false;
panel1.Visible = true;
}
private void button1_Click_1(object sender, EventArgs e)
{
this.panel1.Visible = false;
this.panel2.Visible = true;
}
}
}
and, not sure if this helps, but...
namespace panel_test
{
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.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.testToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.test1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.test2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.test3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.test_panel_1 = new System.Windows.Forms.Panel();
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.panel2 = new System.Windows.Forms.Panel();
this.label3 = new System.Windows.Forms.Label();
this.test_panel2 = new System.Windows.Forms.Panel();
this.label2 = new System.Windows.Forms.Label();
this.panel_1_button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.menuStrip1.SuspendLayout();
this.test_panel_1.SuspendLayout();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.test_panel2.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.testToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(467, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// testToolStripMenuItem
//
this.testToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.test1ToolStripMenuItem,
this.test2ToolStripMenuItem,
this.test3ToolStripMenuItem});
this.testToolStripMenuItem.Name = "testToolStripMenuItem";
this.testToolStripMenuItem.Size = new System.Drawing.Size(38, 20);
this.testToolStripMenuItem.Text = "test";
//
// test1ToolStripMenuItem
//
this.test1ToolStripMenuItem.Name = "test1ToolStripMenuItem";
this.test1ToolStripMenuItem.Size = new System.Drawing.Size(99, 22);
this.test1ToolStripMenuItem.Text = "test1";
this.test1ToolStripMenuItem.Click += new System.EventHandler(this.test1ToolStripMenuItem_Click);
//
// test2ToolStripMenuItem
//
this.test2ToolStripMenuItem.Name = "test2ToolStripMenuItem";
this.test2ToolStripMenuItem.Size = new System.Drawing.Size(99, 22);
this.test2ToolStripMenuItem.Text = "test2";
this.test2ToolStripMenuItem.Click += new System.EventHandler(this.test2ToolStripMenuItem_Click);
//
// test3ToolStripMenuItem
//
this.test3ToolStripMenuItem.Name = "test3ToolStripMenuItem";
this.test3ToolStripMenuItem.Size = new System.Drawing.Size(99, 22);
this.test3ToolStripMenuItem.Text = "test3";
//
// test_panel_1
//
this.test_panel_1.Controls.Add(this.test_panel2);
this.test_panel_1.Controls.Add(this.panel_1_button1);
this.test_panel_1.Controls.Add(this.label1);
this.test_panel_1.Location = new System.Drawing.Point(44, 28);
this.test_panel_1.Name = "test_panel_1";
this.test_panel_1.Size = new System.Drawing.Size(442, 317);
this.test_panel_1.TabIndex = 1;
this.test_panel_1.Visible = false;
//
// panel1
//
this.panel1.Controls.Add(this.button1);
this.panel1.Location = new System.Drawing.Point(218, 187);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(328, 318);
this.panel1.TabIndex = 1;
this.panel1.Visible = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(86, 155);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "panel 3 to 4";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// panel2
//
this.panel2.Controls.Add(this.panel1);
this.panel2.Controls.Add(this.label3);
this.panel2.Location = new System.Drawing.Point(95, 194);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(398, 260);
this.panel2.TabIndex = 1;
this.panel2.Visible = false;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(86, 118);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(71, 13);
this.label3.TabIndex = 0;
this.label3.Text = "this is panel 4";
//
// test_panel2
//
this.test_panel2.Controls.Add(this.panel2);
this.test_panel2.Controls.Add(this.label2);
this.test_panel2.Location = new System.Drawing.Point(154, 101);
this.test_panel2.Name = "test_panel2";
this.test_panel2.Size = new System.Drawing.Size(358, 237);
this.test_panel2.TabIndex = 3;
this.test_panel2.Visible = false;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(70, 118);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(71, 13);
this.label2.TabIndex = 0;
this.label2.Text = "this is panel 2";
//
// panel_1_button1
//
this.panel_1_button1.Location = new System.Drawing.Point(86, 155);
this.panel_1_button1.Name = "panel_1_button1";
this.panel_1_button1.Size = new System.Drawing.Size(75, 23);
this.panel_1_button1.TabIndex = 2;
this.panel_1_button1.Text = "button1";
this.panel_1_button1.UseVisualStyleBackColor = true;
this.panel_1_button1.Click += new System.EventHandler(this.panel_1_button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(28, 21);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(42, 13);
this.label1.TabIndex = 0;
this.label1.Text = "panel 1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(467, 357);
this.Controls.Add(this.test_panel_1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.Text = "Form1";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.test_panel_1.ResumeLayout(false);
this.test_panel_1.PerformLayout();
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.test_panel2.ResumeLayout(false);
this.test_panel2.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem testToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem test1ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem test2ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem test3ToolStripMenuItem;
private System.Windows.Forms.Panel test_panel_1;
private System.Windows.Forms.Panel test_panel2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button panel_1_button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button1;
}
}
I've toyed with Usercontrols a little, and i like the way i can edit and view them much better than panels; however, i don't know how to control hiding and showing them.
Thanks for the help. I know this must be coding 101, but it's something i haven't quite fully figured out yet.
It looks like your test_panel_2 is a child of test_panel_1. panel1 is a child of panel2. This is likely not what you intended. What's happening is that, because test_panel_2 is inside test_panel_1, hiding test_panel_1 also hides test_panel_2. There's a hierarchy there.
There's one or two spots where you set the visibility to false again - I'm not sure if those are correct.
Is there any way to add a System.Windows.Controls.TextBox to GroupBox controls in C#?
I tried the following but it doesn't show up in the groupbox:
public System.Windows.Controls.TextBox textBox6 = new System.Windows.Controls.TextBox();
public System.Windows.Controls.TextBox textBox7 = new System.Windows.Controls.TextBox();
public ElementHost sumtext = new ElementHost();
public ElementHost loctext = new ElementHost();
private void Form1_Load(object sender, EventArgs e)
{
textBox6.Name = "Summary";
textBox7.Name = "Location";
textBox6.FontFamily = new System.Windows.Media.FontFamily("Microsoft Sans Serif");
textBox6.FontSize = 12;
textBox6.SpellCheck.IsEnabled = true;
textBox7.FontFamily = new System.Windows.Media.FontFamily("Microsoft Sans Serif");
textBox7.FontSize = 12;
textBox7.SpellCheck.IsEnabled = true;
groupBox4.Controls.Add(sumtext);
sumtext.Dock = DockStyle.None;
sumtext.Width = 246;
sumtext.Height = 35;
sumtext.Child = textBox6;
sumtext.Location = new Point(3, 33);
sumtext.Visible = true;
sumtext.Enabled = false;
groupBox4.Controls.Add(sumtext);
groupBox4.Controls.Add(loctext);
loctext.Dock = DockStyle.None;
loctext.Width = 246;
loctext.Height = 35;
loctext.Child = textBox7;
loctext.Location = new Point(3, 90);
loctext.Visible = true;
loctext.Enabled = false;
this.Controls.Add(sumtext);
this.Controls.Add(loctext);
}
I need to use System.Windows.Controls.TextBox rather than Form.TextBox as I need it for spell check.
Any help would be greatly appreciated!
I changed the Enabled property of the sumtext, and removed the other box to shorten it:
This code works for me:
public Form1()
{
this.Load += new System.EventHandler(this.Form1_Load);
}
public System.Windows.Controls.TextBox textBox6 = new System.Windows.Controls.TextBox();
public ElementHost sumtext = new ElementHost();
private System.Windows.Forms.GroupBox groupBox4;
private void Form1_Load(object sender, EventArgs e)
{
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.SuspendLayout();
//
// groupBox4
//
this.groupBox4.Location = new System.Drawing.Point(57, 63);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(591, 238);
this.groupBox4.TabIndex = 0;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "groupBox1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(706, 478);
this.Controls.Add(this.groupBox4);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
textBox6.Name = "Summary";
textBox6.FontFamily = new System.Windows.Media.FontFamily("Microsoft Sans Serif");
textBox6.FontSize = 12;
textBox6.SpellCheck.IsEnabled = true;
groupBox4.Controls.Add(sumtext);
sumtext.Dock = DockStyle.None;
sumtext.Width = 246;
sumtext.Height = 35;
sumtext.Child = textBox6;
sumtext.Location = new Point(3, 33);
sumtext.Visible = true;
sumtext.Enabled = true;
groupBox4.Controls.Add(sumtext);
}
Is this code actually getting called? Has groupbox 4 been added to the form yet?
You should not be adding your ElementHost controls to your Form AND your GroupBox, it appears to be confusing .NET. Keeping your original code exactly as-is but commenting out these two lines makes it work:
//this.Controls.Add(sumtext);
//this.Controls.Add(loctext);
Also... I don't think it's hurting anything, but you don't need to do this twice:
//groupBox4.Controls.Add(sumtext);