I was fooling around with Panels and Forms yesterday because I was checking out GUIs with .NET. Being one used to Java, I was glad that .NET came along with some good GUI frameworks since, even until now, Java, for me still could not conquer client side GUIs. Creating a stand alone application in .NET using C# and using Visual Studio 2005 as its editor is fairly easy. Just drag and drop controls from the toolkit to the form’s design area.

The problem came when I wanted to use Panels because I wanted to have sub containers within the Form so I could switch between Panels. I kept surfing for more than 3 hours but I still could not find out why when I drag controls to the design area of a Panel, instead of, say a button, a button’s icon will be displayed instead together with the word button instead of a button that looks like in HTML forms.

Turns out, a UserControl control is what you need. Totally wasted quite some valuable time looking for that and it was in a certain forum that mentioned UserControl that I thought I could check it out. To create a UserControl class, right click on the project name in Visual Studio 2005 in the solutions explorer (right side). Choose Add Item > UserControl. Then in the design area mode, you can drag and drop controls from the toolkit to there. I hope this can help newbies who are baffled why Panels do not work as containers like Forms do. In a way, they can be considered containers but for me, they really are just for grouping sets of controls. Kind of like the group boxes.

There comes a time when we need a visual display of how many seconds are left till the popup window closes. This short code does just that. It displays how many seconds are left and closes the popup window when the number of seconds is zero.

The HTML code would look like this

The Javascript code uses the functions setTimeout and clearTimeout respectively. Take note that window.clearTimeout() must be included in order for this to work. Otherwise, when the page loads, it closes it instantly. I read about what clearTimeout() is for, but I could not grasp how that is of use to the code to make the countdown timer work.

Once the page finishes loading due to the body tag’s onLoad event, the timer calls the timer() function and starts the countdown.

The code above closes the window in 5 seconds. If you need it for a few seconds more, just add 1 to the number of seconds till it reaches zero to the variable seconds.

Even if you know Object Oriented Programming in certain languages like Java, learning C# is totally different. One of the features that varies greatly in C# from Java is passing values by reference as parameters of methods. In Java, data types are automatically passed by value while classes are passed by reference. Good thing I decided to read out a beginner’s book for C#.

In C#, if you want to pass objects by reference, you would have to use the keyword ref. Take for example the sample code below

If you would call the method change() like this

the output for the object str would be haha instead of hoho. You would have to use the ref keyword for this to work

Data types are a different story. To do the same way to data types as parameters in methods, you would use the out keyword like this

Calling the change method

will output 10 as the value. If you do not use the out keyword, the value of i will still be 1.

Related Posts Plugin for WordPress, Blogger...