Wednesday, May 13, 2009

C# Convert string to an array of bytes

How to convert a string or a string array into a double array?
The .NET does not provide any method for us to call directly(if you found it, please let me know :D). However this is easy to implement within just a couple lines of code.

//Convert 1 string to an array of bytes
public static byte[] ConvertToBytes(string str)
byte[] ans = new byte[str.Length];

for (int i = 0; i < ans.Length; i++)
ans[i] = Convert.ToByte(str[i]);

return ans;

Tuesday, May 5, 2009

How to disable a TabPage in the TabControl


The component TabControl in the Visual Studio IDE does not allow people to
disable a certain TabPage within the TabControl. I have Googled for a long time
and there is not any real solution regarding this issue. However, some people put a
customized TabControl which allows you to disable the TabPage. I believe not
everyone wants to download and try the third party software or plug-in.

I'm going to talk about a 'solution' which is more like a trick to solve this problem.
For example, let's create a TabControl and it has 2 TabPages. Now before
dragging any other objects from the tool list, drag a Panel object and drop into
each one of those TabPages. Set those three Panels' Dock property to Fill so
that the panel will be maximized within its TabPage. Now you will probably guess
what I'm trying to tell you. Yes, you put all other objects on top of the panel.
Then you just disable the panel if you want to disable a TabPage. Because the
Panel is the Parent of the other components, the panel and its children will be
disabled if we try to disable the panel itself.

Let's have a look at how this works.

First, create a TabControl and drop a Panel onto one of the tab pages.

Now, set the Dock to Fill for the panel

Add some components to the tabPage1. Actually they are on top of the
panel1. Run the sample application without checking the 'Enable Panel1'

The code implementation for the checkbox. Just for testing.

The effect shows the tabPage1 is 'disabled' once the checkbox is unchecked.

This solution might not be perfect to everyone, but I believe it will help some
people like me to solve a problem by using a simple solution. Remember the KISS
principle? This is all about it!

Sunday, May 3, 2009

Windows 7's Hardware Requirement


The hardware requirements on different Windows Operating Systems

The Windows 7 RC version has been released on Apr 30, 2009 for Technet subscribers only. On May 5 everyone should be able to get it for free. Here is the hardware requirement for installing Windows 7 RC on your computer. We can see that it is about the same as Windows Vista, but Windows 7 is 3 years later than Windows Vista.

  Windows 7 Hardware Requirements:

  ·CPU 1GHz(32 bits OR 64 bits)。

  ·1GB Memory(32 Bits);2GB(64 Bits)。

  ·16GB Hard Drive(32 Bits);20GB Hard Drive(64 Bits)。

  ·Support WDDM 1.0 or higher DirectX 9

Friday, May 1, 2009

Convert File Size To Readable Format

When we use "Windows + E" to browse files, we will see the file size of each file under the "Details" mode. Every file has a file size, but by default all file sizes are displayed in one unit 'KB'. This makes people difficult to make sense if the file size is greater than 10MB, because you will see a lot of numeric digits before the dot symbol.

The following table is not magic and the issue mentioned above is not hard to solve. Here is a way to convert the file size into human readable format under normal circumstance.

1 KB = 1024 Byte = 1024 Byte
1 MB = 1024 KB = 1024 * 1024 Byte = 1048576 Byte
1 GB = 1024 MB = 1024 * 1024 KB = 1024 * 1024 * 1024 Byte = 1073741824 Byte

To convert the file size you retrieve from the original "File.Length" method, we can use the above table to check the byte count.


As you see, after calling the "GetFileSize" method, it will return a converted format of file size string. The implementation is only a few if statements.
You can copy & modify the above code for your convenience.



Copyright 2009 All Rights Reserved Revolution Two Lifestyle theme by Brian Gardner | Blogger template converted & enhanced by eBlog Templates