Category: "Example Codes"
C# codes examples
I found the best way to learn a programming is reading example codes. So, I found this website,Microsoft All-In-One Code Framework, is very useful. Roughly, it got hundreds of example codes. THIS IS VERY COOL!
Error Message :"The type 'TEntity' must be a reference type in order to use it as parameter 'T' in the generic type or method 'XXXXX' (CS0452) -"
I got a line of code like this:
Code
public class HibnerateRepository<TEntity, TId> : IRepository<TEntity, TId> | |
...... | |
public IList<TEntity> GetAll() |
That sounds alright, but I got an Error Message :"The type 'TEntity' must be a reference type in order to use it as parameter 'T' in the generic type or method 'XXXXX' (CS0452) -".
Well, that is a common mistake and easy to fix.
Code
public class HibnerateRepository<TEntity, TId> : IRepository<TEntity, TId> where TEntity:class | |
...... | |
public IList<TEntity> GetAll() |
Set the icon of a window in WPF
Setting the Icon of a window in WPF is very simple! In your window xaml, you need to put the filename of your icon file in icon attribute, like the following:
Code
<Window x:Class="GUIDGenerator.Window1" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
Title="GUIDGenerator" Height="300" Width="300" Icon="generator.ico" | |
> |
Moreover, please the properties in the icon file. The build action sets to be "Content" and Copy to output directory sets to "Always".
Center aligning the buttons in a Windows Form
I was trying to align some buttons in the center of a windows form. I was tried to find any properties in a button can set it to be center aligned. But there is no such property! I tried to use layout controls. The table layout control can be very similar stuff. That is still not what I want. Of course, I can do it in the windows resizing, changing the button positions by codes. But that is too complex! Finally, I found a simple way. I just drop a panel at the center position and set no achor. I think that makes it flow at the center.
And then, you can drop buttons in the panel and set their achor property to "Top, Bottom, Left, Right"
Then it will move to the center position when the windows is resized.
Write a wav file player in C#
That is a bit hard to write a mp3 player in C#. I think you may need to find third party mp3 decoder dll. But if you only want to play to a wav file, that is very easy. .Net libraries already have a control class for you.
Code
System.Media.SoundPlayer |
All you need to put the file name into the constructor
Code
SoundPlayer player = new SoundPlayer(FileName); |
Then you can play and stop the file by these methods:
Code
player.Play(); | |
player.Stop(); |
This afternoon, I wrote an example project for this. Moreover, this project is implemented in WPF rather than normal Windows Forms, because I wish to practice my WPF skills.
Download the example project, please click here.