|
Does anyone know if this value is where your executable will be installed from an install package. I need to be able to reference a database that will be packaged with the setup and want to make sure the connection string points to the proper location. Right now I am testing in Debug mode and so of course the value that Application.StartupPath.ToString(); returns in inside the Debug subfolder. Does this change with a release version build?
Nick Parker
The goal of Computer Science is to build something that will last at least until we've finished building it. - Unknown
|
|
|
|
|
|
how to use it OutputDebugString ?
or its equvalient method in dot net?
r00d0034@yahoo.com
|
|
|
|
|
Look at System.Diagnostics.Debug
In particular the WriteLine method.
Paul
|
|
|
|
|
Hi Code Gurus,
(in a C# Windows Form application) How can I access a public member of the form that defines the static Main() member function from another form? I have two forms, frmMain (defind in frmamin.cs) and frmDialog (defined in frmDialog.cs), I want to access a member of frmMain (a property or a function or whatever member) from within frmDialog. When the application runs, the frmMain form is displayed as the application's main window, if the user chooses Connect... from the application's menu, the other form frmDialog is displayed as a dialog using ShowDialog(), now what I want is to access some member fields of frmMain from within frmDialog while frmDialog is displayed as a dialog box (that was extremely easy in VB).
Well, to be more specific, what I want to do exactly is to enable the user to connect to a database, when the user selects Connect... from the menu, a dialog box is displayed to allow the user to enter the database connection information. There is an OleDBConnection object (public member), defined in frmMain, that I want to use in frmDialog (there is a button in frmDialog whose caption is "Connect"). In the event handler of that button I want to use the public member of frmMain (System.Data.OleDB.OleDBConnection m_OleDBConn). For example like this:
myfrmMain.m_OleDBConn.ConnectionString = ... etc
Regards,
Waleed
(a.k.a. Wal2k) www.wal2k.com
|
|
|
|
|
Hi again
Waleed wrote:
that was extremely easy in VB
Even easier in C#
define a construtor for the dialogform that takes a parameter of the same type as your mainform. IOW:
public class DialogForm : Form
{
private MainForm parent;
...
public DialogForm(MainForm parent)
{ this.parent = parent;}
...
void SomeFunction(){
parent.OleDbConnection.Connect();
}
}
Thats it Also , dont make OleDbCOnnection public , but rather create a Property for it.
Give them a chance! Do it for the kittens, dear God, the kittens!
|
|
|
|
|
Well, actually I've done this (another way I used was to move Main() to a startup class and define a static member of the main form) but I was wondering whether we can do it the VB way:
in Form2
Form1.MyMethod()
(a.k.a. Wal2k) www.wal2k.com
|
|
|
|
|
Waleed wrote:
another way I used was to move Main() to a startup class and define a static member of the main form
I guess you can do this , although some peolpe might not agree to having a static member on a form.
RE: Properties, thats just way MS wants it . A property is basically just a get/set function() for a private member. Again, its not strictly required, but rather recommended.
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.
|
|
|
|
|
That was the VB6 way, you can't do that anymore under VB.NET or C#. It's part of the whole OOP thing...
Norm Almond: I seen some GUI's in my life but WTF is this mess
Leppie: I made an app for my sister and she wouldnt use it till it was colorful enough
Norm:good point leppie, from that statement I can only deduce that this GUI must be aimed at children
Leppie:My sister is 25
-Norm on the MailMagic GUI
|
|
|
|
|
Waleed wrote:
Form1.MyMethod()
IMNSHO, I always considered that a BAD thing to do; instead you should have created an instance of Form1 and passed that to Form2.
If you didn't create an instance of it, you can only use the window once; which is a pain when you are showing inter-related data and the same window needs to be used twice.
James
Sig code stolen from David Wulff
|
|
|
|
|
Funny sig!
Norm Almond: I seen some GUI's in my life but WTF is this mess
Leppie: I made an app for my sister and she wouldnt use it till it was colorful enough
Norm:good point leppie, from that statement I can only deduce that this GUI must be aimed at children
Leppie:My sister is 25
-Norm on the MailMagic GUI
|
|
|
|
|
Thanks, I was cleaning the root of my C drive when I came across my quotes.txt file so I just had to share that one
James
Sig code stolen from David Wulff
|
|
|
|
|
BTW, why not to make OleDBConnection public?
(a.k.a. Wal2k) www.wal2k.com
|
|
|
|
|
Hi Code Gurus,
(in a C# Windows Form application) How can I detect that the Enter key was pressed in a single line TextBox control in .NET? I want to enable the user to type in some text in the TextBox (a command) then by pressing Enter an action is taken (like the address bar in IE). This can be done very easily in VB and VC++. But in .NET, the only way I discovered so far is to use a multi-line textbox control and detect when the user presses Enter (using either KeyDown or KeyPress events) then remove the new line characters from the text, this is other than deleting any multine text pasted in the control except the first line. I believe there must be a better and more elegant way to do this but I just don't know it!
Regards,
Waleed
(a.k.a. Wal2k) www.wal2k.com
|
|
|
|
|
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
|
|
|
|