Search...

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

9 June 2011

C# Closing A Form In The Constructor

I had an odd need to close an application if a user failed to set some basic user data in a C# application tonight. The flow was that a very simple dialog was displayed during the main windows constructor call and if they dismissed the dialog the application was to end.

After some head scratching I found the only way to do this was with ...

this.BeginInvoke(new MethodInvoker(this.Close));

22 October 2009

Creating a readonly Checkbox control

I had to make an ASP.Net checkbox readonly today and found that adding the following :

w.AddAttribute("onClick", string.Format("javascript:{0}.checked = {1};", this.ID, this.Checked.ToString().ToLower()));

to the overridden Render() method of a control that inherits a standard Checkbox control did the trick. The call must be place before the call to the parent classes Render() method.

2 September 2009

NDesk.Options C# Command Args Parser.

I have a small application that I am working on that can receive args passed it's Main() method. While looking for some nicer code than I could conjurer up I found NDesk Options a C# based parser written by Jonathan Pryor which makes parsing args a very easy task.

It has a Git repository which I cloned which includes documentation. It seems a simple but powerful solution to a boring problem.

27 March 2009

C# TextInfo Class, ToTitleCase() Method.

As someone who spent a number of years working with VB6 I still miss a built in function for turning a string to Proper case. string has the ToUpper() and ToLower() static methods which work as you expect but no ToTitleCase or ToProperCase function. (Yes I know that if you are compiling against .Net3.5 you can extend the string type but I am still compiling against .Net2.0 for this project).

So I was looking at the best way to proper case a string today and I found this Microsoft help article. The article points to the TextInfo class and it's ToTitleCase() instance method.

Although you have to go through hoops to get an instance of the TextInfo class (you need to provide details of the culture etc) you can then pass the ToTitleCase() method a string and use the result.

Only what seems to be undocumented is the fact that the string provided to the method has to be lower case. It seems to work on the assumption that it only needs to make the first letter in the word upper case and ignore the remaining characters. Therefore, if you provide an upper case word you get an upper case word back.

I may be alone but I think that is a bit lazy. Whoever, implemented that function should be checking the case of the parameter. I mean to make the function fool proof parameter.ToLower() as the first line of the function would do the trick.

1 December 2008

.Net Get Assembly Build Date.

Being of more of a (u)/(l)inux mindset these days I like to include some basic info on apps that I write, even simple 4 line command line applications. In C# I have been trying to create a simple library of functions that I tend to use over and over in command line apps and I have just added a nice little method to get the applications build date using reflection.

I would love to own up to writing the code but honors must go to this post on Coding Horror.

18 September 2008

C# Label transparency

I am writing a small configuration editor for work at the moment and have come to make the initial release for the other guys to test. As the tool could end up in the hands of our partners I thought I would add a decent looking splash screen and about box.

Creating a graphic was the easy part. I left room for a label to be added on the splash window to display the application version. This was an approach I took when working with VB6/VC++ many years ago so I could pass the version info in the page load event. The only trouble is that C# label controls do not support full transparency.

I did a bit of searching and reading of docs. It turns out that turning the .BackColor property to Transparent will only set the back ground colour to the colour of the containing control. Hence if like me you have a picture box and a label it will show the back ground colour of the picture box control, grey (or the default control colour).

Therefore, the following (where pb is a picture box control and l is a label) will do the job. The code should be added to the splash window class.

Private void pb_Paint(object sender, PaintEventArgs e)
{
l.Visible = false;
e.Graphics.DrawString(l.Text, l.Font, new SolidBrush(l.ForeColor), l.Left - pb.Left, l.Top - pb.Top);
}