Category Archives: Visual Studio

Hyperlink or Linklabel in Silverlight

Silverlight 3 does not have a control similar to link label. This post tells you how you can add a hyperlink or linklabel in Silverlight 3.

Expression Blend:

1. Create a Silverlight project.

2. Add a Text Block object. Change the text to suit your requirement, let’s say My Website.

3. Under Properties->Brush tab, change the color to R=0, G=0, B=204.

4. Under Properties->Text tab, click on the Underline button. This will add underline to the text.

5. Under Properties->Common Properties, change the Cursor to Hand. (Choose from the drop down list)

6. Under Events tab, double click on MouseLeftButtonUp to add a event handler. In the code-behind, you can open the desired website.

Modifying XAML:

1. Create a Silverlight project

2. In MainPage.xaml, add the following lines

<TextBlock Text="My Website" TextWrapping="Wrap" TextDecorations="Underline" Foreground="#FF0000CC" Cursor="Hand" MouseLeftButtonUp="TextBlock_MouseLeftButtonUp"/>

In the event handler, add the code to open your website.

Tagged , , , , , ,

Calling a Javascript function from Silverlight

This article talks about how you can call a javascript function from Silverlight.

1. Create a Silverlight project of type ‘Silverlight Application’ in Visual Studio. You will get a dialog box asking you whether you want to host your application on a website. Check this checkbox and click on OK.

2. Open MainPage.xaml and replace the existing content with this:

<UserControl x:Class="Blog1.MainPage"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
 <Grid x:Name="LayoutRoot">

 <Button x:Name="testButton" Content="Call js function" Click="testButton_Click"
 Width="150" Height="40" />

 </Grid>
</UserControl>

3. In MainPage.xaml.cs, a method testButton_Click should have been created. Replace the method with the one below. If the method is not present already, add it yourself.

 private void testButton_Click(object sender, RoutedEventArgs e)
 {
 HtmlPage.Window.CreateInstance("calledFromSilverlight", new string[] { "Silverlight User" });
 }

4. Save all the files and build your solution.

5. Go to the Bin folder where the xap file is located. Normally under <project>/Bin/Debug. There should be a file named TestPage.html. Open this file in your Visual Studio and add the following function.

 function calledFromSilverlight(user) {
 alert("Hello " + user);
 }

6. Open this page in a browser. You should see a button with text ‘Call js function’. Click on this and if everything is fine, you should see a pop-up window saying ‘Hello Silverlight User’.

That’s it. You just invoked a javascript function from Silverlight. Wasn’t that easy?

Note:

  1. If you rebuild the solution, the TestPage.html is created again, removing all the changes that you have made. You can make a copy of this file, name it MyTestPage.html and add the javascript function in this.
  2. This works for Silverlight 3 and Visual Studio 2008.

Next up: Calling Silverlight method from Javascript.

Visual Studio: Restore Missing Templates

Symptom: You cannot add a new project or an item in Visual Stuio IDE.

Problem: Open the Event Viewer (Control Panel->Administrative Tools->Event Viewer) and see if the suggestion says “Run devenv.exe /installvstemplates”. If this is the case, then the problem is the template files are missing.

Solution:

1. Close all instances of Microsoft Visual Studio.

2. There are two folders under IDE folder (C:\Program Files\Microsoft Visual Studio xx\Common7\IDE), namely ProjectsTemplateCache and ItemsTemplateCache. Delete these two folders. Yes, I said, delete. If you are hesitant, don’t worry, it will go to the recycle bin from where you can retrieve them.

3. After deleting the folders, open the command prompt and run this command: Devenv /InstallVSTemplates .

4. Done. The template files should be restored and you should be able to add new projects and items.

I found myself in this situation out of nowhere. I was innocently coding and debugging and when I tried adding a control to my project, I suddenly see this error message. Thank God, the solution was this simple. Imagine spending another 4 hours on reinstalling Visual Studio. I wonder why I got into this situation, in the first place. Any idea?

Tagged , ,

Using App.Config for user defined runtime parameters

These are the steps involved in defining runtime parameters and using them in your code.

