I'm trying to understand how using statement is working as when I run the code below my textbox (mytextbox) is disappearing.
public partial class Form1 : Form
{
TextBox mytextbox = new TextBox();
public Form1()
{
InitializeComponent();
this.Controls.Add(mytextbox);
mytextbox.Validated+=new EventHandler(mytextbox_Validated);
}
private void mytextbox_Validated(object sender, EventArgs etxtbox)
{
using (TextBox myeventtxtbox = ((TextBox)sender))
{
}
}
}
I was thinking that the using statement will release myeventtxtbox at the end of the statement but will not release mytxtbox as well.
What I do in this code:
I create an object mytxtbox which will remain on the mainform (Form1).
I also create an event which will, on validated mytxtbox, will raise.
In the event I'm using the using statement to create a TextBox (myeventtxtbox) which is exactly the same that mytextbox (property and so on) but IS NOT mytextbox.
SO WHY mytextbox is released while getting out of the using statement? only myeventtxtbox (the copy) should be released.
I really miss something in here, could you help me?