Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Refresh control between WinForms
Message
From
14/02/2009 16:27:37
 
General information
Forum:
ASP.NET
Category:
Forms
Environment versions
Environment:
VB 9.0
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01380662
Message ID:
01381817
Views:
46
>Do I really have it backwards? I really thought Form2 has access to Form1's controls. Note to self: play with this in the morning. (Quiet, you, LOL).
>
>Still learning the basics, obviously, but loving it.


No worries ... we'll keep you honest. <g>

~~Bonnie




>
>>>Is Form2 modal? Sounds like it should be. In that case, it has direct access to Form1's controls. They are all still in memory.
>>
>>You've got it backwards Mike. Form1 has access to Form2 (unless you've passed Form1 to Form2 in it's constructor or as a property).
>>
>>>If not modal, you can save the "this" pointer to a static public variable in Form1, then use that to get to Form1's controls from Form2.
>>
>>I'd be more inclined to go along with Paul's suggestion to call a public refresh method on Form1 than to mess with Form1's controls directly from Form2. In fact, I'd take it one step further and use an interface:
>>
>>Suppose I have created this Interface:
>>
>>
>>public interface IRefresh
>>{
>>	void RefreshData();
>>}
>>
>>
>>Now, set up Form1 to implemement this interface.
>>
>>
>>public class Form1 : Form, IRefresh
>>{
>>	public void MethodToCallForm2()
>>	{
>>		OtherForm Form2 = new OtherForm(this);
>>		Form2.ShowDialog();
>>	}
>>	
>>	// IRefresh interface method
>>	public void Refresh()
>>	{
>>		// code here for refreshing your data
>>	}
>>}
>>
>>
>>And here's the relevant part of the OtherForm class.
>>
>>
>>public class OtherForm : Form	
>>{
>>	#region Declarations
>>
>>	protected IRefresh CallingControl = null;
>>
>>	#endregion
>>
>>	#region Constructors
>>
>>	public OtherForm(IRefresh callingControl)
>>	{			
>>		this.CallingControl = CallingControl;			
>>		InitializeComponent();
>>	}		
>> 
>>	public OtherForm()		
>>	{			
>>		InitializeComponent();
>>	}
>>
>>	#endregion
>>		
>>	#region Events
>>
>>	/// or whatever event you want to use to signify you're done.
>>	private void cmdOK_Click(object sender, System.EventArgs e)
>>	{
>>		// If this dialog form was called by a class that implemented IRefresh, 
>>		// then call call the Interface method.
>>		if (this.CallingControl != null)
>>		{
>>			this.CallingControl.Refresh();
>>		}
>>	}
>>
>>	#endregion
>>}
>>
>>
>>~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform