Archive for March, 2009

How to make a text string fit your labels in .NET

March 9, 2009 - 10:43 pm 4 Comments

In my chosen career, I solve problems everyday. Sometimes the problems I’m given are small and simple to solve, and sometimes they are most definitely not. That, I can deal with. What annoys me is when a problem seems like it should only take five or so minutes to solve and yet somehow, it takes you all day. It’s not that you’re bad at what you do, it’s just that the thing you’re trying to do wasn’t quite as simple as you or anyone else had thought it would be. Or perhaps the framework you’re working with has some nasty little quirks that you weren’t aware of. Or perhaps the thing you’re trying to do just hasn’t been done before, or at least hasn’t been well documented. Whatever the reason, spending all day on a “simple problem” isn’t good for your sanity and makes you feel like you ought to be doing COMP101 all over again.

One such problem that I’ve come across more than once in the last few weeks is how to make a text string fit inside a label or other similar control. If the string is too long to fit, the end should be truncated and replaced by an ellipsis. Sounds pretty straightforward eh? Nope, think again.

After coming up with a few complicated solutions including but not limited to iterating through every character in the string and measuring the length of the string (yes, highly inefficient, I know!), I finally found a solution which seems to do the trick. I feel this amazing solution needs to be shared with everyone so here it is:

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private string textToDraw = "Hello, how are you? Why does this string not fit in the label that I have drawn? Maybe it is because it is too long?";
 
public Form1()
{
    InitializeComponent();
    label1.Text = ShortenString(textToDraw, this.Width - 10, label1.Font);
}
 
public string ShortenString(string myString, int width, Font font)
{
    string result = string.Copy(myString);
    TextRenderer.MeasureText(result, font, new Size(width, 0), TextFormatFlags.EndEllipsis | TextFormatFlags.ModifyString);
 
    return result;
}
 
private void Form1_ResizeEnd(object sender, EventArgs e)
{
    // Set the width to something - I've just set it to the width of the form - 10 to allow for the ellipsis but
    // I guess this would need to depend on which control the label is to be displayed inside of
    int width = this.Width - 10;
    label1.Text = ShortenString(textToDraw, width, label1.Font);
}

The trick to solving this problem was to use the TextRenderer class and calling its MeasureText function. MeasureText doesn’t seem like the most obvious function to use but it seems to work.

Note that this example is for a C# winforms application. The same could be done for a web application although you’d have to use JavaScript to re-calculate the string when the browser or element is resized.

Has anyone else ever had to solve this particular problem? If so, I’m interested to hear how you did it!

  • Share/Bookmark

Traveling with your tech gadgets

March 8, 2009 - 11:54 pm 1 Comment


Tech gadgets
All the gadgets I take with me on holiday… is it too much?



Technology has invaded every aspect of our lives. I dare say that most households these days will have a computer, television, dvd player, and most people carry with them at least a cellphone and an mp3 player. We use technology in our work, our finances, our entertainment and our travel. So with all these choices, what should you carry with you when you travel?

A few years ago I went on a 4 month long trip around the world and I took with me 1 film-based camera, 1 mp3 player and 1 gameboy. Not bad for 4 months I thought. Didn’t take long however before I was robbed and lost the mp3 player and gameboy. Without much choice in the matter, I was left carrying only one ‘electronic’ item, my camera. And guess what? I had a great holiday and didn’t miss any of the other stuff that I thought I would need.

More recently, Chris and I took a 3 week holiday to Canada and between us we took: 3 cameras (+ xtra sd-cards, chargers and cables), 1 laptop, 2 cellphones, 2 ipods (80gig video) and a GPS unit (which we hardly ever used). Hmm. Writing that made me laugh. At myself. You’d think that our bags have nothing but electronics in them. But seriously, at some point whilst packing our bags we figured that we’d need all of that stuff and well, to be honest, we did use it all in one way or another.

