KeyPress Event Problem

B

BR

I am trying to associate a method in a utility class with the KeyPress
event of a textbox in my GUI. How do I make that method "browseable"
to my IDE(Visual Studio in my case)?
 
I

Ignacio Machin ( .NET/ C# MVP )

I am trying to associate a method in a utility class with the KeyPress
event of a textbox in my GUI. How do I make that method "browseable"
to my IDE(Visual Studio in my case)?

you do not mark a method as browseable, the IDE knows the signature a
method should have to be able to handle a given event from the UI and
only those methods are displayed in the IDE.
Only the methods declared in the container are displayed, IIRC
 
J

Jon Skeet [C# MVP]

BR said:
I am trying to associate a method in a utility class with the KeyPress
event of a textbox in my GUI. How do I make that method "browseable"
to my IDE(Visual Studio in my case)?

Is there any particular reason you need to do it in the designer
instead of just hooking it up manually?
 
B

BR

you do not mark a method as browseable, the IDE knows the signature a
method should have to be able to handle a given event from the UI and
only those methods are displayed in the IDE.
Only the methods declared in the container are displayed, IIRC

The signature matches and yes I could manually associate the method
with the KeyPress event but I am attempting to minimize the code
within the initialization. Is there a workaround for this?
 
B

BR

Note that whether you do it via the Designer or explicitly, it's the same 
code.  All that the Designer does is insert the appropriate code in the 
InitializeComponent() method, called by the relevant container's  
constructor.

So, with respect to the goal of "attempting to minimize the code within  
the initialization", the question of visibility of a method to the  
Designer is moot.

Pete

Thanks. I inaccurately stated my goal. I would actually like to
minimize the amount of "manual code" entered. I would like for that
association to be maintained by the designer.
I hope I have more clearly expressed my need.
 
B

BR

Ah.  Okay, that's more clear.

I can think of a number of ways to approach the problem, but let me say  
first that I think that simply initializing it manually is really not that  
bad, and in fact is probably the most easily maintained approach.  
Especially since the alternative methods don't really put the information 
in the Designer per se; they would just make the subscription to the event  
more automatic.

Those alternatives include:

     -- Using a message filter in your utility class (initialized in a  
static constructor) that watches for all WM_KEY events.  This has a number  
of problems, including the fact that you'll have to filter for the control  
because you'll see the message for _all_ recipients, not just the textbox 
you care about, and of course the fact that you'll be dealing not with an 
actual C# event, but rather a window message.

     -- Sub-class the textbox.  If you do this, a number of alternatives  
open up:
         * simply always subscribe the utility method to the event
         * use the Tag property as a Designer-controlled way toturn the  
subscription on or off
         * create a new property for the purpose

There are other approaches along these lines, but the above is reasonably 
representative, I think.  There are other options that could be used if 
your scenario is in fact more restricted than you've explained so far.

But in any case the fact is, I don't really think any of these  
alternatives are really better than just writing the code manually.  At 
some point, someone has to configure the code to subscribe the method.  
Whether it's done with the Designer or by actually writing a single line  
of code in a specific place, I don't see much difference.  I don't really  
think you can get the Designer itself to support your goal, so the  
work-arounds seem worse than what you're trying to avoid.  :)

Pete

Thanks for your help, Pete!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top