Jump to content
Xtreme .Net Talk

Retrieve active control, check if it is a textbox and insert text.


Recommended Posts

Posted

I would like to...

 

... retrieve the active control

... check if it is a textbox

... insert some text in it

 

Thanks

 

grtz, Spyru :p

Posted

Here is one snippet:

foreach(Control c in this.Controls)
						{
							try
							{
								if(c is Label || c is GroupBox || c is Panel || c is LinkLabel || c is CheckBox || c is RadioButton)
									((Control)c).BackColor= Color.FromArgb(0,0,0,0);

Here is a full method:

/// <summary>
		/// Returns the focused control on a form. If you want only the focused control of a panel or tab page ext.
		/// Just make f null and send a control to the method otherwise make c null and send a whole form. Returns
		/// null if no control is focused.
		/// </summary>
		public static Control GetFocusedControl(Form f, Control c)
		{
			if(f != null)
			{
				foreach(Control d in f.Controls)
				{
					if(d.Focused)
					{
						FocusedControl= d;
					}
					else
					{
						GetFocusedControl(null, d);
					}
				}
			}
			else if(c != null)
			{
				foreach(Control d in c.Controls)
				{
					if(d.Focused)
					{
						FocusedControl= d;
					}
					else
					{
						GetFocusedControl(null, d);
					}
				}
			}
			return FocusedControl;
		}

C#
Posted

Yeah I think I can do that, but how do I retrieve the width of my form. Because when I use

pgbLaden.Left = Form1.ActiveForm.Width - 142

I get an error:

An unhandled exception of type 'System.NullReferenceException' occurred in VideoBase.exe

 

Additional information: Object reference not set to an instance of an object.

 

grtz Spyru

Posted

Me.Width and Me.Height

Me.ClientWidth and Me.ClientHeight - without title bar.

 

When you get that type of exception it means that one of your variables is nothing. It has been declared but has no value at all.

C#

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...