473,549 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing control properties on other forms

AMP
Hello,
I have an mdi program with a child form ("A") and another child ("B").
I want to change the text of a label on A by changing an item from a
combobox on B. I can code the comboBox1_Selec tedIndexChanged that is on
B but I dont know how to send that info to the label on A.I cant seem
to access A from B.
Help
Thanks
Mike

A.label from B. combobox.

Mar 7 '06 #1
6 6864
Just set B's label to public rather than the default visibility -- you
can do it in the control properties.

Or, cleaner than that, I would usually create a public string property
in B and have that set the text property of the private control.
Remember that B is a class like any other, and its public interface
should be minimal and should not expose the internals ... so a simple
public property is the best way from an OO standpoint. The public
property remains constant even if you decide later that a different
control or multiple controls need to respond to the change of value.
And the client code in A need know nothing about the actual control or
its name or even whether the value set goes to a control at all ... it
just needs to concern itself with informing B of the change via a nice
generically named public property.

--Bob

AMP wrote:
Hello,
I have an mdi program with a child form ("A") and another child ("B").
I want to change the text of a label on A by changing an item from a
combobox on B. I can code the comboBox1_Selec tedIndexChanged that is on
B but I dont know how to send that info to the label on A.I cant seem
to access A from B.
Help
Thanks
Mike

A.label from B. combobox.

Mar 7 '06 #2
AMP
This is on Form 1
private void button1_Click(o bject sender, EventArgs e)
{
Form frm = new Form2();
frm.label1.Text = "Hello";
}
The label is on a different form (Form 2)
Why doesnt this work?
label1 is public
Thanks

Mar 7 '06 #3
"AMP" <am******@gmail .com> wrote in message
news:11******** **************@ i40g2000cwc.goo glegroups.com.. .
This is on Form 1
private void button1_Click(o bject sender, EventArgs e)
{
Form frm = new Form2();
frm.label1.Text = "Hello";
}
The label is on a different form (Form 2)
Why doesnt this work?
label1 is public
Bob gave an excellent explanation but he missed out one really important
point, how to get the reference to the Form. The above code won't work
because you are creating a new form every time the button is clicked but not
showing it. You need some way for Form1 to be able to access Form2. Where in
your code now do you currently have the "new Form2()"? Where ever that is
you need to keep that reference and somehow give your form1 access to that.
eg, in your MDI form:

public static Form2 Form2;

MyMDIForm_Load( )
{
Form2 = new Form2();
Form2.MDIParent = this;
Form2.Show();
}

then in Form1:
private void button1_Click(o bject sender, EventArgs e)
{

MyMDIForm.Form2 .ChangeLabel("H ello");
}

Thanks

Mar 7 '06 #4
It doesnt work because you are creating a new instance of Form2, not
modifying the form that already exists.

On Tue, 07 Mar 2006 10:24:05 +0800, AMP <am******@gmail .com> wrote:
This is on Form 1
private void button1_Click(o bject sender, EventArgs e)
{
Form frm = new Form2();
frm.label1.Text = "Hello";
}
The label is on a different form (Form 2)
Why doesnt this work?
label1 is public
Thanks


Mar 7 '06 #5
AMP
Heres the relevent code:
Here is how the 2 children are created.
On the MainForm:
private void optionsToolStri pMenuItem1_Clic k(object sender, EventArgs
e)
{
Form thisOptions = new Options();
thisOptions.Mdi Parent = this; ;
thisOptions.Sho w();
}

private void newToolStripMen uItem_Click(obj ect sender,
EventArgs e)
{
Form frm1 = new Form1();
frm1.MdiParent = this; ;
frm1.Show();
}
I click both of these to create the 2 forms.

This is on the Options Form:
private void comboBox1_Selec tedIndexChanged (object sender, EventArgs e)
{
keyof = (string)comboBo x1.SelectedItem ;
}

There is a label on frm1 which is my newly created Form1 that I want to
change the text value .
So the next line in the SelectedIndexCh anged above,I want to set the
text property to keyof.
I thought there werent any Form1's or Options until I create them in
the code above
Thanks,
Mike

Mar 7 '06 #6
Mike,

There indeed aren't any form1's, etc until you create them, but look at
what frm1 for example is scoped to. It's local to
newToolStripMen uItem_Click(), so it goes away as soon as that routine exits.

If you need frm1 available elsewhere than store it in (say) a private
field of the form you're coding from so that you can access that
instance later as needed.

--Bob

AMP wrote:
Heres the relevent code:
Here is how the 2 children are created.
On the MainForm:
private void optionsToolStri pMenuItem1_Clic k(object sender, EventArgs
e)
{
Form thisOptions = new Options();
thisOptions.Mdi Parent = this; ;
thisOptions.Sho w();
}

private void newToolStripMen uItem_Click(obj ect sender,
EventArgs e)
{
Form frm1 = new Form1();
frm1.MdiParent = this; ;
frm1.Show();
}
I click both of these to create the 2 forms.

This is on the Options Form:
private void comboBox1_Selec tedIndexChanged (object sender, EventArgs e)
{
keyof = (string)comboBo x1.SelectedItem ;
}

There is a label on frm1 which is my newly created Form1 that I want to
change the text value .
So the next line in the SelectedIndexCh anged above,I want to set the
text property to keyof.
I thought there werent any Form1's or Options until I create them in
the code above
Thanks,
Mike

Mar 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3334
by: horos | last post by:
hey all, Ok, a related question to my previous one on data dumpers for postscript. In the process of putting a form together, I'm using a lot of placeholder variables that I really don't care about in the submitted action. I'd therefore like to get rid of them by doing something like:
8
3339
by: deko | last post by:
I'm hoping someone can sanity check my understanding of the Object Model for Forms/Controls. I'm having trouble drilling down into Control properties. First, I have a record set with the following for a given Form: 1)Container
2
3962
by: Mevar81 | last post by:
Hi to everybody.I have a problem with the PropertyGrid control.I want to display not all the properties of a generic Control(Button,TextBox,ComboBox,ecc.).In general I don't want to display only one category(Appearance, Behavior,ecc.) but I want to chose directly which properties to show.I've read that I can use the SelectedObjects to put an...
4
2430
by: Tony W | last post by:
Hi, I am trying to write a simple application to retrieve data from the Windows registry and insert it into textboxs on a windows form. So far I have one namespace containing two classess. The first class handles the form generation - (this was done using GUI form designer).
2
3161
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the problems that I have encountered to date and the solutions (if any) that I found....
6
10467
by: Ioannis Demetriades | last post by:
Hi, I need to change the printer's font to "control" -a printer font, and then send a sequence of characters to the printer. My problem is that I cannot change the printer's font. Can this be done without API calls? Thanks Ioannis
0
5552
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control is used to view the state of the running jobs and schedule new jobs. The control also runs in the context of Internet Explorer (we do this so the...
7
2938
by: Sakharam Phapale | last post by:
Hi All, How to preserve the old font properties while changing new one? I posted same question 2 months back, but I had very small time then. eg. "Shopping for" is a text in RichTextBox and already formatted as "Shopping" ---Bold "for" -----Regular Now I want to underline whole text by preserving old style i.e. Bold and
5
3781
by: g6023 | last post by:
Hi All Newbie to C# I am afraid The array of panel dispays OK but I am having problems with accessing the controls within. My panels each have a number of label controls. How do I set, say, the Name.Text property in List<Panel> ListPanelArray
0
7740
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7985
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7830
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6071
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1082
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
784
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.