Thursday, April 22, 2010

Code4Food #4: How to change WinForms Treeview control image size?

Hi,

Today I visited Bytes.com and saw a question about Treeview.

Indeed how would one change ImageSize of a node ? So I had 15 free minutes and this is what I found.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TreeView.ImageSize
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            FillTreeView();
        }

        private void FillTreeView()
        {
            // Load the images in an ImageList.
            ImageList myImageList = new ImageList();
            myImageList.Images.Add(Image.FromFile("63vts4.jpg"));
            myImageList.Images.Add(Image.FromFile("ToDeleteIfNotSold.png"));
            
            // Change the size of the images.
            myImageList.ImageSize = new Size(50, 50);

            // Assign the ImageList to the TreeView.
            trvImages.ImageList = myImageList;
            trvImages.ItemHeight = 50;

            // Set the TreeView control's default image and selected image indexes.
            trvImages.ImageIndex = 0;
            trvImages.SelectedImageIndex = 1;

            // Create the root tree node.
            TreeNode rootNode = new TreeNode("CustomerList");

            TreeNode childNode1 = new TreeNode("Customer1");
            TreeNode childNode2 = new TreeNode("Customer2");

            // Add a main root tree node.
            trvImages.Nodes.Add(rootNode);
            trvImages.Nodes[0].Nodes.Add(childNode1);
            trvImages.Nodes[0].Nodes.Add(childNode2);
        }
    }
}

So the magic line was myImageList.ImageSize = new Size(50, 50); Here is the output and the code to download.

Download source code link

Thursday, April 1, 2010

.NET 4.0 : System.Tuple

So, let’s go back to our subject, .NET 4.0 new stuff & features.

One of the newest things that we will have in .NET 4.0 is a tuple, what’s right and it’s not a joke!

A tuple is data structure which can contain different types of data coupled. This is what makes it different from a List or other generic types.

Note: Do not confuse it with Microsoft.Dynamics.Framework.Reports.Tuple. It is another type.

So a tuple which will contain to 7 elements is created with a factory. System.Tuple.Create. A tuple with 8 elements will contain 7 elements and another tuple Rest.

Why would we need this  type? Well, this allows us to return two or more values of different types.

Here is the code which i made to play a little bit with this type.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Intro.Tuple
{
    class Program
    {
        static void Main(string[] args)
        {
            var squaresList = System.Tuple.Create(1, 4, 9, 16, 25, 36, 49, 64);

            Console.WriteLine("1st item: {0}", squaresList.Item1);
            Console.WriteLine("4th item: {0}", squaresList.Item4);
            Console.WriteLine("8th item: {0}", squaresList.Rest);
            Console.ReadKey();

            Console.WriteLine(Environment.NewLine);
            
            var tupleWithMoreThan8Elements =
                System.Tuple.Create("is", 2.3, 4.0f,
                new List { 'e', 't', 'h' }, "udevi", new Stack(4), "best", squaresList);

            // we'll sort the list of chars in descending order
            tupleWithMoreThan8Elements.Item4.Sort();
            tupleWithMoreThan8Elements.Item4.Reverse();

            Console.WriteLine("{0} {1} {2} {3}", tupleWithMoreThan8Elements.Item5,
                tupleWithMoreThan8Elements.Item1,
                string.Concat(tupleWithMoreThan8Elements.Item4),
                tupleWithMoreThan8Elements.Item7);

            Console.WriteLine("Rest: {0}", tupleWithMoreThan8Elements.Rest);
            Console.WriteLine("Rest's 2nd element: {0}", tupleWithMoreThan8Elements.Rest.Item1.Item2);
            Console.WriteLine("Rest's 5th element: {0}", tupleWithMoreThan8Elements.Rest.Item1.Item5);
            Console.ReadKey();

            Console.WriteLine(Environment.NewLine);

            var tuple = GetNameAndAge();

            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Name: {0}", tuple.Item1);
            Console.WriteLine("Age: {0}", tuple.Item2);
            Console.ReadKey();
        }

        private static Tuple GetNameAndAge()
        {
            var resultingTuple = System.Tuple.Create(string.Empty, 0);

            Console.WriteLine("Enter your name please: ");
            string name = Console.ReadLine();
            Console.WriteLine("Enter your age please: ");
            string ageValue = Console.ReadLine();

            int age = -1;

            if (int.TryParse(ageValue, out age))
            {
                resultingTuple = new Tuple(name, age);
            }

            return resultingTuple;
        }
    }
}

