|
I don't know about other peoples standards--this is what I use:
private instance fields: _camelCasing
private static fields: s_camelCasing
public instance/static properties: MixedCase
any methods: MixedCase
any classes: MixedCase
local variables: camelCasing
local parameters: camelCasing
controls on a form: _camelCasing (because after all, they are just private fields)
Since there are no global functions or variables in C#, you can make various assumptions in your code. If you have the following snippet:
variable = "value";
Then you know that you are either have a local parameter to the current method, a local variable, an instance field/property, or a static field/property. Local parameters and local variables can be treated in a similar way (because they are all local to the current method). I use camel casing preceded with an underscore for private instance fields (so that I don't have to deal with name clashes). I precede static fields with "s_" for the same reason.
The VS.NET IDE provides tooltips with detailed information about the variable/type that the mouse hovers over. Embedding the type into every variable name becomes somewhat redundent.
--
Peter Stephens
|
|
|
|
|
There are times in my application that I want to prevent the user from exiting the application window with the exit button (the X button in the upper right hand corner of most window applications).
I want in some circumstances to print out a message box and to let the user know that the application is in a state that cannot exit the application and to keep it running.
The following code catches that application exiting from clicking on the exit button, and prints out the message box. But how can I stop the application from continuing with the exit ?
Actually, by the time the message box is printed, the application window is already closed.
I am fairly new at C #, any help is appreciated.
static void Main(string [] args)
{
Application.ApplicationExit += new EventHandler(ApplicationExitEventHandler);
Application.Run(new CsMain());
}
static void ApplicationExitEventHandler(Object sender, System.EventArgs e)
{
if (state != CLOSED_STATE)
{
MessageBox.Show("Error ! \n\nThe line is still open. You must close the \nline before terminating the application", "CsTdrv", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
|
|
|
|
|
Hi,
Here I am assuming you are inheriting from the System.Windows.Forms.Form class. In the class add an EventHandler to the Closing Event e.g.
Form.Closing += new CancelEventHandler(MyClose);
Then create the code in here, you can cancel the event using the CancelEventArgs e.g.
MyClose(object sender, CancelEventArgs)
{
.......
if(shouldNotClose)
e.Cancel = true;
}
Hope this helps,
Andy
|
|
|
|
|
I need to write a help application for a windows desktop program like the one that appears in many of Microsoft's windows products that includes a Contents tab, Index tab and a Search tab with a results window to the right side. Can anyone offer some direction to help me get started?
Kyle
|
|
|
|
|
Hello eveyone,
I want to handle Visio events from C#. Do you have any idea on how to do this?
Visio object has some Event handlers but I couldn't use them.
|
|
|
|
|
I have a treeview with the nodes inside the treeview. I would like to show the contextMenu on the node selected whenever the node is selected.
Unfortunately, i can't do that because the node is not a control.
Anyone out there can help, please????
Thank you
Regards,
cK
|
|
|
|
|
put a ContextMenu on your Form
private System.Windows.Forms.ContextMenu contextMenu;
Example:
(I used a Treeview (treeview) with 3 Nodes, every Node has one SubNode
and my ContextMenu has two MenuItem's)
Declare two Menu-Items for the ContextMenu:
private MenuItem contextMenuItem1 = new MenuItem();
private MenuItem contextMenuItem2 = new MenuItem();
Add the following Code to your Constructor:
//Initialize Context-Menu
this.treeView.ContextMenu = this.contextMenu;
contextMenuItem1.Text = "Action 1";
contextMenuItem2.Text = "Action 2";
this.contextMenu.MenuItems.Add(0,contextMenuItem1);
this.contextMenu.MenuItems.Add(1,contextMenuItem2);
contextMenuItem1.Click += new System.EventHandler(this.contextMenu_Action_1);
contextMenuItem2.Click += new System.EventHandler(this.contextMenu_Action_2);
Override the MouseDown Event from your Treeview Control:
private void treeView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button != MouseButtons.Right) return;
this.contextMenuItem1.Text = "Action 1";
this.contextMenuItem2.Text = "Action 2";
TreeNode selectedTreeNode = this.treeView.GetNodeAt(new Point(e.X,e.Y));
if(selectedTreeNode != null)
{
this.contextMenuItem1.Text = "Action 1 (Selected Node = " + selectedTreeNode.Text + ")";
this.contextMenuItem2.Text = "Action 2 (Selected Node = " + selectedTreeNode.Text + ")";
}
}
Implement the Funktions for the Eventhandler:
private void contextMenu_Action_1(object sender, System.EventArgs e)
{
if(this.contextMenuItem1.Text == "Action 1")
MessageBox.Show("Action 1 fired for Treeview");
else
MessageBox.Show("Action 1 fired for selected Node in Treeview");
}
private void contextMenu_Action_2(object sender, System.EventArgs e)
{
if(this.contextMenuItem2.Text == "Action 2")
MessageBox.Show("Action 2 fired for Treeview");
else
MessageBox.Show("Action 2 fired for selected Node in Treeview");
}
I Hope this was helpfully for you.
rgrds
Martin
|
|
|
|
|
I have solved the problem. Anyway, thank you for your helpful code!!
|
|
|
|
|
{Hi:I'm writing a program in C to shut-down Windows OS; I'm wondering if any of the members know the code,or how to access the "kill" sequence(not the code to load the "shut_downDialogBox)Thanks for your "input")}
|
|
|
|
|
ExitWindowsEx()
look it up in msdn..
|
|
|
|
|
How can I obtain the local IP address in C#?
Thanks much to anyone that can help me!!
|
|
|
|
|
|
I am having problems getting a simple timer to work. I am very new to C#. When I go to compile the code below I get the following error:
An object reference is required for the nonstatic field, method, or property 'TimerTest.mStartTime'
Anyone got any ideas ?
using System;
using System.IO;
using System.Timers;
class TimerTest
{
public DateTime mStartTime;
public static void Main()
{
Timer tmr = new Timer();
tmr.Tick += new EventHandler(OnTimedEvent);
tmr.Interval= 1000;
DateTime mStartTime = DateTime.Now;
Console.WriteLine("Timer test running...");
tmr.Enabled = true;
while(true)
{
}
}
public static void OnTimedEvent(object source, EventArgs e)
{
String timestring;
DateTime now = DateTime.Now;
TimeSpan diff = now.Subtract(mStartTime);
timestring = (diff.Hours.ToString("d2" + ":" + diff.Minutes.ToString("d2") + ":" +
diff.Seconds.ToString("d2"));
Console.WriteLine("{0}",timestring);
}
}
|
|
|
|
|
B. Wood wrote:
public DateTime mStartTime;
public static DateTime mStartTime;
Since your methods are static, the data it accesses must be static, or have a reference to an instance of the class.
James
Sonork ID: 100.11138 - Hasaki
"Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. " - Michael P Butler Jan. 18, 2002
|
|
|
|
|
Thanks James, that did the trick.
|
|
|
|
|
Hi,
is there a possibility to call functions from a DLL written with C# from managed/unmanaged c++ code?
I know it is very simple to create a C#DLL and then use the function in a C# Project with adding a reference. This is really great and simple.
But...
I have a very big project written with VC++6.0. I solved all problems after converting it to VC++7.0. But now I want to use the big, great new features like ADO.NET to connect to a SQL-Server. So I thought to write some C#DLLs with all the functions I need and then call these functions from C++. How can I do this?
I tried several things.
1.) Is it possible to add a reference to a C#Dll from C++ ? I can't see a posibility...
2.) The old common way with LoadLibary is not possible, because I can't declare my function in the Dll as a dllexport function, and because of this GetProcAdress returns zero.
So how can I declare my function for external use ?
Any ideas??
Thanks for help.
|
|
|
|
|
The easiest thing to do would be to wrap your C# dll with COM (C# classes can be exposed as COM objects), then you can use COM to call into your C# dll...
In MC++ you might be able to #using your dll like you would the system dlls.
HTH,
James
Sonork ID: 100.11138 - Hasaki
"Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. " - Michael P Butler Jan. 18, 2002
|
|
|
|
|
The easiest way is to write an unmanaged C++ library that exports the functions you want. It then contains managed C++ that calls your C# classes directly. This gives you the best of both worlds, but requires two files. One for C++ and one cor C#.
|
|
|
|
|
ohhh! I forgot about that
James
Sonork ID: 100.11138 - Hasaki
"Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. " - Michael P Butler Jan. 18, 2002
|
|
|
|
|
Hello everybody,
Does anyone know how to trap the arrow keys in a class derived from usercontrol?
I noticed that the arrows keys don't fire a keyup, keydown or keypress event,
instead they move the focus to other controls on the form like the Tab key does.
I want the control itself to be able to process the arrows keys without having to add code to the form that is it in.
Thanks in advance
Paul
|
|
|
|
|
Look at the IsInputKey protected method on the usercontrol.
protected override bool IsInputKey(Keys keyData)
{
this control,
if (keyData == Keys.Left ||
keyData == Keys.Right ||
keyData == Keys.Up ||
keyData == Keys.Down)
return true;
else
return base.IsInputKey(keyData);
} Then I think the OnKey* events will fire for it, the docs aren't too clear on that aspect.
Your control will have to have the UIPermission.AllWindows security permission to run that code however.
HTH,
James
Sonork ID: 100.11138 - Hasaki
"Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. " - Michael P Butler Jan. 18, 2002
|
|
|
|
|
Hi everyone,
I have a web forms page, which contains a dropdownlist. The dropdownlist posts back to an event handler which adds a new button to the controls collection of a PlaceHolder. The text property of this button is set to the text of the DropDownList's selected item. My problem is that after the first time I select my dropdownlist, it changes the text of the existing button in the PlaceHolder, as apposed to adding a new Button to the placeholder.
How can I do that?
I very much appreciate any help I can get this on this.
Thanks.
|
|
|
|
|
Try searching the DOTNET mailing list archives; here is one post that might help a little bit, though I could have sworn I've seen more similar to what you're trying to find.
Sorry I can't be of more help,
James
Sonork ID: 100.11138 - Hasaki
"Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. " - Michael P Butler Jan. 18, 2002
|
|
|
|
|
I need implement a singleton in C#. I have tried making a static class but I seem to be getting members that go out of scope. What is the best was to do this? Thanks.
Steve
|
|
|
|
|