|
Hi
See my post below about overriding and event for a button. Now all you will need to do is override the OnKeyDown Method.
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
else base.OnKeyDown(e);
}
This way you ensure that the Enter key was properly captured and not passed onto the base class aka TextBox.
Hope this help
Give them a chance! Do it for the kittens, dear God, the kittens!
|
|
|
|
|
Thanks a lot, I'm going to try this out.
(a.k.a. Wal2k) www.wal2k.com
|
|
|
|
|
Three things:
1) Keys.Enter and Keys.Return are two seperate things.
2) Do you have an AcceptButton set up for the form (what used to be called the Default Button)?
If so, you'll never get the KeyDown event, even the way leppie describes, because the AcceptButton's Click event is executed first.
If not, why don't you set up an AcceptButton? (go to the form's properties, find AcceptButton and drop down a list of buttons)
3) If you must and can use the KeyDown event, handle it, don't override it. eg.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter)
{
e.Handled = true;
}
}
Paul
|
|
|
|
|
Paul Riley wrote:
e.Handled = true;
I probably wont even remeber this I'm so tired . Nice tip!
Give them a chance! Do it for the kittens, dear God, the kittens!
As seen on MS File Transfer: Please enter an integer between 1 and 2.
|
|
|
|
|
Well, System.Windows.Forms.Keys.Enter and System.Windows.Forms.Keys.Return are equal, both of them equal 13 (I checked that myself, just cast them to an integer and display the value in a message box) Actually, I don't know why Microsoft provide both of them.
(a.k.a. Wal2k) www.wal2k.com
|
|
|
|
|
Waleed wrote:
System.Windows.Forms.Keys.Enter and System.Windows.Forms.Keys.Return are equal
You're right, which is odd because the first time I coded it as Keys.Enter and it didn't work, so I've used both ever since. Go figure.
I assume they offer both with other OSs in mind, allowing some frameworks to differentiate between the two.
I have an idea for your other situation but I want to give it a quick test and it may be a sizable reply . Bear with me a minute.
Paul
|
|
|
|
|
Thanx for replying my posting, I wonder why it is so hard to do such a trivial thing in .NET ... this is extremely easy to be done in VB or VC++. Actually I want to have an AcceptButton in the form to connect to a database. Ok, here are the details:
I have a form which contains two text boxes and a button (with the caption "Connect") in a groupbox, one of the textboxes is used for entering a path to a DB file, the other textbox is used for entering the DB password for that database (if any), when the button is clicked the application tries to connect to the database using the information in the two textboxes. The button is the AcceptButton for the form (to make it easier for the user .. just enter the location and password then press Enter). One last thing to mention about the button is that when the button is clicked and the connection is made to the datebase the caption of the button is changed from "Connect" to "Disconnect" and if the button is clicked again the connection is closed and the caption is changed back to "Connect" and so on. In the same form I have antoher groupbox which contains a text box and a DataGrid control (disabled if there is no connection to the database). What I want is after the user makes the connection to a database to be able to enter a SQL command in the text box and press Enter then the results of the query is displayed in the datagird control. Of course, this is not possible now because if the user enters a SQL command and then presses Enter, the AcceptButton is clicked and the connection is lost.
The whole purpose of this application is educational. For now, the only way to finish the application was to not use an AcceptButton in the form and use another button beside the SQL command textbox to execute the SQL command when clicked after entering the command in the textbox.
I just find it so natural to be done this way, I want to learn how. Many thanx
(a.k.a. Wal2k) www.wal2k.com
|
|
|
|
|
Before I start this, I want to point out that this isn't a fantastic design. This is the reason you're finding it so difficult. The problem is that C++ is capable of pretty much anything and VB is capable of pretty much anything you really shouldn't do. [half joking, I'll come back to this later]
It is possible though, and really quite easy.
You need to handle the Enter and Leave events of the SQL Command TextBox and use them to change the AcceptButton of the form.
private void textBox2_Enter(object sender, System.EventArgs e)
{
this.AcceptButton = button2;
}
private void textBox2_Leave(object sender, System.EventArgs e)
{
this.AcceptButton = button1;
} If you would rather not have an AcceptButton for this TextBox but would prefer to trap the KeyDown, you could replace button2 with null . I would suggest this is a really bad idea though, it should be obvious to the user that the AcceptButton has moved.
In the real world (non-educational), I would suggest quite strongly that this whole thing is bad design. In the real world, I would expect a menu containing Connect and Disconnect (only one active at any given time). Connect would give you a dialog for User/Password, Disconnect wouldn't. The only things I'd have in the main form is the SQL Command TextBox and the related AcceptButton.
As a consumer, I wouldn't buy your product designed to be so confusing.
Just a few thoughts.
Hope this helps
Paul
|
|
|
|
|
Well, may be you're right .. anyway, I was not thinking about program design when I sent my posting, I just wanted to know how to do it. You know, it's just about being used to doing something easily and after that you move to .NET and you find out that it is not possible anymore and you try to figure out how to do it, I guess it's all about curiosity. I wonder why the KeyDown event is not raised when Enter is pressed, I wish to understand their point of view.
|
|
|
|
|
Didn't mean to sound critical there, just offering several options and why I thought one would be better than another.
I assume it doesn't raise the event because it raises the form-level events first. The first thing in the form's Key events will be "if [enter pressed] and AcceptButton != null then simulate AcceptButton.Click".
The second thing it will do is "raise the same Key event for the active control".
While this sequence doesn't quite fit what you wanted it to do, it is the sequence that makes sense. People are more likely to want to trap key presses at the form level BEFORE the control level.
Paul
|
|
|
|
|
What I have discovered so far is that the KeyUp event is raised whenever Enter is pressed (unlike KeyDown). But it is still raised after the Click event for the AcceptButton is raised.
Now, I'm trying to use OnNotifyMessage() (first, you have to use SetStyle() to enable the control to receive Windows messages and derive your class from System.Windows.Forms.Form). I have to use the Message ID for the KeyDown Windows message (WM_KEYDOWN). I think it can be done this way, but there must be an easier way. Any clues?
(a.k.a. Wal2k) www.wal2k.com
|
|
|
|
|
An alternative way (that I just thought of while reading this) might be to trap the form's KeyDown event and then use ActiveControl to check if your SQL Command TextBox is active at the time.
This would, IMHO, be the worst of the several designs I've just suggested. The first button would still appear to be the AcceptButton - VERY confusing.
I'm just mentioning this for complicity.
Paul
|
|
|
|
|
i want to show a string in vs.net debug output window.
i think i can use Trace or Debug class. but i dont now how to use them to display a string in managed code ?
if there exist some other way plz tell?
i want to see the execution and flow of my program
can any bode tell?
plz write the complete syntex.i will be very thank full to u.
r00d0034@yahoo.com
|
|
|
|
|
Debug.WriteLine(string text);
or
Trace.WriteLine(string text);
or
Console.WriteLine(string text);
Give them a chance! Do it for the kittens, dear God, the kittens!
As seen on MS File Transfer: Please enter an integer between 1 and 2.
|
|
|
|
|
I am trying to open an Excel workbook to use in my C# application. I have found many examples of opening a blank (new) workbook using new Excel.Application(). How do I open a specific xls file using Excel.Application ?
//open Excel with new workbook
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Application.Visible = true;
I have been able to open a specific file using Process
string path = "C:\\";
string file = "flow.xls";
System.Diagnostics.Process xlProcess = new System.Diagnostics.Process();
xlProcess.EnableRaisingEvents = false;
xlProcess.StartInfo.FileName = "excel";
xlProcess.StartInfo.Arguments = path + file;
xlProcess.Start();
as long as the path has no spaces.
But with a path like
string path = "C:\\Documents and Settings\\userName\\My Documents\\data\\FlowData\\flow.xls";
I also tried a verbatim string
string path = @"C:\Documents and Settings\userName\My Documents\data\FlowData\flow.xls";
How do I add the spaces to the path?
When you come to a fork in the road, take it! Y. Berra
|
|
|
|
|
Excel.Application excel = new Excel.ApplicationClass();
excel.Visible = true;
Excel._Workbook wbk2 = excel.Workbooks.Open(@"c:\mydoc.xls",true,true,true,true,true,true,true,true,true,true,true,true);
if you start putting in too manay features, it no longer remains useful for beginners
quote in a CP article comment, shiraz baig
|
|
|
|
|
Great
Thanks
When you come to a fork in the road, take it! Y. Berra
|
|
|
|
|
Hi,
I am looking for a way to activate an event procedure, such as combox_clicked, without really clicking on the control . I remember in VB we can do thing like that (combox_clicked = true). Just wonder can we do this in C#? I appreciate your inputs.
|
|
|
|
|
Hi
You just need to call the event handler
Like:
First derive from Button, then add the following:
public void CallButtonClick()
{
base.OnClick(this, EventArgs.Empty);
}
Now , call the function from your code
Hope this helps, there seems to be many ways to do this though.
Give them a chance! Do it for the kittens, dear God, the kittens!
|
|
|
|
|
Hi Leppie,
I am not sure if I understand what you mean by
leppie wrote:
First derive from Button, then add the following:
public void CallButtonclick()
{
base.onclick(this, EventArgs.Empty);
}
Can you please give more detailed explaination? I really appreciate that!!;)
|
|
|
|
|
D Shen wrote:
I am not sure if I understand what you mean by
It's the beauty of inheritance
OK. Lets see how/why I say that
1. What we want is a button that we can programatically fire events. It makes sense to add this functionality to the Button class. So we make one:
public class ButtonEx():System.Windows.Forms.Button
{
public void CallButtonclick()
{
base.OnClick(EventArgs.Empty);
}
}
2. We replace our existing Button in the in the form with our new ButtonEx.
private System.Windows.Forms.Button button1;
private ButtonEx button1;
And in the windows forms designer region, change :
button1 = new System.Windows.Forms.Button();
button1 = new ButtonEx();
Be sure to save before viewing in the designer(in fact close it beforehand, it does more harm than good ).
3. Finally we just call our new ButtonEx's CallButtonClick methods as follows:
button1.CallButtonClick();
NOTE: If you'll be calling this method from thread u will need to invoke it. I'm not 100% sure, but it would look something like this:
button1.Invoke( new MethodInvoker(button1.CallButtonClick));
Hope this adds some insight
Give them a chance! Do it for the kittens, dear God, the kittens!
|
|
|
|
|
Thank you, leppie. It really helps!
|
|
|
|
|
leppie,
Here comes a minor problem
When I compile the code, I got "...does not contain a definition for..." this button1.
I followed the order you post here, but.... Please help!!
|
|
|
|
|
leppie,
I found the problem, it was so stupid.....(typo )
|
|
|
|
|
Hehe, thought it was either that or the designer screw'd up the UI
Give them a chance! Do it for the kittens, dear God, the kittens!
As seen on MS File Transfer: Please enter an integer between 1 and 2.
|
|
|
|