So what should you take with you when taking your next holiday? In reality, it all depends on where and when you’re going, how long you’re going for and what you’ll be doing when you’re there (oh and how much of a geek you are). Although there is no definitive answer, I thought I’d share some of the factors that affect my decision making when it comes to picking which gadgets to take with me on holiday.

  • Take only what you really think you won’t be able to live without.
    If you don’t take your cellphone and you really need to make a phone call, you can always use a payphone. Alternatively, you could rent a cellphone. Many airports house shops that will rent you a cellphone for the duration of your stay. Or you could even buy a new cheap phone if you absolutely run out of other options. See? You really don’t need to take yours.
  • Take small, stream-lined versions of all the electronics you think you’re going to need.
    If you can’t stand the idea of not having your own laptop whilst on holiday then make sure you take a small, light netbook with you, not a full-sized desktop replacement brick. You really don’t want to be carrying 5kg of solid expensive gear with you everywhere you go. You’ll end up a stooped old hunchback if you do.
  • Try to detach yourself from caring too much if your stuff does get stolen or damaged.
    This is hard to do, especially if you have some sort of emotional attachment to your gadgets. If it’s irreplaceably special to you or would cost a lot of money to replace, I wouldn’t recommend taking it. Leave it at home, safe and sound. You can’t control how or when some scummy thief out there may take an interest in it and just take it from you. I’m particularly guilty of this and often worry about losing something of value. It just ends up frustrating me that I spend more time worrying about locking away anything of value than I do just relaxing, letting go and enjoying myself.
  • If you have a digital camera and are taking lots of photos, back them up early and often.
    If something were to happen to your camera, you don’t want to lose all your photos as well. The simplest option is to take your camera into a shop that can write your photos to a CD/DVD (you’ll find these all over the world) or if you have another gadget with you, you could back them up there instead. For example, I backed some of my photos up onto my Ipod since it had plenty of free space on it.
  • If you’re traveling with someone else and you’re both taking gadgets that have the same chargers / cables, take only one charger between you.
    Chances are that you’re not going to need to fully charge your gadget at exactly the same time as whoever else you’re traveling with so if you can, share one between you. Chris and I took two sets of Ipod chargers and cables with us on one of our long trips and ended up sending 1 set home to save space.

If you’re a traveler, what do you take with you?

  • Share/Bookmark

Girls in IT

March 1, 2009 - 10:03 pm 5 Comments

Since I was about 5 years old and got my little hands on my first Commodore 64, I’ve been comfortable around computers. At first it was all about the games, and then it was about the BASIC programs that my mum and I wrote after school. Later on it was about mIRC, ICQ and horrible geocities pages. And now computers are my job, hobby and obsession. Computers have been in my life for well over 20 years in one way or another and I totally take them for granted. Oh, and did I mention that I’m a girl?

However, this is not the norm. The norm is that girls don’t play with computers and don’t work in IT. But why? Why are there so few girls in IT?

Well, the answer is that there is no one simple reason. It’s something that Universities and education ministries around the world have struggled with for years. For every 200 guys graduating from Computer Science at University, there is 1 girl. This may not be the same at every University around the world but it’s a pretty sad statistic. The same is true in the IT industry. Basically, girls are under-represented in IT almost everywhere you look.

