UserControl event

event that fires when usercontorl appears

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 3187 Views - Last Post: 19 March 2010 - 11:00 PM Rate Topic: -----

#1 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

UserControl event

Posted 19 March 2010 - 12:35 AM

i want to run some code when a usercontrol appears on my form.

i have a form with multiple usercontrols 1 over the other and when a usercontrol appears in front, the others are hidden. A menustrip is used to let the user show the desired usercontrol (puts it in front of the other usercontrols). i wish to put some code when one of the usercontrol appears on the form. i tried the visible changed event handler but this runs the code whenever any usercontrol is called and put in front of the others. i want to fire the code on the usercontrol when a the same usercontrol is called from the menu and appears in front.

Any tips please?

Thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: UserControl event

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: UserControl event

Posted 19 March 2010 - 01:06 AM

In your visible changed event handler cast the sender to a user control, check it's name and execute the code if the name of the user control is the one you want code to execute...

Cleaner solution would be to have an interface that every user control would implement and provide its own code in this implemented method... then let the code that is responsible for switching your user controls call this implemented method...
Was This Post Helpful? 1
  • +
  • -

#3 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: UserControl event

Posted 19 March 2010 - 02:13 AM

thanks for the tip ^^

sry for my ignorance .. but i don't know how to 'cast the sender to a usercontrol'
Was This Post Helpful? 0
  • +
  • -

#4 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: UserControl event

Posted 19 March 2010 - 02:18 AM

1. option: UserControl uc = sender as UserControl;
2. option: UserControl uc = (UserControl)sender; //not really sure if this one will work...

then if cast succeeded, you would get a usercontrol, if it doesn't you would get a null value. You really should handle this scenario as well.
Was This Post Helpful? 1
  • +
  • -

#5 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: UserControl event

Posted 19 March 2010 - 02:40 AM

thanks a lot FlashM worked fine .. what i did is this:

void userControl_VisibleChanged(object sender, EventArgs e)
        {
            userControl us = sender as userControl;

            if (us.Visible == true)
            {
                //code
            }
        }



but what am i doing exactly with the 'userControl us = sender as userControl;'. am i creating an instance of the userControl and setting it as the sender Object of the event handler?

Thanks as always m8 :)
Was This Post Helpful? 0
  • +
  • -

#6 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: UserControl event

Posted 19 March 2010 - 02:45 AM

No. Sender in this case is the control that called/invoked/fired this visible changed event. Because the sender is of type object you need to convert it to UserControl to be able to use all the properties and methods that user control provides. At this step we are only guessing that the sender is a user control (it could be anything), so we are trying to convert it to a user control. If this conversion succeeds, than it's OK, if it doesn't, you will get a null value. I would update your code as follows:

void userControl_VisibleChanged(object sender, EventArgs e)
{
   userControl us = sender as userControl;

   if (us != null)
   {
      if (us.Visible == true)
      {
         //code
      }
   }
}



Was This Post Helpful? 1
  • +
  • -

#7 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: UserControl event

Posted 19 March 2010 - 05:29 AM

is there an event for when a usercontrol is 'hidden' (not visible)?
Was This Post Helpful? 0
  • +
  • -

#8 EtherealMonkey  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 33
  • Joined: 17-March 10

Re: UserControl event

Posted 19 March 2010 - 05:38 AM

View Postxikky, on 19 March 2010 - 04:29 AM, said:

is there an event for when a usercontrol is 'hidden' (not visible)?


?

try:

void userControl_VisibleChanged(object sender, EventArgs e)
{
   userControl us = sender as userControl;

   if (us != null)
   {
      if (us.Visible == false)
      {
         //code
      }
   }
}


Was This Post Helpful? 0
  • +
  • -

#9 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: UserControl event

Posted 19 March 2010 - 05:42 AM

^^ . . . that does not work EtherealMonkey. it's because the userControl is always visible == false until it is called.

i want to put some code when the usercontrol is visible == false after visible == true, not before ..
Was This Post Helpful? 0
  • +
  • -

#10 EtherealMonkey  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 33
  • Joined: 17-March 10

Re: UserControl event

Posted 19 March 2010 - 05:48 AM

View Postxikky, on 19 March 2010 - 04:42 AM, said:

^^ . . . that does not work EtherealMonkey. it's because the userControl is always visible == false until it is called.

i want to put some code when the usercontrol is visible == false after visible == true, not before ..


Then you don't need an event.

Just test the control directly before you step into your code.


if (usercontrol.Visible == false)
{
    ...
}




or

keep up with it proactively


bool userControlVisible = usercontrol.Visible;

if (userControlVisible == false)
{
    ...
}



