|
sure but i've just missed that the
System.ComponentModel.CancelEventArgs.Cancel property has to be set to true
BTW is this call a good idea:
this.OnClosing(new System.ComponentModel.CancelEventArgs());
Here I raise the System.Windows.Forms.Form.Closing event, when a button Quit is clicked on (or another exit condition is met).
|
|
|
|
|
to Raise the Form.Closing event you need to just call:
this.Close();
|
|
|
|
|
OK I have actually found out how it is done. Here is what a I did (nothing specially, really). I happens to me too often the solution is obvious but I miss it.
buttonExit_Click... etc... is an event handler... is it how you are going to do it? Thanx
private void buttonExit_Click(object sender, System.EventArgs e)
{
this.OnClosing(new System.ComponentModel.CancelEventArgs());
}
private void FormOfficeTools_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
System.Windows.Forms.DialogResult result =
System.Windows.Forms.MessageBox.Show(this,
"Желаете ли да излезете от програмата?",
"Изход от програмата",
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question
);
if (System.Windows.Forms.DialogResult.Yes == result)
Application.Exit();
else
e.Cancel = true;
}
#endregion
|
|
|
|
|
You don't need to call Application.Exit (in fact, I recommend you don't). The only condition you have to worry about is anything that would lead you to set CancelEventArgs.Cancel to true . If you don't set it, the form will continue closing as normal. The .NET Framework SDK even has a good example for the Form.Closing event.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you, Heath Stewart
What's wrong with Application.Exit ? Why don't you recommend using it?
How should I then quit an application?
I'm looking for the Form.Closing event... and still can't find it.
|
|
|
|
|
Because the form is already closing. If the form is your main form - the one you passed to Application.Run , once it's closed the application will exit anyway. There may already be messages in the pump that were posted that may need to clean-up resources. Calling Application.Exit circumvents that. So, if you don't set CancelEventArgs.Cancel to true , the form will close and the application will exit just as if you hadn't handled the Form.Closing event.
And what do you mean you can't find it? It's documented in the .NET Framework SDK, and if you use the designer you can click the "Events" button in the PropertyGrid when your form is selected, find the Closing event and double-click it. You can also use IntelliSense.
The thing is, you're not looking for Form.Closing , you're looking for this.Closing or form1.Closing or whatever is appropriate for accessing your instance of your form. Some basic understanding of programming is assumed. Closing is an instance event, so it will be defined for an instance of your form (so access it using this or a variable that refers to your Form ).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
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)
|
|
|
|