Anyway, I thought I would list a few of the factors that I believe strongly contribute to why girls end up in IT:

  • Programming is not taught in all-girls high schools
    This is obviously a pretty big reason why girls don’t take up computer science at University. I actually experienced this myself when I was told in no uncertain terms that “girls don’t program” after asking what programming languages we’d be learning in our Computer Studies course in an all girls’ college I went to in Wellington, New Zealand. The result of this is that any girls who may be interested in trying Computer Science or Computer Engineering at University are already starting off at a disadvantage, especially compared to boys who apart from being curious and probably having played computer games and scripted for years already, may well have been taught some form of programming in high school, simply because they are boys.
  • Barbies versus binary
    Parents continue to buy their daughters dolls while their sons get computer games and game consoles. I don’t have children but I have friends who do and I have seen this happen time and time again. If children that young are seeing and being made part of this divide, how can we really expect it to be any different when those same children are trying to decide which career to follow?
  • Working with computers is seen to be nerdy and uncool
    There is quite a strong stereotype around geeky computer programmers and it could be possible that a lot of girls, in their formative years, don’t feel that it would be cool to be associated with that sort of image. Peer pressure is a huge motivator for many children and adults, and if that peer pressure is telling girls not to spend time on computers but rather to spend that time reading fashion magazines then that is what they will do.
  • Could it be that our brains are just wired different?
    I’m no psychologist or doctor so I don’t have the answer to this one. But obviously males and females are different in many other ways so it only follows that our brains probably work differently as well. However, I do believe that this is the last string in the list of reasons why girls aren’t in IT and should most definitely not be used as an excuse for it. As pointed out above, I think there are other reasons why girls don’t pursue IT careers that need to be looked at carefully before we fall back onto a reason that we can’t really prove or do anything about.

Having done a 4 year degree in Computer Science and now having worked in IT for more than 5 years and having quite often been the only girl in my team, I would also like to dispel some of the myths about working in IT.
  • You don’t have to be a social outcast to be a girl software developer
    There is a stereotype surrounding girls in IT – usually very geeky looking, perhaps overweight, no friends, bad skin, etc etc. This is not true and most of the girls I know in IT are very much the opposite – smart, intelligent and strong women. It doesn’t matter what you look like or how you dress; if you want to be a programmer, go for it.
  • All guys working in IT are geeky and no fun to hang out with
    Again, this is complete rubbish. Guys who work in IT are for the most part smart, funny, kind and caring. I’ve been friends with geeky guys all my life and in my experience, they’ve been way cooler to hang out with than the mindless jocks who don’t even know how to turn a computer on.
  • As a girl in IT, you’ll never be as good as the guys you work with
    Not true. If you’re good at what you do, people will realize that and treat you accordingly. Just make sure you keep up to date with the technologies that you use in your job and don’t be afraid to speak up if you’ve got some ideas you want to share. The guys in your team will come to trust and respect you and trust me, that’s a great feeling.

So if you’re a girl and you’re thinking about getting into IT, here is my advice:
  • If you have some geeky friends, ask them for help or advice.
    Asking people who already work in IT what it’s like for them may help you get a better idea of what it might be like for you. Ask them if they can show you some of the work they do, or help you get started with a small project of your own.
  • Set up a blog or a small website for yourself.
    Creating a blog for yourself is a great way to start learning about IT. There are plenty of free blogging engines out there like Blogger, WordPress or Textpattern. This is a great introduction into some common but very useful technologies (HTML, JavaScript, CSS) and you’ll end up with a cool blog that you can use to express yourself online. Pretty cool huh?
  • Keep up with some technologies that interest you by reading online blogs and articles.
    Whether you’re interested in web programming, desktop application development or linux scripting, there are many resources out there for you to keep an eye on. It’s always handy to be able to tell interviewers that you actively follow some geeky site – it shows that you’re truly interested in IT. Some good sites to check out are Coding Horror, Joel on Software, MSDN Blogs, however, there are many many more out there.
  • Go get yourself some IT qualifications.
    If possible, go to University and do a Computer Science degree. However, if you can’t or don’t want to do that just yet, do a short course with a polytechnic or some equivalent. A qualification will not only give you the introduction you’ll need to start off in IT, but it will prove to those who interview you that you’re serious about what you want to do and that you’ve got the smarts to pull it off.
  • Don’t let the lack of women in IT put you off.
    For me personally, being in IT has been a lot of fun. I’ve met heaps of really great people and have fit right in with all the development teams I’ve been on. IT guys will treat you like one of them and you’ll never feel left out.

And finally, if you have any questions or want any advice on any of the above then please post a comment on this blog and I’ll be more than happy to have a chat.

  • Share/Bookmark