|
Sorry, I have meant the Form.Closing sample you have mentioned about and not the event! I have made a pretty stupid mistake and haven't checked what I have written.
I understand about Application.Exit now. I thought that it isn't generally a good idea to use Application.Exit at all.
BTW I have replaced Application.Exit with:
private void buttonExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
Then I handle the Form.Closing event to ask the user if he really wants to quit etc. Is it a good idea or not? It seems to me like an implementation of your idea.
|
|
|
|
|
Yes, that is the way to handle it. Form.Close will send the Windows messages to close the Form . You're handling the WM_CLOSING message and optionally cancelling it. If you don't, the Form continues to close. When it's closed, if the message pump was waiting on it (i.e., you passed an instance of that Form to Application.Run ), then the application will exit.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Yes there is... it's called Closing... and when you handle it you can do all kind of stuff (including cancellation of closing) with it
|
|
|
|
|
I was messing around in C# express and I though it was cool how everything is "visual styled". When I put a tab control on my form, the background is all fancy and has a gradient. that's great I say to my self... but then I add a label or something, and the backgrounds don't match - it looks really ugly. How can I fix this?
/\ |_ E X E GG
|
|
|
|
|
First search and - if not reported already - report a bug at http://lab.msdn.microsoft.com/vs2005/[^]. This has to do with the Theme API and how they're using it since a TabPage and GroupBox require owner-drawing with the P/Invoked Theme API. This was a known bug with .NET 1.x that they attempted to fix with .NET 2.0. As you can see, it still has problems (but it's beta, so now is the good time to report bugs).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Friends
I want to make a project in C# so that i can get the practical knowledge of
the basic features as well as the advanced features of the C#.
Please if any one knows from which site i can have the idea of the project.
Im in Software field and have experience in C++,C,C#,VC++.
Any help is most appreciated.
Naveen
Naveen
|
|
|
|
|
Um...how about this one? CodeProject overs thousands of articles in many different languages. Go to the homepage and browse the categories that are applicable to what you want to learn.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanx a lot Heath.
Actually i want a topic on which i can make a project.
In Code project the projects are already made.
Please help me in this.
As you have been managing lots of projects u sure would be having the
idea of the project that i must do.
Waiting
Naveen
Naveen
|
|
|
|
|
Do you think every project in this world is unique? Hardly. Find a project that looks interesting - maybe a few - and code something similar (I didn't say to copy their source). This way, you have reference while you learn but above all else, be sure to read through the .NET Framework SDK. This is like some simple calculator you can fumble through to learn - it is an advanced framework that requires reading otherwise you're nothing more than a coder, and anyone can simply write code.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi All,
I have added a menu with one menuitem titled "(No Filter)". Based on the selection in another menu I want to populate this menu with items. Then if the menu item changes in my other menu then I want to clear all the items except the "(No Filter)" Item and repopulate the items filtered by the new menu selection.
I have been able to fill the menu items the first time but when I select my filter and repopulate my items the list disappears. I am using the following code to remove the current list before populating.
for(int icount=2;icount < imnu_count;icount++)
{
this.mnuLocation.MenuItems.RemoveAt(icount);
}
I am starting the count at two to preserver the "(No Filter)" Item and using this code to populate the list again.
//Add the items to the Menu
MenuItem menuItem1 = new MenuItem(cs_location_string[0,icount]);
menuItem1.Click += new System.EventHandler(this.mnuLocationNF_Click);
this.mnuLocation.MenuItems.Add(menuItem1);
When I look at my menu it has no items at all
Any help would be appreciated.
thanks
Stephen
|
|
|
|
|
You do realize that arrays in .NET are 0-based, so that the second MenuItem actually starts at index 1, don't you? You're starting at index 2, which is actually the third MenuItem (which could cause problems depending on the current state of your application).
Also keep in mind that when you're removing items from the MenuItem.MenuItems collection property, the collection is being re-indexed. You should actually have a reverse for-loop that starts with Count - 1 and works back to > 0 , or at leat loop without changing the value passed to RemoveAt . Examples of each way follows:
for (int i=menuItem1.MenuItems.Count - 1; i > 0; i++)
menuItem1.MenuItems.RemoveAt(i);
for (int i=1; i < menuItem1.MenuItems.Count - 1; i++)
menuItem1.MenuItems.RemoveAt(1);
Also, context-sensitive menus are typically better to fill before being opened. This is a good use of the MenuItem.Popup event. Handle this instead of re-filling your menu list when an item is invoked. This gives you must getter control over what gets displayed and is similar to what many other applications do to provide context-sensitive menus (the Windows shell, various Office applications, Visual Studio, etc.).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I'd like to disable mouse scrolling on the AutoScroll bars controls get.
I'm at a loss as to how to do this however.
Anyone know how?
cheers
Cata
|
|
|
|
|
As with many lower-level solutions, override WndProc and ignore (i.e., don't call base.WndProc ) the WM_MOUSEWHEEL (0x020a) message:
protected override void WndProc(ref Message m)
{
if (m.Msg != 0x020a) base.WndProc(ref m);
} Override this in a derivative of the class in which you want to disable the effects of the mouse wheel.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hey all. I suppose this is easy really, but I don't know how to do it.
How can I catch exceptions that occur in message handlers i windows forms? It's also gotta be before the default message pump exception handler.
I have some exceptions that can occur at a million places on user input, and should be handled centrally.
Thanks in advance.
|
|
|
|
|
Honestly, there's no reason an exception should be thrown to the message pump. Following the doctrine of "never trusting user input", all input should be validated prior to doing anything with it.
In any case, Application.ThreadException should typically do the trick. Even the event documentations states about what I said above, though. Always validate user input as close to the input as possible (i.e., the presentation layer or in methods and property set accessors that can be called by outside code).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I'm working on some translation software that integrates third-party translation components, and the component sed to handle Japanese uses a non-Unicode encoding (Shift-JIS, for those in the know) that doesn't mashal all that easier to one of the stanford encodings supported by .NET. As a result, I need to open the Languages control panel and coach XP to assume that all non-Unicode text should be interpreted as Japanese.
However, when Japanese is selected, .NET strings that I think are storing German words don't display properly.
char umlautedU = 0x00FC;
char eszet = 0x00DF;
StringBuilder builder = new StringBuilder();
builder.Append("Schl");
builder.Append(umlautedU);
builder.Append(eszet);
Console.WriteLine(builder.ToString());
prints out Schlus to the console, although it DOES print out SchluB (where the u is really an umlauted u and B is the eszet which looks like the scripted capital B) just fine when the language for non-Unicode applications is set to English.
I'd always assumed that .NET strings printed correctly regardless of OS settings and the OS's treatment of non-Unicode strings. Anyone have any idea how to print German regardless of the language setting?
Thanks for your help,
Jerry Cain
jerry@cs.stanford.edu
|
|
|
|
|
I'm not sure I quite understand your question, but if you're printing to the console, you're grossly limited by the font support for the console. Even with Windows Forms you must use a font that contains glyphs for the character you want. Some font formats will specify what character sets they support. See a previous thread where I posted some code for EnumFontFamilies(Ex) that enumerated this information (all chracter sets for all font families).
Now, what I think you're asking is how to display one culture in the UI while the OS is set to another. This is done by setting Thread.CurrentCulture to a CultureInfo (like new CultureInfo("de-DE") for German region and language) for regional settings (dates, times, numbers) and Thread.CurrentUICulture for determining which CultureInfo classes like the ResourceManager and ComponentResourceManager should use for resolving resource files (that would contain localized strings and other types with TypeConverter s that can convert from/to strings).
If you've used the designer to localize an app (bad idea with VS.NET 2002 and 2003 which uses the ResourceManager , as the number of calls necessary adds a lot of size to your assembly and slows performance; VS 2005 will use the ComponentResourceManager which is more efficient in the way it works), then you have to set Thread.CurrentUICulture before InitializeComponent is called. If you want to swap languages at runtime, you need to extract the initialization code (not the instantiation code, though - you only want to instantiate controls and support types once) and call a method that re-runs that code (basically).
Be sure to read Developing World-Ready Applications[^] in the .NET Framework SDK for more information.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
How can i show a border(highlight) around a control that is selected by a user. And how can controls be resized by using mouse.
Please let me know.
Thanks
smartyosu
|
|
|
|
|
when i align the tab control to left or write the text on the tabs doesnt apear at run time ? (the tab text is empty)
|
|
|
|
|
Hi,
I am working on a windows application where the user should be able to move,resize and drag and drop a control (a picture box in my case). I have both MouseDown and Click events for this control. But I cant really use the Click event because everytime I press the mouse button on the control the MouseDown event fires up. Is there a way that i can differentiate between MouseDown and Single Click. Please let me know.
Thanks
smartyosu
|
|
|
|
|
Use a timer. The actual implementation should be placed in the timer event handler (just depends on which Timer class you use; for example, the System.Windows.Forms.Timer.Tick event). The MouseDown event handler starts the timer. If the timer elapses, your original MouseDown implementation is executed. The Click event handler should stop the timer. The timer duration should be a single occurance (non-repeating) and should be at least SystemInformation.DoubleClickTime (an extra 10 milliseconds should be plenty in case of lag).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
How can I make an event handler for every time a certain method "Send" is called?
Or actually event better. An event handler for when a new item is added to a listbox.
/\ |_ E X E GG
|
|
|
|
|
The event pattern in .NET is quite easy. The correct approach is define a delegate (using EventHandler or some EventHandler -like syntax (like delegate void MyEventHandler(object sender, MyEventArgs e) ). Then you declare your event on your class, as well as define an OnEventName method.
The naming convention follows: for an event name "MyEvent", you define a delegate and members like so:
delegate: MyEventHandler
arguments: MyEventArgs
event name: MyEvent
handler: OnMyEvent
So an example class would look like this:
public delegate void SentHandler(object sender, SentArgs e);
public class Example
{
public event SentEventHandler Sent;
protected virtual void OnSent(SentEventArgs e)
{
if (Sent != null)
Sent(this, e);
}
public void Send(string message)
{
OnSent(new SentEventArgs(message));
}
}
public class SentEventArgs : EventArgs
{
string message;
public SentEventArgs(string message)
{
this.message = message;
}
public string Message
{
get { return message; }
}
} You define a protected virtual member named OnEventName so that derivative classes can override it (and they should call base.OnEventName ) without handling the event, which require far more IL statements and doesn't allow for as much handling (like the ability to not have the base handler called, which is actually what raises the event).
This is also documented in the .NET Framework SDK.
What you want to do for a ListBox isn't at all easy. While you can extend ListBox and ListBox.ObjectCollection to override ListBox.CreateItemCollection and return your own ListBox.ObjectCollection derivative, you can't override Add , Remove , etc. While you could hide them using new , your definition won't be called because the ListBox internally refers to the ListBox.ObjectCollection class.
With member hiding, the call is not virtual. This means that in order for your implementation to be called, your class must be the class that's referred to. The following example should demonstrate better:
public class Test
{
static void Main()
{
Test t = new Test();
t.A();
t.B();
t = new Test2();
t.A();
t.B();
((Test2)t).A();
((Test2)t).B();
}
public void A()
{
Console.WriteLine("Test.A");
}
public virtual void B()
{
Console.WriteLine("Test.B");
}
}
public class Test2 : Test
{
public new A()
{
Console.WriteLine("Test2.A");
}
public override void B()
{
Console.WriteLine("Test2.B");
}
} This would print the following to a console:
Test.A
Test.B
Test.A
Test2.B
Test2.A
Test2.B So, as you see, unless it's Test2.A being called instead of Test.A when hiding the member, Test.A is called. This is the difference between the call IL instruction that is executed for A() where the callvirt IL instruction is executed for B() .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I'm looking for a control which is similar to the gradient bar seen when windows starts up. Yes, the one where the colors move from left to right. I'm pretty sure I've seen it somewhere around, but can't find it anymore.
Thanks for any help.
Matthias
If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs.
(Lounge/David Wulff)
www.emvoid.de
|
|
|
|
|
Dear sir,
i want to navigate between different controls in my windows form using enter key. how it is possible. all the events that i hve see is for specific object like form keydown event whenever focuss chage to another child control this event dose't work.
thnx alot.
farukhalifarooq
|
|
|
|