Windows Develop Bookmark and Share   
 index > Windows Forms General > changing a label Visible property from another form??
 

changing a label Visible property from another form??

Hi
I have 2 forms, form1 and form2. i have some label controls in the form1 and their Visible property are set to FALSE. now i want to change their Visible property and Text property from form2. i created a method in form2 that calls another method in the form1 and send it strings for the Text property of the labels, but the method in the form1 cannot change the Visible property. when i run the program and click the required button in form2 nothing happen. please tell me why?
thanks

  • Moved byTaylorMichaelLMVPTuesday, September 22, 2009 4:58 PMWinForms related (From:Visual C# General)
  •  
FMZL  Tuesday, September 22, 2009 4:31 PM
Hi

Write a new Constructor and define a member to form 2.

private Form1 frm1=null;
public Form2(Form1 frm)
{
InitializeComponent();
frm1=frm;
}

At Form1 whilecreating an instance ofform2 use this constructor.

Form2 frm2=new Form2(this);
frm2.Show();

At Form2 Write this code to the click event of button

((Label)frm.Controls.Find("myLabel",true)[0]).Visible=false; /or true
  • Marked As Answer byFMZL Tuesday, September 22, 2009 7:08 PM
  •  
Tamer Oz  Tuesday, September 22, 2009 6:58 PM

sorry, but i dont understand. it doesnt work.

FMZL  Tuesday, September 22, 2009 5:13 PM
the problem is I cannot change the visible property of a label resides on the form1 through a button tha resides on the form2. i write the code, no compile error, ijust doesnt work. nothing happens??
why? thats my problem
FMZL  Tuesday, September 22, 2009 5:20 PM
In form 2:

public string MyTextBoxText
{
get { return myTextBox.Text; }
set { myTextbox.Text = value; }
}

public bool MyTextBoxVisible
{
get { return myTextBox.Visible; }
set { myTextBox.Visible = value; }
}

In form 1 (must have a reference to form 2):

form2.TextBoxText = "Hello World";
form2.TextBoxVisible = true;

[Edit] Wasn't really paying attention, you want labels and you want it the other way around. So just replace myTextBox with the label name, and switch form 1/form 2

[Edit2] I'm assuming you're opening form 2 from form 1. So to get a reference to form 1 you'll need to either pass it in the constructor:

Form2 form2 = new Form2(this); // 'this' is form1

Or create an event in form 2 whenever you want it to change (more complicated but the correct way to do it, form2 doesn't even have to know form1 exists)
  • Proposed As Answer byAKGMA Tuesday, September 22, 2009 5:54 PM
  • Unproposed As Answer byFMZL Tuesday, September 22, 2009 6:53 PM
  •  
ScottyDoesKnow  Tuesday, September 22, 2009 5:47 PM

please send me your example.
farid_lavasani2004@hotmail.com

FMZL  Tuesday, September 22, 2009 5:54 PM
AKGMA why not use FindControl method of controls collection?

ex:

this

.Controls.Find("myControl",true)

Tamer Oz  Tuesday, September 22, 2009 6:27 PM
the label that i want to change its visible property resides on form1 but after opening form2 from form1 and doing some calculation i want to change the visible and text property of label that resides on form1 through a button that resides on form2.
it doesnt wirk with your solution.
regards
FMZL  Tuesday, September 22, 2009 6:51 PM
Hi

Write a new Constructor and define a member to form 2.

private Form1 frm1=null;
public Form2(Form1 frm)
{
InitializeComponent();
frm1=frm;
}

At Form1 whilecreating an instance ofform2 use this constructor.

Form2 frm2=new Form2(this);
frm2.Show();

At Form2 Write this code to the click event of button

((Label)frm.Controls.Find("myLabel",true)[0]).Visible=false; /or true
  • Marked As Answer byFMZL Tuesday, September 22, 2009 7:08 PM
  •  
Tamer Oz  Tuesday, September 22, 2009 6:58 PM

You can use google to search for other answers

Custom Search

More Threads

• Background Operation
• Refresh listbox from another form
• My application Winforms with Ora 10gXE don't run in other machine
• Resizing columns
• How to Get the location of a ToolStripMenuItem?
• Closing One Form Then Opening Another
• Using HierarchicalDataSet accross multiple tables
• Image not shown in Treeview Control Node
• TreeView control missing datasource property
• calling from script Control Library and Form