How to write an Event without using visual studio (in .cs or simply a .txt file)

0

I'm trying to write a Enter Event for TB1 (textbox)

This code for Form1 (in Form1.cs) , would normally write it with the help of Designer:

    using System;
    using System.Windows.Forms;
    namespace Project1
    {
            // aqui seria o método para o evento Enter que eu queria, o que está abaixo representado
            // para que a letra 'text' desapareça              

        private void TB1_Enter(object sender, EventArgs e)
            {
                if(TB1.Text=="Text")
                {
                    TB1.Text="";
                }
            }
     }

In the code below I wanted to implement the above event. What I was going to want to know is if I could write the event in the below file so that it can be compiled later. >

        using System;
        using.System.Windows.Forms;
        using System.Runtime.InteropServices;
    namespace Project1
    {
    [ComVisible(true)]
        public class EventArgs
        {
            //
            //Summary:
            //provides a value to use with events that do not have event data
            public static readonly EventArgs Empty;

            //
            //Summary:
            //Initializes a new instance of the System.EventArgs class
            public EventArgs()
            {

            }
        }
            public class Form1:Form
            {
                public static void Main()
                {
                     AplicationEnableVisualStyles();
                     Aplication.Run(new Form1());
                }
                public Form1()
                {
                    //textBox1
                  TextBox TB1 = new TextBox();
                TB1.Location = new Point(30, 20);
                TB1.Size = new Size(100, 30);
                TB1.Text = "Text";
                    //add control to textBox
                    this.Controls.Add(TB1);
                }
           }
     }           
    
asked by anonymous 26.09.2018 / 23:01

1 answer

1

The answer is yes, but the code you have made does not make much sense.

A class EventArgs is already defined in the System namespace in the System.Runtime.dll, mscorlib.dll, netstandard.dll assemblies. That is, basically it is present in all the projects you create, since mscorlib.dll is a dependency that you can not remove.

What this means is that even if you define your class EventArgs and you can do that (you can create classes with equal names as long as they are in different namespaces). The event signature, which in this case is void (object, EventArgs ) , will use the EventArgs defined in the System namespace and not yours.

In other words you created the class EventArgs , but you do not need it, it already exists.

One thing missing from your code is subscribing to the Enter event of the textbox.

TB1.Enter += TB1_Enter;
    
27.09.2018 / 00:01