This post has been edited by EtherealMonkey: 19 March 2010 - 05:50 AM

Was This Post Helpful? 0
  • +
  • -

#11 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: UserControl event

Posted 19 March 2010 - 05:49 AM

There is some Enter event that might be interesting...
Was This Post Helpful? 0
  • +
  • -

#12 EtherealMonkey  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 33
  • Joined: 17-March 10

Re: UserControl event

Posted 19 March 2010 - 05:59 AM

View PostFlashM, on 19 March 2010 - 04:49 AM, said:

There is some Enter event that might be interesting...


Funny, I was just thinking about (and looking for) this:

public partial class frmMain : Form
{
	private RichTextBox _rtfCurrent;

	public frmMain()
	{
		InitializeComponent();
		_rtfCurrent = new RichTextBox();
	}
	private void rtf_Enter(object sender, EventArgs e)
	{
		_rtfCurrent = (sender as RichTextBox);
	}
}


Was This Post Helpful? 0
  • +
  • -

#13 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: UserControl event

Posted 19 March 2010 - 06:31 AM

can't manage this one :( ...

i'm trying .. :
void userControl_Enter(object sender, EventArgs e)
        {
            userControl us = sender as userControl;

            if (us.Visible == false)
            {
                //code...
            }
        }



.. thinking that this will fire after the user contorl is entered and when visible == false but this does not work .. tried some other properties such as lostFocused, focused and some others and the leave event but still in vain.
Was This Post Helpful? 0
  • +
  • -

#14 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: UserControl event

Posted 19 March 2010 - 06:48 AM

If you explained what exactly you are trying to do, might be of a good help... Maybe you are currently thinking in wrong direction...
Was This Post Helpful? 0
  • +
  • -

#15 EtherealMonkey  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 33
  • Joined: 17-March 10

Re: UserControl event

Posted 19 March 2010 - 07:26 AM

try this:

using System;
using System.Windows.Forms;

namespace testForm
{
    public partial class Form1 : Form
    {
        
        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;
        private System.Windows.Forms.ToolStripMenuItem textBoxToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem richTextBox1ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem pictureBox1ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem listView1ToolStripMenuItem;
        private System.Windows.Forms.StatusStrip statusStrip1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.ListView listView1;
        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.ToolStripMenuItem button1ToolStripMenuItem;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
        private System.ComponentModel.Container components = null;
        
