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

Asked

Viewed 49 times

1

I’m trying to write in Eventargs() a Enter Event for TB1 (textbox)

This code of Form1 (in Form1.Cs), would write it normally 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 wanted to implement the event that is above. What I intended to know is if I could write the event in the file below so that it is later compiled

        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);
                }
           }
     }           
  • I don’t know if I’m being clear on the problem. : S

1 answer

1

The answer is yes, but the code Voce made doesn’t make much sense.

A class EventArgs is already defined in the namespace System in the assemblies System.Runtime.dll, mscorlib.dll, netstandard.dll. In other words, basically it is present in all the projects that Oce creates, since mscorlib.dll is a dependency that Voce cannot remove.

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

In other words, Voce created the class EventArgs, but you don’t need it, it already exists.

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

TB1.Enter += TB1_Enter;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.