|
I designed a form that got 2 textboxes(i named tbUserName & tbPassword) and 2 button (btOK & btCancel).
private void btOK_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Enter Key has been pressed");
}
tbPassword_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ((Control.ModifierKeys & Keys.Enter) == Keys.Enter)
btOK_Click(sender, e);
}
but nothing happen.
Please help me as possible as you can.
Thanks.
Mr Duc Linh Nguyen
|
|
|
|
|
duclinh wrote:
if ((Control.ModifierKeys & Keys.Enter) == Keys.Enter)
The Enter key is not a modifier key, so this will never be true. Here's what you're looking for:
if(e.KeyChar == (char)Keys.Enter)
However, you don't even need to do that. Just set your form's AcceptButton to be btOK and the button will be "clicked" when Enter is pressed in your form.
Charlie
if(!curlies){ return; }
|
|
|
|
|
What is the BEST / HIGHEST PERFORMANCE method of reading data from a SqlDataReader and returning a "default" value if the value from the reader is null. I have several different methods that all work but all involve calling a seperate void to test the values or catch the null exception. I just keep thinking there has to be a better way - i.e., one "slick" and simple line of code; but I can't seem to find it???
|
|
|
|
|
if (value == null) return defaultValue; Don't rely on exceptions for this purpose because they are expensive to catch and thus decrease performance. Also, value types (like numeric types, booleans, DateTime , etc.) can't be null, so you'll already get their default value (0, false, etc.) or DBNull depending on how things are set up).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Why won't this double buffer?
I'm drawing this graph that allows you to either move value sliders or input values and redraw the graph. I've looked at tons of double buffering examples and they all seem to be doing the same or similar type of code. But my graph still flickers. BAH!
[code]public void DrawGraph()
{
this.UpdateGraphVariables();
Point[] pointarray=new Point[5];
Pen newpen = new Pen(Color.Black);
this.m_bmImageOffScreen = new Bitmap(this.Width, this.Height);
this.m_gOffScreen = Graphics.FromImage(this.m_bmImageOffScreen);
this.m_gBuffer=Graphics.FromHwnd(this.Handle);
//computational stuff
//not interesting
I do all my drawing on the bitmap...
//Set point of origin to be 50,50 on the form
this.m_gOffScreen.TranslateTransform(50,130);
this.m_gOffScreen.DrawLine(newpen,1,1,1,150);
this.m_gOffScreen.DrawLine(newpen,1,150,150,150);
//Draw X and y axis
this.m_gOffScreen.DrawLine(newpen,-3,30,3,30);
this.m_gOffScreen.DrawLine(newpen,-3,60,3,60);
this.m_gOffScreen.DrawLine(newpen,-3,90,3,90);
...
//Join the points
pointarray[0]=new Point(30,Convert.ToInt32(a));
pointarray[1]=new Point(60,Convert.ToInt32(b));
pointarray[2]=new Point(90,Convert.ToInt32(c));
pointarray[3]=new Point(120,Convert.ToInt32(d));
pointarray[4]=new Point(150,Convert.ToInt32(f));
this.m_gOffScreen.DrawLines(new Pen(Color.Yellow),pointarray);
Then I attempt to double buffer!
if (this.m_bmOldBitmap != null)
{
this.m_bmOldBitmap.Dispose();
}
this.m_bmOldBitmap = this.m_bmImageOffScreen;
this.m_gBuffer.Clear(this.BackColor);
this.m_gBuffer.DrawImage(this.m_bmOldBitmap,30,50);
}[/code]
It still flickers!
|
|
|
|
|
In InitializeComponent put SetStyle(ControlStyles.DoubleBuffer);
|
|
|
|
|
I've got that!
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
|
|
|
|
|
You also need ControlStyles.AllPaintingInWmPaint , which is documented in the ControlStyles enumeration documentation.
Also, that enum is attributed with the FlagsAttribute , which means you can save your code some time and just use:
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true); This gets compiled as a constant value so SetStyle is only called once.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Do I do one or the other or do I set the styles AND do the manual double buffering witht the bitmaps and the two graphics objects?
|
|
|
|
|
Have you enabled double buffering in your form?
Do this in your initialisation (I put it in my Form constructor after the IntializeComonent() call.
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
this.UpdateStyles();
|
|
|
|
|
Nope, that didn't help.
|
|
|
|
|
How do you call your DrawGraph( ) function?
|
|
|
|
|
The DrawGraph() function is called from OnPaint()
Invalidate is called every time the draw graph button is clicked or a track bar is moved changing one of the values in the graph.
protected override void OnPaint(PaintEventArgs e)
{
if(this.m_bmImageOffScreen==null)
this.m_bmImageOffScreen=new Bitmap(this.ClientSize.Width,this.ClientSize.Height);
this.m_gOffScreen =Graphics.FromImage(this.m_bmImageOffScreen);
//this.m_gBuffer.Clear(this.BackColor);
if(this.m_bgraphDrawn)
this.DrawGraph();
else
{
this.textBox1.Text = "0";
this.textBox2.Text = "0";
this.textBox3.Text = "0";
this.textBox4.Text = "0";
this.textBox5.Text = "0";
this.m_bgraphDrawn = true;
this.DrawGraph();
}
this.m_gOffScreen.Dispose();
e.Graphics.DrawImageUnscaled(this.m_bmImageOffScreen,30,50);
}
private void trackBar5_Scroll(object sender, System.EventArgs e)
{
// Display the trackbar value in the text box.
textBox5.Text = "" + trackBar5.Value;
value5 += trackBar1.Value;
//this.redrawChart();
//this.DrawGraph();
Invalidate();
}
|
|
|
|
|
what is a better way to implement a server with multiple clients ,using an Event driven handling or to init new
threads to handle each Event?
|
|
|
|
|
I guess it depends on what you mean by "server", which is a rather generic term. If you're referring to a socket listener, then waiting for a connection and passing the new request off to a thread is a good idea. You can also use the event-driven method and use BeginInvoke (it may not show up in IntelliSense, but it's there) which invokes the handler asynchronously.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
According to MSDN doc, when I derive a class that implements IDisposable, I need to override protected virtual void Dispose(bool disposing). And in this function, if disposing is true, I need to call Dispose() on all managed objects that I own (in addition to releasing unmanaged resources).
Now, when I create a new Form object, the wizard gives me the overridden Dispose(bool) method with the only statement being:
if (disposing)
{
if (components != null)
components.Dispose();
}
base.Dispose(disposing);
However, if I add a control, say a button named button1, it doesn't add the statement button1.Dispose() into the above method. Is this a mistake? I mean, do I need to manually add this statement, or is it already called implicitly somewhere?
|
|
|
|
|
First, the Dispose(bool) method doesn't NOT need to be overridden and is not declared in IDisposable . IDisposable declares one method: void Dispose() . Dispose(bool) is a virtual method inheritted from Control . You can implement IDisposable on anything. You don't need to override Dispose(bool) unless you contain components that need to be disposed, like AxHost controls (ActiveX controls in a managed environment) and stuff like that.
Managed objects are managed by the garbage collector. They will be garbage collected when necessary. You typically implement IDisposable (or override Dispose(bool) in your controls) when you need to clean-up unmanaged resources like window handles, file handles, and the like. These things are unmanaged, i.e. they are not managed by the garbage collector (GC). Objects that don't need to free unmanaged resources can implement and use IDisposable to free managed resources, but should use GC.SurpressFinalize(this) in the Dispose() implementation to prevent the GC from wasting time cleaning it up (since you already did).
Other reasons for implementing IDisposable and for callers to call Dispose is to release resources immediately since the GC doesn't run continuously. This makes sure that resources (even managed resources) are released when you want them to be. For things like a Button , it typically doesn't matter. The GC will get around to it. Dispose it only if it's absolutely necessary (otherwise you degrade the performance of your application since the GC is taking time away from your program's execution).
See Programming for Garbage Collection[^] in the .NET Framework SDK for more information and examples.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Actually I wrote the email after reading the Proramming for Garbage Collection article. I guess the reason why I need to Dispose() managed objects like Button() is if holds unmanaged resources, such as maybe icon, brush, etc. But now I realise that I probably don't need to call Dispose() directly on these controls, because when I call base.Dispose(bool) in the Dispose(bool) override that I have in my form, it will presumably call Dispose() for every control that it has (in the Controls collection). Other components such as Timer will be disposed when I call components.Dispose(). This is a guess, which I will verify later on.
|
|
|
|
|
You're really worrying about it too much. The controls and many of the other classes in the FCL will clean themselves up just fine in due time when the GC comes around to collect them. Unless you're strapped for memory (which could be signs of a bad design as well), don't worry about it. If a class should be explicitly disposed, it will typically tell you, like the Graphics class if you create it yourself (never dispose of it from the Paint event or the OnPaint override - the callee will do that when it's ready).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi all,
This might be a silly question but here it goes. I have some dll which i've heard is made with c++ but i would like to use them in my C# code. I hope that the functions i am looking for is in those dll's. I am not sure.
the dll's are located here : http://www.pictureflow.com/YarcPlus/Dist/YarcPlusCanonSDK_V10.exe
Hope that somebody can give a answer/example how to do this.
Best Regards
Mihai Marinescu
|
|
|
|
|
That's an EXE, not a DLL, and can't be mapped into memory so you can't use it. Judging by the name, however, it looks like a self-extracting ZIP or installation program, so extract/install it and find the DLLs you need, which should be documented with the functions you need. Then see the DllImportAttribute documentation in the .NET Framework SDK for information about how to declare native functions using P/Invoke. You'll also find more information on MSDN by reading Consuming Unmanaged DLL Functions[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You have to import the functions using the DllImport attribute, like this:
[DllImport("user32.dll")] public static extern IntPtr GetWindowDC(IntPtr hWnd);
But, of course, you have to know the function names ahead of time.
RageInTheMachine9532
|
|
|
|
|
Hello,
I am sure similar problem must have been here many times, but I am unable to find it, so I am writing about it again. I have a class SSocket, a child of System.Net.Sockets.Socket, the only difference is that it has two static methods, so in worst case it won't be problem to change the architecture a little.
When I retreive a client socket from socket.Accept() and try to cast it to my SSocket variable, exception is thrown, saying the cast is invalid. Can anyone say, what am I doing wrong?
This is how the code looks like:
class SSocket : Socket{
SSocket(..parameters..) : Socket (..same parameters as passed to child constructor..)
public static void GetCommand()
{...}
public static void SendCommand(...){...}
}
...
{
SSocket clientSocket = (SSocket)serverSocket.accept(); <- exception is always thrown
Thanks
John
|
|
|
|
|
The return, of course, is a Socket , not an SSocket , so you can't cast because it isn't that type. Casting to a parent always works, and in a polymorphic design if a method returns the parent class of a deriviative object but the object is a type of the derivative, you can then cast down, like so:
public class Parent
{
public abstract Parent ReturnMe();
static void Main()
{
Child child = (Child)new Parent();
Console.WriteLine(child.ReturnMe());
}
}
public class Child : Parent
{
public override Parent ReturnMe()
{
return this;
}
} That's not the case here.
I recommend encpasulating the Socket instead of deriving from it. This also gives you more control over what the caller can do so you only expose what you want to expose.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks, I was already quite reconciled with encapsulating the Socket instead, but wanted to know the reason, why the downcasting wasn't possible.
So, to be sure I do understand it - the child class must not introduce any new members or methods in order to be able to accept it's parent?
John
|
|
|
|
|