MSDN Code Gallery

Microsoft recently launched a new site called MSDN Code Gallery. It’s basically a site where you can publish your code projects, snippets, articles etc. It’s being used heavily by Microsoft expmloyees so there’s already some pretty interesting sourcecode on the site. So if you have something interesting to share, show the world and post it on MSDN Code Gallery.

Text to Speech in 3 lines of code

Hi,

In this post I will show you how you can create a very simple text to speech program using just a few lines of code. It’s actually very simple using the System.Speech namespace. You first start by adding a new reference to System.Speech.

image

After that make sure to actually use this namespace in your code.

using System.Speech;

Ok now that we did that, le’ts finish off the codework. The only thing we need to do is create an instance of the Speech.Synthesis.SpeechSynthesizer class. Set the voice and the PC will do the talking.

if(txtMsg.Text!=String.Empty)
{
       using(System.Speech.Synthesis.SpeechSynthesizer synth = 
        new System.Speech.Synthesis.SpeechSynthesizer())
       {

           synth.SelectVoiceByHints(System.Speech.Synthesis.
           VoiceGender.Male,
           System.Speech.Synthesis.VoiceAge.Senior, 0);
           synth.Speak(txtMsg.Text);
       }
}
else
{
      MessageBox.Show("Nothing to say!");
}
Ok I have to admit, there are some more lines of code,
I did this to actually make sure we have something to say.



				
			

.NET 3.5 Namespace poster updated

Some time ago I posted a namespace poster for the .NET 3.0 framework. The poster gave a complete overview of the most commonly uses types and namespace of WPF,WCF, WF, ASP.NET, DATA XML, LINQ and the basic Fundamentals. Now there are new versions, some minor updates. You can find the poster in different formats on the following links.

Full XPS
Split XPS
PDF
Tiled PDF

 

If anyone wants to print this for me, please post a comment :p

Using typeConverter

Hi,

In this example I’ll show you how you can use the typeConverter class to convert one type to another. For example you can use the typeConverter to convert a string to a color. You can use it for about any type.

I first go through the Color enumeration and afterwards I change the color of a label when a certain color is selected. Here is the code.

string[] colorArray; if (!this.IsPostBack) { //get the names from the Color enumeration colorArray = Enum.GetNames(typeof(KnownColor)); //Bind the array to the list lstColors.DataSource = colorArray; lstColors.DataBind(); } //Convert the string to the color //Here we need to first set the correct converter and then convert it. this.lblTest.BackColor = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(lstColors.SelectedItem.Text);

Oh yea, don’t forget to add this namespace:

using System.ComponentModel;

List every available font in a dropdown box

Hi,

In this post I’ll show you how can generate a list with every font that’s installed on your pc in your ASP.NET page or windows application. You need the following namespaces so don’t forget to include them.

using System.Drawing; using System.Drawing.Text;

Now I’ll show you the sourcecode:

protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { //Create an instance of this class, this contains all the fonts InstalledFontCollection fonts = new InstalledFontCollection(); //create a FontFamily variable and run through each available family foreach (FontFamily family in fonts.Families) { //and add that family to the dropdownbox lstFonts.Items.Add(family.Name); } } //change the font-family style of the label's text. this.lblTest.Style["font-family"] = lstFonts.SelectedItem.Text; }

I added a combobox to the page and enabled the AutoPostBack propperty. This way a PostBack is generated every time the user selects another font. With every different selection the Page_Load event is raised. This isn’t the best way to do this, but good enough for this example.

image

Changing width and height of a web control

If you want to set the width and the height of a web control, you can’t just call it like f.e. the text property and give it a certain value in pixels or percentage. This will result in an error. You need to use one of the static functions of the Unit class. You can set the size via pixels or percentage. This works like this:

protected void btnChangeSize_Click(object sender, EventArgs e) { lblResult.Width = Unit.Pixel(100); lblResult.Height = Unit.Percentage(5); lblResult.Text = "Width: " + lblResult.Width + ", Height: " + lblResult.Height; }

Display special symbols and HTML code in your ASP.NET page

Hi,

If you try to add the following examples to your site you will not get the expected result:

Enter your PIN <here>

You need to use <a> tag to create a hyperlink.

They don’t give the expected result because ASP.NET tries to convert this to something it knows. It sees it a tag and thinks it needs to convert that. This is basically an encoding problem.  You need to convert the <,> symbols to something that HTML can encode as a regular  < or > and not a full tag. This is done using the Server.HtmlEncode() function.

You can solve the problem this way:

protected void Page_Load(object sender, EventArgs e) { lblResult.Text = Server.HtmlEncode("Enter your PIN <here>"); lblResult.Text += "<br />" + Server.HtmlEncode("You need to use the <a> tag to create a hyperlink."); }

Custom settings from web.config

Hi,

In this example I’ll show you how you can get custom settings from the web.config file in your ASP.NET project. You can add as many custom settings as you want.

First you open your web.config and search for the <appSettings> tag. Here you use the add tag. You have to specify a specific key and give it a value. This looks like this:

<appSettings> <add key="logDir" value="c:\logs"/> </appSettings>

Then you can call this keyed value in your ASP.NET page like this:

protected void Page_Load(object sender, EventArgs e) { Result.InnerText = "The path of the log directory: " + WebConfigurationManager.AppSettings["logDir"]; }

WebConfigurationManager needs this namespace:

using System.Web.Configuration;

So don’t forget to add that.

ASP.NET Tips & Tricks

Hi,

I like studying and also like to share my knowledge with other people. I get my knowledge from the net so I think it’s normal that I share what I know with my public.

Lately I’ve been working quite a lot with ASP.NET and have learned a lot of new things. My idea was to share some of that cool stuff with you. So when I come across information I think is useful for me I will post that here. This can be very short posts because I’m basically going to post a short peace of code and a little bit of information.

Hope people will find that useful!

What would Bill Gates’s last day look like ?

A couple days ago Bill Gates gave his last keynote on CES (Consumer Electronics Keynotes). Too bad I never got to see him live..

He showed a funny video spoof of what his day would look like. You can watch it here. The video also has some big celebrities in it.

You can watch the entire keynote on this link.

Volgende pagina »