Click here to Skip to main content
15,909,591 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public class aDecimal1 : TextBox
{
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        MessageBox.Show((sender as TextBox).Text);
    }
}



hi friends.
please help me
:(
error: the name 'sender' does not exist in the current context
Posted

Because you are overriding the TextBox class, you don't need that part of the call. Try this:
C#
MessageBox.Show(Text);

If you place a regular TextBox on a form and then add the OnKeyPress handler on the form, the from will require a context for the call so it includes the object sender argument in the call. This is not required in your inherited aDecimal1 class as it is already instance specific. Because of this, you can just drop the sender as TextBox part.
 
Share this answer
 
Comments
faezun 16-May-12 11:21am    
thanks
:)

error: the name 'text' does not exist in the current context
fjdiewornncalwe 16-May-12 13:32pm    
I have tried this to confirm whether my answer is valid in both Windows.Forms and asp.net. In both cases the code works. You do have to make sure that "Text" is not written "text" as C# is case sensitive.
Sergey Alexandrovich Kryukov 18-May-12 14:32pm    
"Text", not "text". It does exist.
--SA
BillW33 16-May-12 11:23am    
You beat me to the answer; 5 from me :)
VJ Reddy 16-May-12 11:26am    
Good answer. 5!
The reason is that in the protected method
C#
protected override void OnKeyPress(KeyPressEventArgs e)
{
    MessageBox.Show((sender as TextBox).Text);
}

there is neither a parameter nor a local variable by name sender. Further there may not a field or property in the class with name sender. Hence, the above error.

In .NET the standard practice to implement events is to have a protected method like OnEventName. In this case the Event of TextBox is KeyPress and within the TextBox class this event is raised by calling the OnKeyPress event.

In .Net the standard pattern for events is of type
C#
public void EventName(object sender, EventArgs e) {  }

When a client subscribes to this event the client is to be informed who has raised this event so that they can use the object raising the event for some purpose.

When the protected method is used to raise the event it is already present in the concerned class and there is no need of sender. Hence, the OnEventName methods do not have sender parameter.
 
Share this answer
 
Comments
faezun 16-May-12 11:44am    
thanks.

i want : create a textBox class , when i press any key show it
what is your solution?

is it Possible?
Sandeep Mewara 17-May-12 1:09am    
5!
VJ Reddy 17-May-12 1:48am    
Thank you, Sandeep :)
Sergey Alexandrovich Kryukov 18-May-12 14:31pm    
Actually, the best answer is by priyanktripathi84, please see. All OP needs is to replace "Text" with "this.Text".
You are trying to explain how to handle an event, which is redundant because OP already created a derived class and overridden a relevant method.
--SA
VJ Reddy 18-May-12 20:36pm    
You're correct. The answer by priyanktripathi84 is to the point.
The OP used (sender as TextBox) as in the case when an event is subscribed. I was trying to explain the standard practice in .NET so that the corresponding the On... method of the event does not have sender parameter, as it is not necessary.
But in the process I missed out the essential point
Thank you :)
use this instead of sender.
there is no neas to cast it.
use this.Text directly
 
Share this answer
 
Comments
faezun 16-May-12 11:39am    
MessageBox.Show(Text);

error: the name 'text' does not exist in the current context
Sergey Alexandrovich Kryukov 18-May-12 14:15pm    
Your approach is not going to work. You should not use trial-end-error method without understanding. Finally, you should go in depth and understand how things work. Do you understand what an event object does? And what's the role of "this", where it comes from? "This" is the one of classical OOP fundamentals, but the delegates are much more advanced. Read about all that matter, stop wasting your time and the time of others.
--SA
mr.priyank 16-May-12 12:10pm    
i said MessageBox.Show(this.Text);
Sergey Alexandrovich Kryukov 18-May-12 14:11pm    
Correct, a 5.
This is all what's needed.
--SA
VJ Reddy 18-May-12 20:31pm    
To the point. 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900