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;
}
Related
I have a issue : I have a main form who plays music when loaded : and I made a function who plays a sound when a button is pressed : the next form is minimized and their content too (from config.cs to every form), it's supposed to be at fullscreen and I want to keep it fullscreen and not minimized when playing a sound.
Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using System.IO;
using System.Runtime.InteropServices;
namespace Poker
{
public partial class Main : Form
{
[DllImport("wmp.dll")]
static extern Int32 mciSendString(string command, StringBuilder buffer, int bufferSize, IntPtr hwndCallback);
//LoadForm lform = new LoadForm();
Config config = new Config();
Settings settings = new Settings();
public Main()
{
InitializeComponent();
}
public void Main_Load(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
new System.Threading.Thread(() =>
{
try
{
player1.PlayLooping();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,
"Impossible de jouer le morceau.",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}).Start();
}
private void btnJouer_Click_1(object sender, EventArgs e)
{
btnClick();
this.Hide();
config.Show();
}
Sound function :
void btnClick()
{
new System.Threading.Thread(() =>
{
try
{
var _bt = new System.Windows.Media.MediaPlayer();
_bt.Open(new System.Uri(Environment.CurrentDirectory + "\\sfx\\button.wav"));
_bt.Play();
}
catch(Exception xe)
{
MessageBox.Show(xe.Message,
"Impossible de jouer ce son.",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}).Start();
}
Main form code :
namespace Poker
{
partial class Main
{
/// <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.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Main));
this.titre = new System.Windows.Forms.TableLayoutPanel();
this.label1 = new System.Windows.Forms.Label();
this.btnQuitter = new System.Windows.Forms.Button();
this.btnJouer = new System.Windows.Forms.Button();
this.btnPara = new System.Windows.Forms.Button();
this.titre.SuspendLayout();
this.SuspendLayout();
//
// titre
//
this.titre.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.titre.AutoSize = true;
this.titre.BackColor = System.Drawing.Color.Transparent;
this.titre.ColumnCount = 1;
this.titre.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.titre.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.titre.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.titre.Controls.Add(this.label1, 0, 0);
this.titre.Location = new System.Drawing.Point(0, 0);
this.titre.Name = "titre";
this.titre.RowCount = 1;
this.titre.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.titre.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.titre.Size = new System.Drawing.Size(1920, 300);
this.titre.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Segoe UI Semibold", 36F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.ForeColor = System.Drawing.Color.White;
this.label1.Location = new System.Drawing.Point(3, 0);
this.label1.MinimumSize = new System.Drawing.Size(100, 100);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(225, 100);
this.label1.TabIndex = 0;
this.label1.Text = "Poker";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnQuitter
//
this.btnQuitter.BackColor = System.Drawing.Color.Transparent;
this.btnQuitter.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnQuitter.Font = new System.Drawing.Font("Segoe UI", 16F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnQuitter.ForeColor = System.Drawing.Color.White;
this.btnQuitter.Location = new System.Drawing.Point(258, 754);
this.btnQuitter.Name = "btnQuitter";
this.btnQuitter.Size = new System.Drawing.Size(1361, 95);
this.btnQuitter.TabIndex = 3;
this.btnQuitter.Text = "Quitter";
this.btnQuitter.UseVisualStyleBackColor = false;
this.btnQuitter.Click += new System.EventHandler(this.btnQuitter_Click);
//
// btnJouer
//
this.btnJouer.BackColor = System.Drawing.Color.Transparent;
this.btnJouer.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnJouer.Font = new System.Drawing.Font("Segoe UI", 16F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnJouer.ForeColor = System.Drawing.Color.White;
this.btnJouer.Location = new System.Drawing.Point(258, 489);
this.btnJouer.Name = "btnJouer";
this.btnJouer.Size = new System.Drawing.Size(1361, 95);
this.btnJouer.TabIndex = 4;
this.btnJouer.Text = "Jouer";
this.btnJouer.UseVisualStyleBackColor = false;
this.btnJouer.Click += new System.EventHandler(this.btnJouer_Click_1);
//
// btnPara
//
this.btnPara.BackColor = System.Drawing.Color.Transparent;
this.btnPara.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnPara.Font = new System.Drawing.Font("Segoe UI", 16F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnPara.ForeColor = System.Drawing.Color.White;
this.btnPara.Location = new System.Drawing.Point(258, 621);
this.btnPara.Name = "btnPara";
this.btnPara.Size = new System.Drawing.Size(1361, 95);
this.btnPara.TabIndex = 5;
this.btnPara.Text = "Paramètres";
this.btnPara.UseVisualStyleBackColor = false;
this.btnPara.Click += new System.EventHandler(this.btnPara_Click);
//
// Main
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(1920, 1046);
this.Controls.Add(this.btnPara);
this.Controls.Add(this.btnJouer);
this.Controls.Add(this.btnQuitter);
this.Controls.Add(this.titre);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Main";
this.Text = "Poker";
this.Load += new System.EventHandler(this.Main_Load);
this.titre.ResumeLayout(false);
this.titre.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TableLayoutPanel titre;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnQuitter;
private System.Windows.Forms.Button btnJouer;
private System.Windows.Forms.Button btnPara;
}
}
Target form : (config.cs)
namespace Poker
{
partial class Config
{
/// <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.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(525, 270);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(113, 65);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Config
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Config";
this.Text = "Configuration";
this.Load += new System.EventHandler(this.Config_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
}
The problem is you creating a new thread, and the Program process switches to the second thread.
How to solve: Create one object of MediaPlayer, and switch a song, when you need.
Or: Create two separated MediaPlayers and pause one when you need.
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);
Newbie question. I downloaded the source of a project written in C#. There is an error occurring in one of the designer files used for constructing the GUI. Source attached below:
namespace Neurotec.Samples
{
partial class MainForm
{
/// <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.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPageEnrollFromFile = new System.Windows.Forms.TabPage();
this.enrollFromFilePanel = new Neurotec.Samples.EnrollFromFile();
this.tabPageEnrollFromMicrophone = new System.Windows.Forms.TabPage();
this.enrollFromMicrophonePanel = new Neurotec.Samples.EnrollFromMicrophone();
this.tabPageVerify = new System.Windows.Forms.TabPage();
this.verifyVoicePanel = new Neurotec.Samples.VerifyVoice();
this.tabPageIdentify = new System.Windows.Forms.TabPage();
this.identifyVoicePanel = new Neurotec.Samples.IdentifyVoice();
this.tabControl1.SuspendLayout();
this.tabPageEnrollFromFile.SuspendLayout();
this.tabPageEnrollFromMicrophone.SuspendLayout();
this.tabPageVerify.SuspendLayout();
this.tabPageIdentify.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPageEnrollFromFile);
this.tabControl1.Controls.Add(this.tabPageEnrollFromMicrophone);
this.tabControl1.Controls.Add(this.tabPageVerify);
this.tabControl1.Controls.Add(this.tabPageIdentify);
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Location = new System.Drawing.Point(0, 0);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(551, 398);
this.tabControl1.TabIndex = 0;
this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.TabControlSelectedIndexChanged);
//
// enrollFromFilePanel
//
this.enrollFromFilePanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.enrollFromFilePanel.Location = new System.Drawing.Point(3, 3);
this.enrollFromFilePanel.Name = "enrollFromFilePanel";
this.enrollFromFilePanel.Size = new System.Drawing.Size(540, 316);
this.enrollFromFilePanel.TabIndex = 0;
//
// tabPageEnrollFromFile
//
this.tabPageEnrollFromFile.Controls.Add(this.enrollFromFilePanel);
this.tabPageEnrollFromFile.Location = new System.Drawing.Point(4, 22);
this.tabPageEnrollFromFile.Name = "tabPageEnrollFromFile";
this.tabPageEnrollFromFile.Padding = new System.Windows.Forms.Padding(3);
this.tabPageEnrollFromFile.Size = new System.Drawing.Size(546, 322);
this.tabPageEnrollFromFile.TabIndex = 0;
this.tabPageEnrollFromFile.Text = "Enroll From File";
this.tabPageEnrollFromFile.UseVisualStyleBackColor = true;
//
// tabPageEnrollFromMicrophone
//
this.tabPageEnrollFromMicrophone.Controls.Add(this.enrollFromMicrophonePanel);
this.tabPageEnrollFromMicrophone.Location = new System.Drawing.Point(4, 22);
this.tabPageEnrollFromMicrophone.Name = "tabPageEnrollFromMicrophone";
this.tabPageEnrollFromMicrophone.Padding = new System.Windows.Forms.Padding(3);
this.tabPageEnrollFromMicrophone.Size = new System.Drawing.Size(546, 322);
this.tabPageEnrollFromMicrophone.TabIndex = 1;
this.tabPageEnrollFromMicrophone.Text = "Enroll From Microphone";
this.tabPageEnrollFromMicrophone.UseVisualStyleBackColor = true;
//
// enrollFromMicrophonePanel
//
this.enrollFromMicrophonePanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.enrollFromMicrophonePanel.Location = new System.Drawing.Point(3, 3);
this.enrollFromMicrophonePanel.Name = "enrollFromMicrophonePanel";
this.enrollFromMicrophonePanel.Size = new System.Drawing.Size(540, 316);
this.enrollFromMicrophonePanel.TabIndex = 0;
//
// tabPageVerify
//
this.tabPageVerify.Controls.Add(this.verifyVoicePanel);
this.tabPageVerify.Location = new System.Drawing.Point(4, 22);
this.tabPageVerify.Name = "tabPageVerify";
this.tabPageVerify.Size = new System.Drawing.Size(546, 322);
this.tabPageVerify.TabIndex = 2;
this.tabPageVerify.Text = "Verify Voice";
this.tabPageVerify.UseVisualStyleBackColor = true;
//
// verifyVoicePanel
//
this.verifyVoicePanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.verifyVoicePanel.Location = new System.Drawing.Point(0, 0);
this.verifyVoicePanel.Name = "verifyVoicePanel";
this.verifyVoicePanel.Size = new System.Drawing.Size(546, 322);
this.verifyVoicePanel.TabIndex = 0;
//
// tabPageIdentify
//
this.tabPageIdentify.Controls.Add(this.identifyVoicePanel);
this.tabPageIdentify.Location = new System.Drawing.Point(4, 22);
this.tabPageIdentify.Name = "tabPageIdentify";
this.tabPageIdentify.Size = new System.Drawing.Size(543, 372);
this.tabPageIdentify.TabIndex = 3;
this.tabPageIdentify.Text = "Identify Voice";
this.tabPageIdentify.UseVisualStyleBackColor = true;
//
// identifyVoicePanel
//
this.identifyVoicePanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.identifyVoicePanel.Location = new System.Drawing.Point(0, 0);
this.identifyVoicePanel.Name = "identifyVoicePanel";
this.identifyVoicePanel.Size = new System.Drawing.Size(543, 372);
this.identifyVoicePanel.TabIndex = 0;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(551, 398);
this.Controls.Add(this.tabControl1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "MainForm";
this.Text = "Simple Voices Sample";
this.Load += new System.EventHandler(this.MainFormLoad);
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainFormFormClosing);
this.tabControl1.ResumeLayout(false);
this.tabPageEnrollFromFile.ResumeLayout(false);
this.tabPageEnrollFromMicrophone.ResumeLayout(false);
this.tabPageVerify.ResumeLayout(false);
this.tabPageIdentify.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPageEnrollFromFile;
private System.Windows.Forms.TabPage tabPageEnrollFromMicrophone;
private System.Windows.Forms.TabPage tabPageVerify;
private System.Windows.Forms.TabPage tabPageIdentify;
private EnrollFromFile enrollFromFilePanel;
private EnrollFromMicrophone enrollFromMicrophonePanel;
private VerifyVoice verifyVoicePanel;
private IdentifyVoice identifyVoicePanel;
}
}
The errors state that some variables were undeclared or unassigned. As an example, the variable "enrollFromFilePanel" is said to be undeclared but it was created with the line "private EnrollFromFile enrollFromFilePanel;". I am sorry that I cannot provide an sscce and I hope that the solution is something simple. Thank you.
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;