1. Create a App.Config file
2. Define the runtime parameters and provide values for these
3. Access these parameters in your code
4. Verify changing the parameter value at runtime affects the application


Step 1: Create App.Config file

1a. Create a Console Application
1b. In Solution Explorer, right click on project. Add->New Item. Select Application Configuration file. This file will be added to your project.


Step 2: Define the runtime parameters and provide values for these

2a. Open App.Config.
2b. Define the keys and values for your runtime parameters.

Example:

<configuration>
<appSettings>
<add key="OperatorName" value="Rita"></add>
<add key="LoggerLevel" value="5"></add>
</appSettings>
</configuration>

You can define any key and provide values. Ensure you don’t have duplicate keys.


Step 3: Access these parameters in your code

3a. Add a refernce System.Configuration DLL.1
3b. Add the following lines in your code. You can directly add these lines in Main.

string name = System.Configuration.ConfigurationManager.AppSettings["OperatorName"];
Console.Writeline("Welcome " + name);
string level = System.Configuration.ConfigurationManager.AppSettings["LoggerLevel"];

Console.Writeline("Logger level: " + level);

3c. Build your project and execute it.The output should be:

Welcome Rita
Logger level: 5


Step 4: Verify changing the parameter value at runtime affects the application

4a. When you build your project, the App.Config file is copied to your Debug/Release folder with the name .exe.config. My project is named UseAppConfig. The App.Config in my Debug folder is UseAppConfig.exe.config

Open the .config file in your working directory (Debug/Release). Change the values to Mary and 1. In the same folder you will see your executbale. Double click on this and you should see the new values.2

You are all set. You can identify all those parameters that make sense as runtime parameters, define a meaningful key for each parameter and start using it. You no longer have to build your application every time you want to change the parameter value.


Notes

1: You need this reference only if you are using .NET 2.0
2: If you change the App.Config from Visual Studio IDE, then you need to rebuild your project to see the new values.

Tagged

Disable Close button in Windows form

To disable that little close button in your Windows form, either you can set the ControlBox field in Properties window to False or or add this two line in the contructor:

this.ControlBox = false;

Same goes for Maximize and Minimize boxes. Set it using Properties window or add these lines:


this.MaximizeBox = false;
this.MinimizeBox = false;

Simple, huh?

Building Visual Studio solutions using msbuild in cygwin

I use cygwin extensively and hate switching over to Visual Studio Command Prompt just to be able to use msbuild. I figured out how I can invoke msbuild from cygwin. It’s a neat and easy solution, but I am sure a better solution exists.

Steps to follow:

1. Create a file called vs [See Note 1]

2. Copy and paste the following line in the file:

cmd /k “C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat” x86

3. Save the file under /usr/bin. [See Note 2]

4. Make sure the file is executable. Execute the command

chmod +x vs

You are done.

Test it:

1. Open up cygwin and execute the file.

   cygwin> vs

It will display this message and give you a VS command prompt:

Setting environment for using Microsoft Visual Studio 2005 x86 tools

Note 1: You can use a different name for the file. I named it vs for visual studio.

Note 2: You can put it under any directory you like. If you decide to put it under any other directory, then you you have two options:
1. Add that directory to PATH. OR
2. Give the full path to the directory whenever you execute this file.

Note 3: For some reason the UP key does not work. I need to find a fix for this.

Note 4: I know a better solution exists. The batch file which I execute in the vs file is setting a few environment variables. Instead of invoking this batch file, I should set the environment variables directly. This is a neater solution.

Double clicking on .sln does not open it

Problem: Double clicking on .sln file does not open the solution in Visual Studio.

Solution: This may happen if you have some version control system installed, especially Clearcase. This is a problem with the extension-application association. If you right click on .sln file, you will see that the application associated with this extension is ‘Microsoft Visual Studio Version Selector’. Change the association to devenev.exe. You can find this executable under <Visual Studio installed directory>\Common7\IDE.

Next time when you double click on .sln file, it should open it Visual Studio IDE.

Note: This solution works for Microsoft Visual Studio 8 / 2005. This has not been tried on any other version.