        public Form1()
        {
            InitializeComponent();
        }
        
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.textBoxToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.richTextBox1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.pictureBox1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.listView1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.listView1 = new System.Windows.Forms.ListView();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
            this.menuStrip1.SuspendLayout();
            this.statusStrip1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripMenuItem1});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(466, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // toolStripMenuItem1
            // 
            this.toolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.textBoxToolStripMenuItem,
            this.richTextBox1ToolStripMenuItem,
            this.pictureBox1ToolStripMenuItem,
            this.listView1ToolStripMenuItem,
            this.button1ToolStripMenuItem});
            this.toolStripMenuItem1.Name = "toolStripMenuItem1";
            this.toolStripMenuItem1.Size = new System.Drawing.Size(48, 20);
            this.toolStripMenuItem1.Text = "Select";
            // 
            // textBoxToolStripMenuItem
            // 
            this.textBoxToolStripMenuItem.Name = "textBoxToolStripMenuItem";
            this.textBoxToolStripMenuItem.Size = new System.Drawing.Size(148, 22);
            this.textBoxToolStripMenuItem.Text = "textBox1";
            this.textBoxToolStripMenuItem.Click += new System.EventHandler(this.textBoxToolStripMenuItem_Click);
            // 
            // richTextBox1ToolStripMenuItem
            // 
            this.richTextBox1ToolStripMenuItem.Name = "richTextBox1ToolStripMenuItem";
            this.richTextBox1ToolStripMenuItem.Size = new System.Drawing.Size(148, 22);
            this.richTextBox1ToolStripMenuItem.Text = "richTextBox1";
            this.richTextBox1ToolStripMenuItem.Click += new System.EventHandler(this.richTextBox1ToolStripMenuItem_Click);
            // 
            // pictureBox1ToolStripMenuItem
            // 
            this.pictureBox1ToolStripMenuItem.Name = "pictureBox1ToolStripMenuItem";
            this.pictureBox1ToolStripMenuItem.Size = new System.Drawing.Size(148, 22);
            this.pictureBox1ToolStripMenuItem.Text = "pictureBox1";
            this.pictureBox1ToolStripMenuItem.Click += new System.EventHandler(this.pictureBox1ToolStripMenuItem_Click);
            // 
            // listView1ToolStripMenuItem
            // 
            this.listView1ToolStripMenuItem.Name = "listView1ToolStripMenuItem";
            this.listView1ToolStripMenuItem.Size = new System.Drawing.Size(148, 22);
            this.listView1ToolStripMenuItem.Text = "listView1";
            this.listView1ToolStripMenuItem.Click += new System.EventHandler(this.listView1ToolStripMenuItem_Click);
            // 
            // statusStrip1
            // 
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripStatusLabel1});
            this.statusStrip1.Location = new System.Drawing.Point(0, 438);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(466, 22);
            this.statusStrip1.TabIndex = 1;
            this.statusStrip1.Text = "statusStrip1";
            // 
            // textBox1
            // 
            this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textBox1.Location = new System.Drawing.Point(0, 0);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(466, 460);
            this.textBox1.TabIndex = 2;
            this.textBox1.Visible = false;
            this.textBox1.VisibleChanged += new System.EventHandler(this.Control_VisibleChanged);
            // 
            // pictureBox1
            // 
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(466, 460);
            this.pictureBox1.TabIndex = 3;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Visible = false;
            this.pictureBox1.VisibleChanged += new System.EventHandler(this.Control_VisibleChanged);
            // 
            // listView1
            // 
            this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listView1.Location = new System.Drawing.Point(0, 0);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(466, 460);
            this.listView1.TabIndex = 4;
            this.listView1.Visible = false;
            this.listView1.VisibleChanged += new System.EventHandler(this.Control_VisibleChanged);
            // 
            // richTextBox1
            // 
            this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.richTextBox1.Location = new System.Drawing.Point(0, 0);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(466, 460);
            this.richTextBox1.TabIndex = 5;
            this.richTextBox1.Text = "";
            this.richTextBox1.Visible = false;
            this.richTextBox1.VisibleChanged += new System.EventHandler(this.Control_VisibleChanged);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(185, 208);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 6;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Visible = false;
            this.button1.VisibleChanged += new System.EventHandler(this.Control_VisibleChanged);
            // 
            // button1ToolStripMenuItem
            // 
            this.button1ToolStripMenuItem.Name = "button1ToolStripMenuItem";
            this.button1ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.button1ToolStripMenuItem.Text = "button1";
            this.button1ToolStripMenuItem.Click += new System.EventHandler(this.button1ToolStripMenuItem_Click);
            // 
            // toolStripStatusLabel1
            // 
            this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
            this.toolStripStatusLabel1.Size = new System.Drawing.Size(0, 17);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(466, 460);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.statusStrip1);
            this.Controls.Add(this.menuStrip1);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.listView1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "Form1";
            this.Text = "Form1";
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
        private void Control_VisibleChanged(object sender, EventArgs e)
        {
            
            Control _control = sender as Control;
            string _controlType = _control.GetType().ToString();
            string _button1 = "System.Windows.Forms.Button";

            if (_control != null)
            {
                // Should only fire on the control being hidden
                if (_control.Visible == false)
                {
                    this.toolStripStatusLabel1.Text = "Hiding: " + _controlType;
                }
                // Should only fire on the control being shown and Control.GetType
                // = System.Windows.Forms.Button
                if (_control.Visible == true && (_controlType == _button1))
                {
                    this.Close();
                }
            }

        }
        private void textBoxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.textBox1.Visible = true;
            this.richTextBox1.Visible = false;
            this.listView1.Visible = false;
            this.pictureBox1.Visible = false;
        }

        private void richTextBox1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.textBox1.Visible = false;
            this.richTextBox1.Visible = true;
            this.listView1.Visible = false;
            this.pictureBox1.Visible = false;
            this.button1.Visible = false;
        }

        private void pictureBox1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.textBox1.Visible = false;
            this.richTextBox1.Visible = false;
            this.listView1.Visible = false;            
            this.pictureBox1.Visible = true;
            this.button1.Visible = false;
        }

        private void listView1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.textBox1.Visible = false;
            this.richTextBox1.Visible = false;
            this.listView1.Visible = true;
            this.pictureBox1.Visible = false;
            this.button1.Visible = false;
        }
        private void button1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.textBox1.Visible = false;
            this.richTextBox1.Visible = false;
            this.listView1.Visible = false;
            this.pictureBox1.Visible = false;
            this.button1.Visible = true;
        }
    }
}



Open a notepad window, copy / paste. Save as C:\Form1.cs

Open a command prompt.
Type "CD C:\"
Type "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe Form1.cs"

It will compile the form (albeit strangely - opening a cmd window on each execution).

Try moving through the menu selections (but not the button1, yet).

Finally, select the button1 menu selection and the form should close.

I hope this helps...

This post has been edited by EtherealMonkey: 19 March 2010 - 07:36 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2