Question Form1_Activated

Svolo4

New member
Joined
Mar 3, 2010
Messages
3
Programming Experience
Beginner
Hi guys i'd really appreciate your help in this one. I have been trying this for so long with only bad results.

Here is my code:

VB.NET:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        double z;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            if (z == 1)
            {
                textBox1.Text = ("Button1 clicked");
            }
            else
            {
                textBox1.Text = ("Button2 clicked");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            z = 1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            z = 2;
        }
    }
}

So what I am trying to do is to get textBox1 to show which button i have clicked. Because I have Form_Activated I only see changes when i reactivate the Form. So I would like to have Event that is Active all the time and listens to everything that happens in Application and NOT only when something is clicked or some text have changed.

Thanks in advance.
 
This is just exsample. The thing i want is Event that listent to everything no matter what, so its alvays active not only when something is done.
 
You say what you want is Event that listens to everything. Events DO NOT LISTEN. Events may cause some code to be executed, if that code has been implemented to respond to (listen for) an event. When you are in the VB code editor looking at your code for Form1.vb (or whatever you have named it), the box just above the upper left corner of the editor space is the Class Name drop down list box. Every object on your form is shown there. Select an object, then from the drop down list box where you see "Declarations" select the event you need a reponse for. VB creates a sub for you - and you code your response to the event. Exactly as jmcilhinney says.
 
I think you are under some misconceptions about how events work. As jimmajsterski says, events don't listen to anything. Events get raised by an object when it does something or something is done to it. If you want to be notified of a specific event then you have to register an event handler. When the event is raised, the method you specified to handle the event is invoked and you get to execute your code.

What you're asking for is a bit silly anyway. What use would some ambient event be anyway? What would it be listening for? Think about what it is that you're trying to do: you want to display the name of the last Button that was clicked in a TextBox what would be the point of changing that text any other time than when one of the Buttons was clicked? That's the only time your text could change so that's the only time you should change it. That means that the sensible, and only logical, thing to do is to handle the Click events of those Buttons and to update the Text of your TextBox then.
 
Back
Top