Accessing parent variables from a user defined control

pendragon

Junior Contributor
Joined
Aug 20, 2003
Messages
212
Location
Cambridgeshire
Hi all

I know know that you can use base.FindForm to get the form that your control is on, but is there any way to then access that forms variables for testing purposes.

ie
Code:
Form ParentForm = base.FindForm();
if (ParentForm.A_Variable == true) 
{
     // do something
}
else
{
     // do something else
}

Thank you
 
If you wish to access a form's specific functionality you will need to cast the result of .FindForm to the correct form type.
If the control could be placed on many different forms (probably) then this could get very messy. It may be easier if you gave more details about what you are trying to do and why you feel this is the best solution - there may be an alternate way of approaching the problem.
 
ParentFormType ParentForm = (ParentFormType)base.FindForm(); <---
if (ParentForm.A_Variable == true)
{
// do something
}
else
{
// do something else
}
 
Thank you all for your replies

I had forgotten about using cast, long day and my brain was complaining, only trouble with this is that I have 3 types of forms this control can go on so am not sure how I would know which type to cast it to.

Having played with it to see if this would solve my problem I find that it will not, I was clutching at straws at the time so will do as PlausiblyDamp suggests and let you know the problem.

I have created a comboBox (based on one goodmorningsky wrote) that has a new property on it to say if it is locked or not.

PHP:
using System;
using System.Windows.Forms;

namespace MyComboBox
{

	public class MyComboBox : ComboBox
	{
		private bool isLocked;
		private int iIndex = -1;
		private bool MouseInControl = false;

		public bool IsLocked 
		{
			get 
			{
				return this.isLocked;
			}
			set 
			{
				this.isLocked = value;
			}
		}

		public MyComboBox()
		{
		}

		protected override void OnKeyPress(KeyPressEventArgs e)
		{
			base.OnKeyPress(e);
			if(this.isLocked == true)
			{
				e.Handled = true; 
			}
			else 
			{
				if (e.KeyChar == (char)Keys.Return) 
				{
					SendKeys.Send("{TAB}");
				}
			}
		}

		protected override void OnSelectedIndexChanged(EventArgs e)
		{
			base.OnSelectedIndexChanged(e);
			if(this.isLocked == true && this.MouseInControl == true)
			{
				this.SelectedIndex = this.iIndex;
			}
			else 
			{
				this.iIndex = this.SelectedIndex;
			}
		}

		protected override void OnMouseEnter(EventArgs e)
		{
			base.OnMouseEnter(e);
			this.MouseInControl = true;
		}
	
		protected override void OnMouseLeave(EventArgs e)
		{
			base.OnMouseLeave(e);
			this.MouseInControl = false;
		}
	}
}

The reason for this is that the system I have written has users that can only view data and others that can view and change data, the complaint I had from some of the users who can only view data was that the comboBox was difficult to read when not enabled.

Because there is a large amount of data, they decided that they did not what a list of all records that they could choose from but to be able to more back and forward with buttons, type in a known key or press a find button to search.

Now every thing works fine for those who just view data, the problem comes when you change the contents of a comboBox, as you can see the OnSelectedIndexChanged checks if the control is locked, this does not let them change the contents of the box but if they are moving through the database I still need to show the new data, so I use the OnMouseEnter and OnMouseLeave to set a variable and check that if it is locked, and the mouse is not in the control, then it can update the information.

The trouble is that the OnMouseLeave does not fire when you change the contents of the combox so when it has been changed all records will display the same information.

What I am trying to do is come up with a logical way to do this.

Thanks for reading all this :D
 
Back
Top