Saturday, December 8, 2007
ASP.NET C# - Enter Key
ASP.NET 2.0 makes this problems easier and introduce a concept of a "default button". New defaultbutton attribute can be used with <form> or <asp:panel> control. What button will be "clicked" depends of where acutally cursor is and what button is choosen as a default button for form or a panel.
Here is sample HTML code that contains one form and one panel control:
<form defaultbutton="button1" runat="server">
<asp:textbox id="textbox1" runat="server"/>
<asp:textbox id="textbox2" runat="server"/>
<asp:button id="button1" text="Button1" runat="server"/>
<asp:panel defaultbutton="button2" runat="server">
<asp:textbox id="textbox3" runat="server"/>
<asp:button id="button2" runat="server"/>
</asp:panel>
</form>
Saturday, December 1, 2007
Linux: Tips for new Ubuntu installation
Installing libdvdcss2 and w32 video codecs in Ubuntu Gutsy Gibbon
Support for WMV, RealMedia and other formats has been bundled into the w32codecs package. This package is not available from the Ubuntu repositories due to licensing and legal restrictions.
For Ubuntu Gutsy Gibbon Users run the following command
sudo wget http://www.medibuntu.org/sources.list.d/gutsy.list -O /etc/apt/sources.list.d/medibuntu.list
Now you need to copy the key using the following command
wget -q http://packages.medibuntu.org/medibuntu-key.gpg -O-¦ sudo apt-key add -
Update the source list using the following command
sudo apt-get update
Install Codecs using the following command
sudo apt-get install w32codecs libdvdcss2
Using above download locations you can install most of the mutimedia codecs for ubuntu.
sudo apt-get update
Mplayer Plugin for Firefox
If you want to install Mplayer with plug-in for Mozilla Firefox run the following command
sudo apt-get install mozilla-mplayer
Wednesday, November 28, 2007
Tuesday, November 20, 2007
Linux: Find files on your computer with find
A standard Linux system has an incredible amount of files installed. Looking for a file location can be a painful task to do though a file browser.
Fortunately, there is a nifty command line available by default on any Linux distribution: find.
find can virtually find anything on your computer and comes with a lot of options. This tutorial will introduce a basic use of it and show how you can search your filesystem for file names matching a name pattern.
On Debian based distros, find is part of the package findutils. find allow one to search for files on a filesystem based on different condition, creation date, modified date, file size, file type, permissions, name ....
In this tutorial, I will be focused on finding files/directories based on their name, in order to explain in more depth the syntax of find, I will also show how you can narrow down your search by adding condition on size and file modification time.
This will suit most searches, if you need more details, I would recommend looking at the find's manpage.
1. Find basis
The default syntax of find is as such:
find [path] [expression]
where path is the path used as root for searching pattern and expression the expression we want the file to match.
2. Finding a file based on filename
Let say for instance you want to find all .avi files in users home directories. Search files can be found with the following command:
# find /home -name '*.avi'
If you want to search for *.mpg and *.avi files, you will use the following:
find /home -name '*.mpg' -o -name '*.avi'
Case insensitive searches can be achieved by using the -iname switch:
find /home -iname '*.mpg' -o -iname '*.avi'
3. Adding some more criterias
Those kind of searches might returns far too many results, making it hard to find waht you were looking for in the first place.
Fortunately, you can narrow down the search by adding criteria such as the file size and the file modification date.
Let'search for .avi files bigger than 700M. This can be done with:
find /home/ -name '*.avi' -a -size +700M
Now, let's find the same subset of files that were modified less than 15 days ago:
find /home/ -name '*.avi' -a -size +700M -mtime -15
4. Adding some actions
Grand, we can now find files based on a subset of criteria. What
would be even better is to apply some actions on those files. Action
can be done with the use of -exec switch.
We can now find .avi file that are newer that 15 days, in this example,
we are going to move those file to another location: /my/new/movies . I
consider that this directory already exist on your system.
Moving .avi files bigger than 700M and younger than 15 days to /my/new/movies can be done with:
find /home/ -name '*.avi' -a -size +700M -mtime -15 -exec mv '{}' /my/new/movies/ \;
Mind the use of '{}' and \; (there is a space before \;).
'{}' matches the file that was found, while \; terminate the exec statement.
5. Conclusion
find is a powerful tool with an extensive set of statement. This article only covered a small subset of available features. For more information on the find command I recommend checking out its man page.
Powered by ScribeFire.
Saturday, November 10, 2007
Single Instance Application
Making an application single instance, can be achieved by using a mutex (Mutual Exclusion Semaphore).
A Windows application loads the main form through the Application.Run( ) method. In the Main method, create a new mutex. If a new mutex is created the application is allowed to run. If the mutex has already been created, the application cannot start. This will ensure that only one instance will be running at any time.
// Used to check if we can create a new mutex
bool newMutexCreated = false;
// The name of the mutex is to be prefixed with Local\ to make
// sure that its is created in the per-session namespace,
// not in the global namespace.
string mutexName = "Local\\" +
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
Mutex mutex = null;
try
{
// Create a new mutex object with a unique name
mutex = new Mutex(false, mutexName, out newMutexCreated);
}
catch(Exception ex)
{
MessageBox.Show (ex.Message+"\n\n"+ex.StackTrace+
"\n\n"+"Application Exiting...","Exception thrown");
Application.Exit ();
}
// When the mutex is created for the first time
// we run the program since it is the first instance.
if(newMutexCreated)
{
Application.Run(new AnimatedWindowForm());
} When a new mutex is created the mutex name can be prefixed with either Global\ or Local\. Prefixing with Global\ means the mutex is effective in the global namespace.
Prefixing with Local\ means the mutex is effective in the user's session namespace only.
Windows XP and Windows 2003 allow fast user switching through Terminal Services Sessions. So if a mutex is created with a Global\ prefix, the application can have only one instance system wide. So if one user launches the application, other users cannot create a second instance in their sessions. If the mutex is not prefixed with Local\ it is effective per session only.
Placing Your C# Application in the System Tray
- To get started, open an existing C# Windows form (or create a new one).
- Open the Visual Studio Toolbox.
- Drag a NotifyIcon control onto the form. The control will named notifyIcon1 by default and placed below the form because it has no visual representation on the form itself.
- Set the NotifyIcon control's Text property to the name you want to appear when the user pauses the mouse over the application's icon. For example, this value could be "KillerApp 1.0".
- Set the control's Icon property to the icon that you want to appear in the System Tray
- Add an event handler for the form's Resize event that will hide the application when it's minimized. That way, it won't appear on the task bar.
- Add an event handler for the NotifyIcon.DoubleClick event and code it as follows so that the application will be restored when the icon is double-clicked.
private void Form1_Resize(object sender, System.EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
Hide();
}
private void notifyIcon1_DoubleClick(object sender,
System.EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
At this point, your application will fuction perfectly in terms of an icon appearing in the System Tray when the application is run (see Figure 1), the application not appearing on the task bar when minimized and the application restoring itself when the Tray icon is double-clicked.
Figure 1
Now, let's see the steps involved with adding a context menu to the icon.
- From the Visual Studio Toolbox, drag a ContextMenu control onto the form.
- Right-click the ContextMenu control and select the Edit Menu.option.
- Type in the options that you want to appear in your context menu. For example, you can add options such as Restore and Close Application.
- As with any menu, double-click the menu item to create and code each item's handler. As an example, you could copy the code from the form's DoubleClick handler into the context menu's Restore handler and for the Close Application menu item; simply call the form's Close method.
- Finally, set the NotifyIcon control's ContextMenu property to the new context menu you just created by selecting the menu from the drop-down list. Figure 2 shows a simple Tray context menu.
Friday, October 26, 2007
Friday, October 12, 2007
חידות לראיון עבודה
חידות בראיון עבודה הן משהו שקשה להתחמק ממנו אבל די קל לעבור אותן בעיקר כשערן רום מביא לכם את התשובות, אז קבלו את חלק א' של החידות והפתרונות העיוור והכדים
חידה - מול עיוור מונחים שני כדים, ברשותך 50 כדורים שחורים ו-50 כדורים אדומים.
עליך לחלק את הכדורים בצורה כזו שלעיוור יהיה הסיכוי הגבוה ביותר להוציא כדור אדום.
איך תעשה זאת?
פתרון - יש לשים כדור אדום אחד בכד מסוים ובכד השני את כל שאר הכדורים, כך נגיע לסיכוי של בערך 75%.
הסבר: בכד עם הכדור הבודד הסיכוי הוא מאה אחוז ובכד השני קצת פחות מחמישים אחוז.
שלוש קערות
חידה - יש לנו 3 קערות שבהן ישנן גולות שחורות, גולות לבנות או מעורב, גולות שחורות וגולות לבנות.
על כל קערה רשום פתק:
שחור, לבן או מעורב (שחור ולבן)
ידוע ששלושת הפתקים שקריים.
תוך כמה הוצאות של כדורים תדעו מה הפתק המתאים לכל קערה?
פתרון - הוצאה אחת תספיק.
הסבר: מאחר ונתון שכל הפתקים לא נמצאים על הקערה הנכונה די להוציא כדור אחד מהקערה המעורבת, בהתאם לצבע של הכדור שייצא נדביק מבדקה שחור או לבן ואת הפתקים האחרים פשוט נחליף. זה הסידור הנכון.
החבית הכבדה
חידה - יש 9 חביות ואחת כבדה יותר מהשאר. כיצד תאתרו את החבית הכבדה ביותר בעזרת שתי שקילות בלבד?
פתרון - שוקלים שתי קבוצות של 3 חביות, אם קבוצה אחת יותר כבדה אז שוקלים שם חבית אחת מול חבית אחת.
במצב של שיוויון בשקילה הראשונה ממשיכים עם שלושת החביות שלא שקלנו.
אם יש שיוויון בשקילה השנייה אזי החבית שנותרה בחוץ בשקילה זו היא החבית המבוקשת (הכבדה).
זמן ופתילים
חידה - אנו רוצים למדוד זמן בעזרת פתילים. לרשותנו שני פתילים אשר זמן הבעירה של כל אחד מהם הוא שעה. קצב הבעירה אינו קבוע (כלומר חצי מאורך הפתיל לא יבער חצי מזמן בערת הפתיל). כיצד תמדדו שלושת רבעי שעה?
פתרון - מדליקים פתיל אחד משני הצדדים ופתיל אחד רק מצד אחד, כשהפתיל הראשון מסיים מדליקים את הצד השני של הפתיל השני.
הסבר: אחרי שהדלקנו את הפתיל הראשון משני צדדיו תעבור חצי שעה. אז אם נדליק את הצד השני של הפתיל השני נקבל רבע שעה, סך הכל שלושת רבעי שעה.
כובעים בקופסה
חידה - בקופסה יש חמישה כובעים: שלושה שחורים ושניים לבנים. בוחרים שלושה באקראי ומעמידים בטור שלושה אנשים כאשר האחרון בטור רואה את הכובעים של אלה לפניו, אך לא את שלו. האמצעי רואה את של הראשון והראשון לא רואה אף כובע.
האחרון אומר:אני לא יודע איזה כובע יש לי.
האמצעי אומר:אני לא יודע איזה כובע יש לי.
הראשון אומר: אני יודע איזה כובע יש לי. איזה כובע יש לראשון?
פתרון - לראשון יש בהכרח כובע שחור.
הסבר: אם האחרון לא יודע איזה כובע יש לו מכאן שלשני הראשונים יש או שני כובעים שחורים או כובע שחור אחד וכובע לבן אחד.
מאחר וגם האמצעי לא יודע איזה כובע יש לו (ואם לראשון היה לבן הוא הרי היה יודע שיש לו שחור), נקבל שלראשון יש כובע שחור
Saturday, October 6, 2007
What is ctfmon.exe And Why Is It Running?
You are no doubt reading this article because you are frustrated with the ctfmon.exe process that just won't stop opening no matter what you do. You remove it from the startup items and it just magically reappears. So what is it?
Ctfmon is the Microsoft process that controls Alternative User Input and the Office Language bar. It's how you can control the computer via speech or a pen tablet, or using the onscreen keyboard inputs for asian languages.
If you are using any of the above, you should leave it enabled. For everybody else, we'll get to the job of disabling this annoying service.
Depending on your system configuration, there are a number of different steps to disable it. I've tried to list all the methods below.
Step 1: Disabling in Microsoft Office 2003
We can remove the alternative text input from Microsoft Office 2003 by just removing that feature in the setup.
Note: I haven't figured out where the equivalent setting is for Office 2007 (if there is one), but we can also disable it a different way below.
Go to Add/Remove programs, choose to Change your installation of Microsoft Office and make sure you check the box for "Choose advanced customization of applications" before you hit next.
Find "Alternative User Input" in the list and change the dropdown to "Not available" so it looks like this:
Step 2a: Disabling in Windows XP
There's an additional step we can take to make sure it gets turned off in Windows XP, which really seems to be the best answer for XP users.
Open up Control Panel and choose Regional and Language Options.
Choose the Languages tab and then click on Details in the top section.
Now on the Advanced tab you can choose to "Turn off advanced text services", which should immediately close ctfmon.
You'll also want to take a look at the first Settings tab, and make sure that your "Installed Services" box looks similar to this one:
If you have more than one Installed service then ctfmon might come back… For instance on my system there was an input for my drawing tablet so I could use it as a text input… which I don't care about, so I clicked Remove on it.
Step 2b: Disabling in Windows Vista
The setting above for completely disabling text services doesn't seem to exist in Windows Vista as far as I can tell, but we can remove the additional input services using a similar method.
Open Control Panel, choose Regional and Language Options and then find "Change keyboards or other input methods".
On the Keyboards and Languages tab, you can select Change keyboards.
Now you'll finally be at the same screen as in Windows XP. You'll again want to remove the extra installed services in the list other than your default keyboard language.
Step 3: Remove From Startup
You won't want to perform this step before doing the others, because it will just be overwritten again. Open up msconfig.exe through the start menu run or search box, and then find the Startup tab.
Find ctfmon in the list and disable it by unchecking the box. Just remember that if you haven't disabled ctfmon through one of the other settings this won't help you a lot.
Step 4: If all else fails
You can just completely unregister the dlls that run the alternative input services by running these two commands from the run box (one at a time)
Regsvr32.exe /u msimtf.dll
Regsvr32.exe /u msctf.dll
If you perform this step, you should also use Step 3 to get rid of the startup entries.
Step 5: Reboot
Reboot your computer and then open a Microsoft Office application if you do have that installed. Verify that ctfmon.exe is not running.
For more information you can read the Microsoft article on the subject.