How do windows talk to each other?

Started by
4 comments, last by Kate18 20 years, 3 months ago
Hi, I have made a small app and I am trying to make this slider bar work. What I want to happen is from my main apps main menu there is a button called opacity. When clicked I would like it to open another form with a track bar on it that controls the main apps opacity. Is this possible? So far when I click on the opacity button, a new window opens with the trackbar on it but i cant work out how to connect the opacity control bit. The code to open the trackbar window is private void menuItem7_Click(object sender, System.EventArgs e) { opacity d = new opacity(); d.ShowDialog(); } Can 2 forms access each others properties? Thanks heaps for any info, Kate Oh, I''m using vc#
Advertisement
Probably the easiest way to do this is to have the track bar control on the second form have an event handler for it''s Scroll event that updates a public variable in the second form. Then if ShowDialog() returns ok, just read the variable in your menu item event handler.
What I''d do is to pass the "this" pointer to the second form
and register it to the ValueChanged event handler of the
trackbar.

So the code would look something like this:

// In main formprivate void menuItem7_Click(object sender, System.EventArgs e){opacity d = new opacity(this);d.ShowDialog();}public void trackbar_ValueChanged(object sender, System.EventArgs e){    // Change main form opacity here}// In the opacity formopacity(Form parent){   InitializeComponents();   m_TrackBar.ValueChanged += System.Eventhandler(parent.trackbar_ValueChanged);}


This method allows "live" update of the opacity as the track
bar is being dragged.

I hope this helps.



Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
Cool, Thanks guys
That code you wrote you wrote tangentz worked great.
Cheers


A friend of mine helped me get this working at her place but now i'm home trying to do it but it doesn't work.
Whats happening is that when i try to use parent in the opacity forms constuctor it doesnt give me access to the event code that controls the main forms opacity.
eg

public opacity(Form parent)
{
InitializeComponent();
// if i use parent. here, i only have access to the "form" classes members and not the members of my main app even though we pass "this" to the constructor.
}

Does that make sense?


[edited by - Kate18 on January 15, 2004 6:13:20 AM]
Did you try casting the reference to the type of your main form?
Right, you can downcast to the main form to use its methods.

public opacity(Form parent){InitializeComponent();MainForm f = (MainForm) parent;  // Downcast to MainFormf.DoSomething(); // USe MainForm''s methods}  


Or, just pass in the parent as a MainForm.

public opacity(MainForm parent){InitializeComponent();parent.DoSomething(); // USe MainForm''s methods} 




Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!

This topic is closed to new replies.

Advertisement