|
Hello everyone,
I would like to add few keyboard short cuts to my application and I was wondering if someone could give me some information on how to do this.
What I have done so far "which is not working!" is the following.
private void MainForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)<br />
{<br />
if (pendingCmd)<br />
return;<br />
<br />
if (e.KeyCode == Keys.P && e.Control == true)<br />
{<br />
playToolStripMenuItem_Click(null, null);<br />
return;<br />
}<br />
<br />
if (playState == PlayState.Init)<br />
return;<br />
<br />
if (e.Control == true)<br />
{<br />
switch (e.KeyCode)<br />
{<br />
case Keys.A:<br />
{<br />
pauseToolStripMenuItem_Click(null, null);<br />
break;<br />
}<br />
case Keys.S:<br />
{<br />
stopToolStripMenuItem_Click(null, null);<br />
break;<br />
}<br />
.<br />
.<br />
.<br />
.
Thank you very much and have a great day.
Khoramdin
|
|
|
|
|
Have you enabled the KeyPreview property of your form? If not, you'll only get events when the form is focused but none of its child controls are.
--
CleaKO The sad part about this instance is that none of the users ever said anything [about the problem].
Pete O`Hanlon Doesn't that just tell you everything you need to know about users?
|
|
|
|
|
Hi,
there basically are two ways to get shortcuts to work:
1. add menus and menu items, and give them the necessary shortcuts.
this is my preferred way, since now the user also has a visual indication as to
which shortcuts exist, and it requires no additional code: whenever the
shortcut is entered, the menu click handler is invoked (assuming the menu item
is still present and enabled in the current main menu).
2. add code to the keyboard handlers, as you are trying to do. If your code
does not work properly, add some logging (e.g. Console.WriteLine) statements
to see which parts get executed and which dont.
|
|
|
|
|
Hello.
How can i get a code of pressed key ?
One nation - underground
|
|
|
|
|
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.keydown(VS.71).aspx
like that?
Russell
|
|
|
|
|
try this
this is a small key pressed event
private void cbfilmoname_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==0xd)
{
mymethod();
}
}
// "0xd" means that the Key Pressed is "ENTER" key
// you can find the KeyChar of the any key with tracing this code
// or in help documents
|
|
|
|
|
Hi,
there are 3 keyboard events: KeyDown, KeyPress, KeyUp
the documentation states:
The KeyPress event is not raised by noncharacter keys; however, the noncharacter
keys do raise the KeyDown and KeyUp events.
noncharacter keys include shift, alt, control, shift lock, ...
KeyDown and KeyUp offer KeyCode, KeyChar and KeyValue properties;
KeyPress only offers KeyChar.
The rationale seems to be special characters can be recognized during KeyDown/Up,
whereas KeyPress only is fired for normal characters, where KeyCode would not
be relevant. But the KeyPressEventArgs does offer information on the state
of the modifier keys (Control, Alt, Shift).
Note: normal characters have autorepeat, the special ones dont.
Warning: the KeyPress event fires when the key gets released (or the autorepeat
delay is reached). The KeyDown event fires immediately when the key goes down.
Conclusion: if you really need KeyCode, use KeyDown event.
|
|
|
|
|
Thanks to all for their answers !!
One nation - underground
|
|
|
|
|
Hi all,
in C++ handle is created something like this
CLASSNAME::create("abc", handle);
So how to do the same task in C#.NET
thanks
|
|
|
|
|
A handel is just a pointer to something. Usually a void*
Software_Specialist wrote: CLASSNAME::create("abc", handle);
In C# you'd do this as a static method on a class
public static void Create(string someValue, out object handle)
{
}
Now, if you want to know about this for some interop stuff then a handle is usually an IntPtr in C#
However, this is less than best practice for a .NET application. If you are not doing interop, but you want a factory method you are better doing something like this:
public static Classname Create(string someValue)
{
Classname result = new Classname();
return result;
}
|
|
|
|
|
This is probably the simplest question I have ever asked, but is there a way to make all classes derived from a base class with mandatory override? Ex: I have a class called MyClass123 which has a protected virtual void MyFunction(). When I derive a base class from MyClass123 I don't have to override MyFunction(). Is there a way to make the derived class require MyFunction() to be overwritten?
Chris
|
|
|
|
|
Hi Chris
try 'protected abstract void', you don't implement the function body and the whole class must be abstract.
-------------------------------
Carrier Bags - 21st Century Tumbleweed.
|
|
|
|
|
Hi all,
Please help me in providing example of using get(),set().
What exactly and why necessary get set methods.
Pls illustrate me with example.
Thanks and reagards
Have a Nice Day Dudes
|
|
|
|
|
You can use Proprty setting to handle the data member in a class.
Like
Class Example
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
Example exple = new Example();
You can set the value like
exple.Name = "Kushi Bobby";
You will get that property name thru the object
You can get the value like
string name = exple.Name;
My small attempt...
|
|
|
|
|
So, it's considered a bad practice in OOP to have member variables defined as public in your classes. So you define them as private and you read and write theirs values through "getters" and "setters" which are public.
Properties are just a fancy way to ease the use of get and set methods, as they give you the illusion of accessing the memeber variable directly.
|
|
|
|
|
blackjack2150 wrote: Properties are just a fancy way to ease the use of get and set methods, as they give you the illusion of accessing the memeber variable directly.
It depends on the language. In MSIL Properties compile into two methods with the name of the property, but with one prefixed get_ and the other set_. In Managed C++ the get and set methods are explicit called. e.g. get_Name() or set_Name()
|
|
|
|
|
Visual J# is an OOP and instead of properties for .NET objects their are get and set methods for each .NET property when compared to other .NET languages like C# and VB.
blackjack2150 wrote:
Properties are just a fancy way to ease the use of get and set methods, as they give you the illusion of accessing the memeber variable directly.
Basically
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my homepage Oracle Studios[ ^]
|
|
|
|
|
and of course the advantage of properties are:
- you can make something public readable, but not public writeable
- you can add code to the get/set part of the property, so you can easily
count the operations, log them, modify the internals of your class, whatever,
all without changing the code that uses your class.
|
|
|
|
|
I need some help on the following problem. I need to have a object set itself to null. Is this in any way possible?
|
|
|
|
|
try
obj = null
Mubashir
Software Architect
Storan Technologies Inc, USA
Every job is a self portrait of the person who did it.
|
|
|
|
|
I will explain myself a bit.
public class A {
public void SetToNull() {
}
}
I know that I can set it like this below. But that is not what i want.
public class A {
public void SetToNull()
{
}
}
public class B {
public A a = new A();
public void SetToNull()
{
a = null;
}
}
It needs to be like this.
public class A {
public void SetToNull()
{
}
}
public class B {
public A a = new A();
public void SetToNull()
{
a.SetToNull();
}
}
|
|
|
|
|
The answer is no because the the object does not have any knowledge of who holds a reference to it.
There may, however, be a way around it using weak references. The problem with that solution is that the point at which the references become null is non-deterministic.
You could also implement IDisposable and Dispose of the internals of the object when you don't need it. However, anything accessing the object would have to be able to handle the fact that the object may exist, but it has already been disposed of.
The above work arounds do not take into account the overall big picture. It would be better to know what it is you are trying to achieve. That way we could probably suggest a better solution.
|
|
|
|
|
Hi,
an object can not set itself to null.
an object is a piece of code+data, and what you use to access an object
really is a reference (often also called object, but that is not strictly correct).
Now an object cannot access its reference(s) in general, since an object is
not aware of its references.
In special cases you can do something, but that requires you get access to the
reference, typically by using the ref keyword, as in:
Nullable A=new Nullable();
a.NullifyMyRef(ref a);
a.Something();
class Nullable {
public void NullifyMyRef(object ref obj) {
obj=null;
}
}
But what is the use of all this ???
Most scenarios I can come up with are a result of bad OO practices.
The one good use I could come up with is something like the Dispose() method
that may want to nullify its own reference; the fact that it does not proves
to me once more it can not be done.
|
|
|
|
|
Matglas wrote: I need to have a object set itself to null. Is this in any way possible?
If you mean what I think you mean then the answer is no.
Do you mean something like this:
MyObject obj = new MyObject();
...
obj.DoSomeOperationThatSetsObjToNull();
Assert.IsNull(obj);
The answer is no because the the object does not have any knowledge of who holds a reference to it.
There may, however, be a way around it using weak references. The problem with that solution is that the point at which the references become null is non-deterministic.
You could also implement IDisposable and Dispose of the internals of the object when you don't need it. However, anything accessing the object would have to be able to handle the fact that the object may exist, but it has already been disposed of.
The above work arounds do not take into account the overall big picture. It would be better to know what it is you are trying to achieve. That way we could probably suggest a better solution.
|
|
|
|
|
Ok I hope my english is good enough to explain what I want to achieve.
I have a deserialized object from a xml file. This object is shown in a treeview to edit the content. I want to have the option to remove a object by clicking on it and getting it context menu. This context menu is unique for every object. I get it by a GetContextmenu method that is implementated by a Interface. The eventhandlers for each menuitem is set to a method in that object.
I hope the situation is clear. Otherwise i have to make some code to show it.
|
|
|
|