• 0

[C# .Net] Accessing other form controls


Question

Like, I have Label1 on Form1 and Form2 is the setting window. I open Form2 and change the text settings of Form1.Label1.

How would I do that?

In Vb6, it use to be Form1.Label1.text = "whatever";

I'm pretty new to C#, so sorry for the noobish question.

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

make a property reference to them is one way...

which is also the best way if your doing OOP style programming

so in the form that has the label you'd have

(this is a VB.NET code it's easily converted to C#)

Public Property LabelText as string

Get

return me.lbltext.text

end get

Set (byval Value as string)

me.lbltext.text = value

end set

End Property

that will make a publically accessable property to where ever the form is in scope, then you can set it like any normal property

myform.labeltext = "text to put here"

Link to comment
Share on other sites

  • 0

here's a working example of what you're looking to do

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace WindowsApplication1

{

// form1

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Label label1;

private System.ComponentModel.Container components = null;

public string LabelText

{

set { this.label1.Text = value; }

}

public Form1()

{

InitializeComponent();

}

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.label1 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(8, 56);

this.button1.Name = "button1";

this.button1.TabIndex = 0;

this.button1.Text = "button1";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// label1

//

this.label1.Location = new System.Drawing.Point(16, 16);

this.label1.Name = "label1";

this.label1.TabIndex = 1;

this.label1.Text = "label1";

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(160, 93);

this.Controls.Add(this.label1);

this.Controls.Add(this.button1);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

[sTAThread]

static void Main()

{

Application.Run(new Form1());

}

private void button1_Click(object sender, System.EventArgs e)

{

Form2 dlg = new Form2();

if(dlg.ShowDialog() == DialogResult.OK)

{

this.LabelText = dlg.TextboxText;

}

}

}

// form2

public class Form2 : System.Windows.Forms.Form

{

private System.Windows.Forms.TextBox textBox1;

private System.Windows.Forms.Button button1;

private System.ComponentModel.Container components = null;

public string TextboxText

{

get { return this.textBox1.Text; }

}

public Form2()

{

InitializeComponent();

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(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.textBox1 = new System.Windows.Forms.TextBox();

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// textBox1

//

this.textBox1.Location = new System.Drawing.Point(8, 8);

this.textBox1.Name = "textBox1";

this.textBox1.TabIndex = 0;

this.textBox1.Text = "textBox1";

//

// button1

//

this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;

this.button1.Location = new System.Drawing.Point(8, 40);

this.button1.Name = "button1";

this.button1.TabIndex = 1;

this.button1.Text = "button1";

//

// Form2

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(152, 69);

this.Controls.Add(this.button1);

this.Controls.Add(this.textBox1);

this.Name = "Form2";

this.Text = "Form2";

this.ResumeLayout(false);

}

#endregion

}

}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.