Posts

Es werden Posts vom Mai, 2009 angezeigt.

Hosting any control in a ToolStrip

When using ToolStrips, you soon discover that not many Classes of items can be added regularly. There is a simple way of adding any kind of Control to a ToolStrip by using the ToolStripControlHost class: ToolStrip ts = new ToolStrip(); ts.Items.Add( new ToolStripControlHost( new DateTimePicker())); However, there is NO design-time support for this. jfo has described how to add design-time support on his blog . I have tried this with a DateTimePicker Control and experienced strange Designer behaviour (The Designer show DTP as a permitted Item Type but when I select it, it disappears immediately. Another unexpected behaviour occurs in case of some third-party controls. If, for example, you add a Janus CalendarCombo to a ToolStripControlHost, the control vanishes and the ControlHost fails to show. Effectively, you've just lost a control :) After some research on the Janus Website, I found out that you have to set the hosting element's AutoSize Property to False. ToolStrip ts = new

Get the Date-String (and only that) from a DateTime

Up to now, whenever I tried to get today's date in C#, I used DateTime.Today and started chopping away the time information using substrings. However, the DateTime type offers a method ToShortDateString() that does exactly what I did in three lines before PLUS it is hooked up with the thread's current culture. // old version, works for de-DE only DateTime.Now.ToString(new CultureInfo("de-DE")).Substring( 0, 10 ); // new version, culture safe DateTime.Now.ToShortDateString(); More useful DateTime stuff can be found here .

Using distinct members of anonymous object-type instances

Sometimes it may happen that you have to set property values on objects depending on whether they exist or not. I though this was feasible using interfaces, but objects can only be checked against contracted interfaces (i.e. such interfaces that occur in the object's class hierarchy), so this is no good here. An easy way to achieve this is the type.GetProperty() and type.GetProperty().SetValue() methods, as explained below. PropertyInfo pi = obj.GetType().GetProperty("Enabled"); bool enablable = (pi != null && pi.PropertyType.Equals(typeof(bool))); if (enablable) { obj.GetType().GetProperty( "Enabled" ).SetValue( obj, true, null ); } The very same thing can also be done with events, methods, fields, nested classes etc. See the following example for an event handler implementation: EventInfo ei = obj.GetType().GetEvent("Paint"); bool paintable = (ei != null); if (paintable) { PaintEventHandler pe = _Paint; obj.GetType().GetEvent("P

Microsoft Exam 70-505 Self-Paced Training Kit

Bild
To complete my MCTS, I currently study for 70-505. As I said about 70-536, the SPT is the best way to learn. Here comes the but: The mock exam is so faulty that you have to be very careful using it.I will update this post once Microsoft has put up a knowledge base site for this book and my errors have been submitted. UPDATE : And here it is! Edit : After having taken the exam yesterday, I can tell for sure that using only the Self Paced Training Kit may result in a somewhat close call. Sure it helped me get through, but not as good as 70-536 (Score : 828). I think the Microsoft Official Classroom Course may have been of some use, eventually...Anyway, now I have the right to officially use these credentials: If that ain't hot...