|
Hello!
(Sorry, my English knowledge is not perfect, but I hope, that my words can be understood
I have a problem with processing arrow keys. I have a Form and a PictureBox on it. I want to move a small drawing on the picture box, and it works with the W,A,S,D keys, but nothing happens when I press the arrow keys. For test purposes I've put a label on the form to display the codes of the pressed keys and I've seen that even shift, home, pageup/down, etc. keys are processed, but the arrow keys do not seem to make a KeyDown event.
(I tried the form's KeyPreview property, but it did not help.)
Can anybody help me?
Here is my code code for the event:
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)<br />
{<br />
lblStatus.Text = "KeyDown : KeyCode = "+e.KeyValue.ToString();<br />
if ( (e.KeyCode == Keys.Right) || (e.KeyCode == Keys.D) ) <br />
{<br />
NextDirection=0;<br />
lblStatus.Text = "Right";<br />
}<br />
if ( (e.KeyCode == Keys.Up) || (e.KeyCode == Keys.W) ) <br />
{<br />
NextDirection=1;<br />
lblStatus.Text = "Up";<br />
}<br />
if ( (e.KeyCode == Keys.Left) || (e.KeyCode == Keys.A) )<br />
{<br />
NextDirection=2;<br />
lblStatus.Text = "Left";<br />
}<br />
if ( (e.KeyCode == Keys.Down) || (e.KeyCode == Keys.S) ) <br />
{<br />
NextDirection=3;<br />
lblStatus.Text = "Down";<br />
}<br />
}
|
|
|
|
|
Have you tried placing the code in other methods? Keypress, etc?
|
|
|
|
|
Thanks for the advice. Unfortunately it doesn't work. Using KeyPress event provides a KeyPressEventArgs type variable, which has information on the character only
(char KeyPressEventArgs.KeyChar), there is no chance to get information on special keys.
|
|
|
|
|
|
That works!!!
Thanks a lot for this link. Probably I did not use the proper keywords in google, so I did not find this page. Anyway, thanks again. Bye!
|
|
|
|
|
private int left = 0;
private int top = 0;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyData)
{
default :
break;
case Keys.Up :
top -= 1;
break;
case Keys.Down :
top += 1;
break;
case Keys.Left :
left -= 1;
break;
case Keys.Right :
left += 1;
break;
}
DrawIt();
}
private void DrawIt()
{
Bitmap b1 = new Bitmap(10, 10);
Graphics g = Graphics.FromImage(b1);
g.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(0, 0), b1.Size));
Bitmap b2 = new Bitmap(this.Width, this.Height);
g = Graphics.FromImage(b2);
g.DrawImage(b1,new Point(left,top));
this.BackgroundImage = b2;
this.Invalidate();
}
|
|
|
|
|
I'm scratching my head over a solution to a problem. The answer I have come up with seems to work but a little voice in the back of my head tells me it's not good design. I'm looking to find out what other people think.
I have an abstract Base class called MessageBase
I have concrete subclasses called SystemMessage, ErrorMessage etc.
at one point i had a static class called MessageFactory which read the element name of an XML node and returned the appropriate Concrete class. Now, however, i have moved the factory method into the Base class, MessageBase. This looks tidier all-round but i'm worried that i now have a superclass that knows too much about its subclasses.
Does this design sound good or is it asking for trouble when i've added a few nw messages
Thanks
Russell.
|
|
|
|
|
It's not so abnormal, you first used the Abstract Factory Pattern where a seperate class did the work to the easier Factory Method Pattern where you don't use a seperate class
It depends mostly on how much you had in the factory class. I do it all the time when my factory class just has one create method. There are other reasons to do this though, like wanting to keep stuff private or something.
You could always use some code to automaticly discover the classes and don't worry about it anymore.
|
|
|
|
|
i thought it was OK it just didn't seem quite right to me for a while there.
Thanks for your response,
Russell
|
|
|
|
|
arachnoid wrote: i'm worried that i now have a superclass that knows too much about its subclasses
Personally, I'd have stuck with the factory class and kept the abstract base class blissfully ignorant of its derivatives. If you ever have to add a new concrete type then you will have to update the base class as well. I suppose you would have had to update the factory class anyway, but by putting the creation aspect into a factory class I feel it leaves you open to more options in the future.
|
|
|
|
|
arachnoid wrote: is it asking for trouble
Yes. A separate factory static class is the right way to go.
Marc
Thyme In The CountryPeople are just notoriously impossible. --DavidCrow There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith
|
|
|
|
|
There is no (valid) reason for this logic to reside in the base class.
|
|
|
|
|
It seems odd (to me) to have a nonvirtual method in an abstract base class. I'd create a separate MessageFactory class that served up instances of SystemMessage , ErrorMessage , etc.
/ravi
|
|
|
|
|
Hi,
how to get the duration of an mp3 file using c#
Regards,
Sivaprasad
|
|
|
|
|
This isn't something you can do natively in the .NET framework. You'll need to use a 3rd party library to do this. For example, Managed DirectX has APIs to do this.
|
|
|
|
|
If you know the samplingsfrequency Fs and number of samples N: duration = N/Fs [sec]
Cheers Al
|
|
|
|
|
If I oversimplify this or you all ready know parts of this, I appologize. But here goes...
MP3 files are made up of a bunch of music sections, each having a header. At the end of the
file is a TAG 128 bytes long.
The Gist of getting the duration of an MP3 is to first get the size of the file, then subtract
128 bytes for the TAG. Divide the number of remaining "Bits" in the file by the bitrate.
So to get the file length:
.
.
using System.IO;
.
.
FileStream MP3File;
.
MP3File = new FileStream("C:\\mySong.mp3", FileMode.Open);
.
FileLength = MP3File.Length; // gives file length in bytes
.
And for the duration:
.
Duration = ((FileLength - 128) * 8) / bitrate; //gives duration in seconds
.
The problem now lies in getting the bitrate. If you know all your files are say 128kbits/sec
you can cheat an get a pretty good estimate. So your formula would be.
Duration = ((FileLength - 128) * 8) / (1024*128);
The problem is real MP3 files can have different bitrates within individiual song blocks.
So it would be neccessary to read each block header for the bitrate and then add everything
up at the end. Rather a tedious exercise.
Hope that helps a bit
greycrow
|
|
|
|
|
Hi All
the string value is:
strValue = "ABCD*XYZ";
I want to know the possition of sterik *
thank you
|
|
|
|
|
int pos=strValue.IndexOf("*"):
Luc Pattyn
|
|
|
|
|
Use something like:
int asteriskPosition = strValue.IndexOf("*");
This returns the index of the * in the string.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
thank you sir
|
|
|
|
|
I have a (simple) custom control, a NumericTextBox. This control allow to the user to type only numbers (0..9) inside.
I do this OnKeyPress and OnKeyDown events.
The problem is if the user do paste from Clipboard. I don't know how to deny "paste" operation if the content of the clipboard is not a number. (in each textbox you have an context menu, with "paste" function).
Or... better, not to deny the "paste" operation, but to add to the existing text of the textbox only numeric part of the clipboard content. (if the clipboard contains 12ad3 in my textbox to be added 123).
Any suggestion?
|
|
|
|
|
Hello,
I would suggest to validate the text in the OnTextChanged.
Store your valid nunber first and if the Conversion or better double.TryParse is not ok, insert the stored value again.
All the best,
Martin
|
|
|
|
|
why don't you use minimum and maximum properties??
life is study!!!
|
|
|
|
|
how to use this? min and max?
|
|
|
|