And here is what in our case the Rest is:

And the output:

Hope it helps you! See you around!

Wednesday, March 31, 2010

Tools I can’t imagine my dev life without #1: Ninite.com

Hi,

Last day of March and I want to start today this new series of posts where I’ll try to share with you some of the gems already there, but I suppose the majority of developers in the world don’t really know about.

So one of these gems is site Ninite.com.

Every time when I need to install a fresh version of Windows somewhere (and as a programmer I do this more than I would like to) and there is an internet connection I use Ninite installer to install all the software that is needed. It does have almost everything there, and the list is growing.

Once you’ve chosen the programs you’d like to install, it will generate an installer for you, and you should save it on your machine and download it. Now after you’ve downloaded it, run it and you will see something like these (it depends on the programs you’ve chosen).

After that, you’ll just need to wait while all of your programs will finish to install, and that’s all!

Awesome!!! Isn’t it?

Ideas, concerns, suggestions, tips & tricks – I’m waiting for all these. Take care.

Friday, March 26, 2010

jQuery - SlideShare

I love and use JQuery, it should be used in each and every single web application. Don't use raw javascript use JQuery instead.

Here is the presentation of Rey Bango jQuery Team Member. It should gave you a little overview on what is JQuery and how to use it.

Have questions, ideas? Post a comment and I will try to answer you.

Friday, March 19, 2010

.NET 4.0 : String.IsNullOrWhiteSpace Method

Hello one more time guys,

So I think the most of you are familiar with string.IsNullOrEmpty method which is very useful when you’re checking your string on actually having the data for different reasons:

  • Sometimes we need to parse the string
  • Add something to the string
  • Use some of the string members
  • AND try to see if the data from the database/xml/file is valid.

But now another problem is around: often the data in database has some whitespaces because there are constraints on some varchar columns NOT NULL. To works these around when something is not available, someone could insert a whitespace, and this would solve the problem, for the moment.

Actually it’s not a lot of fun to do something like

if (!string.IsNullOrEmpty(" ") && " ".Trim() != string.Empty) 

{
            

}

What’s why .NET framework 4.0 has the string.IsNullOrWhiteSpace method.

To compare there 2 methods I did the following program:

using System;
using System.Collections.Generic;

namespace Intro.StringIsNullOrWhiteSpace
{
    class Program
    {
        /// 
        /// The main entry point for the application.
        /// 
        static void Main(string[] args)
        {
            // let's say we have a Dictionary of int - string
            // the string representation of a number. Example: 1 - one
            Dictionary integerDictionary = new Dictionary();
            integerDictionary.Add(1, "one");
            integerDictionary.Add(2, "two");
            integerDictionary.Add(300, "three hundreds");
            integerDictionary.Add(4, "four");
            integerDictionary.Add(67, "sixty seven");

            // let's assume that we don't know anything about the data in the dictionary
            // we also could have something like
            integerDictionary.Add(5000, null);
            integerDictionary.Add(50001, string.Empty);
            integerDictionary.Add(56565, " ");
            integerDictionary.Add(23, " \r\n ");

            // now let's check it with string.IsNullOrEmpty
            Console.WriteLine("string.IsNullOrEmpty");
            Console.WriteLine("===================================");
            foreach (var keyValuePair in integerDictionary)
            {
                if (string.IsNullOrEmpty(keyValuePair.Value))
                {
                    Console.WriteLine(keyValuePair.Value + " - True");
                }
                else
                {
                    Console.WriteLine(keyValuePair.Value + " - False");
                }
            }
            Console.ReadKey();
            Console.WriteLine(Environment.NewLine);

            // let's check it now with the new one : string.IsNullOrWhiteSpace
            Console.WriteLine("string.IsNullOrWhiteSpace");
            Console.WriteLine("===================================");
            foreach (var keyValuePair in integerDictionary)
            {
                if (string.IsNullOrWhiteSpace(keyValuePair.Value))
                {
                    Console.WriteLine(keyValuePair.Value + " - True");
                }
                else
                {
                    Console.WriteLine(keyValuePair.Value + " - False");
                }
            }
            Console.ReadKey();
        }
    }
}

Here is the output of the program:

Have a nice day and  use it in .NET 4.0 !