Welcome to WindowsClient.net | Sign in | Join
In the current project, I have to develop some windows services to sending out notification mails. The first challenge I faced is I can't directly run and debug the Windows service. For that I need to install the service (using installutil –I ServiceName) and attach the process to Visual studio, and then only I can debug it. It works fine for me. Then the next challenge was, debugging the code in the onStart event. Because once the service started, then only we can able to attach it to Visual Studio. So I put some code to write to text file, and/or event log. But I feel like it’s not a good method. After searching in the net I got a nice workaround.

protected override void OnStart(string[] args)
{
System.Diagnostics.Debugger.Launch();
//Rest of the code
}

While starting the Service, it will display a dialog like this



 

After selecting Yes, Visual Studio will open up and you can start debugging. You can get more information from these links
  1. http://blogs.msdn.com/yaleeyangmsblog/archive/2007/05/02/three-ways-debugging-net-windows-service.aspx
  2. http://geekswithblogs.net/GertVerhoeven/archive/2007/11/28/117181.aspx
  3. http://msdn.microsoft.com/hi-in/magazine/cc301845(en-us).aspx

I started my .net career in VB.Net. While developing a windows application in VB.Net it is pretty easy to put a Splash screen. There is Project property called Splash screen and you can put any form there, after that the specified form will act as a Splash screen. But when I started an windows application in C#, there is no project property called Splash screen. While doing some searching, I found lot of ways, like Putting a timer and closing the splash etc. But I feel like it is not the right way of doing a splash screen, then I started my own implementation and found one. I am not sure it is good one or not, but it is working for me.

Here is the steps:

1) Added one Form with an Image, as Splashscreen.cs, and my main UI is Welcome.cs
2) In the Program.cs I modified the code like the following

[STAThread]
static void Main()
{
    Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    SplashScreen s = new SplashScreen();
    s.Show();
    Welcome welcome = new Welcome(s);
    Application.Run(welcome);
}

3) And in the Welcome.cs, added one parameterized constructor, with SplashScreen as the Parameter

SplashScreen _s = null;
public Welcome(SplashScreen s)
{
    InitializeComponent();
    this._s = s;
}

4) And in the Form_Load() event I added code to close the splash screen if exists.

if (this._s != null) { this._s.Close(); }

5) Run the application, see the splash is coming and after that Main UI is coming

logo

Kerala Microsoft user’s group (K-MUG)
is organizing a Mini-TechEd in Trivandrum. Don’t miss this Free opportunity to learn about Windows 7, Visual Studio 2010 features, WPF, What is new in ASP.NET 4.0, SQL server best practices,SQL logical query execution and optimization tips,Hidden Gems in SQL Server.

Date : 27th June 2009
Venue : Technopark, Trivandrum

Agenda

   1. Visual Studio 2010 Features
   2. What is new in ASP.NET 4.0
   3. Silverlight 3.0
   4. Windows 7 for developers
   5. SQL server best practices
   6. SQL logical query execution and optimization tips
   7. WPF Graphics
   8. Hidden Gems in SQL Server
   9. All about User Experience

Register Now!

Related Links:

WPF Menu - How to implement Shortcut Heys

There are many ways to add shortcut keys in WPF

# Using Underscore - In the Header Property, put the "_" before the Text, and you can use "ALT+<Key>". This is not Shortcut key, it is normally called "HotKey"

   1:  <MenuItem Header="_Open" /> 

# Using InputGestureText - This is the way Visual Studio gives directly. - You can set a Key combination directly over here. 

   1:  <MenuItem Header="_Open" InputGestureText="Ctrl + O" /> 

But it will invoke the click event, even if we use the Key combination. This is for display purpose only.

#Using CommandBindings - - I found this way is better and easier one

  1. In the markup create the command Bindings using Command Collections.
    <CommandBinding Command="ApplicationCommands.Open" 
    CanExecute="CommandCanExecute" Executed="CommandExecuted" /> <CommandBinding Command="ApplicationCommands.Close"
    CanExecute="CommandCanExecute" Executed="CommandExecuted" />

  2. In the code implement the event Handlers.

       1:  Sub CommandExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
       2:  ‘Add the commands to be executed.
       3:  If e.Command.Equals(ApplicationCommands.Open) Then
       4:  MessageBox.Show(”Open Menu item clicked”)
       5:  ElseIf e.Command.Equals(ApplicationCommands.Close) Then
       6:  MessageBox.Show(”Close menu item clicked”)
       7:  End If
       8:  End Sub
       9:  Sub CommandCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
      10:  e.CanExecute = True ‘I am always returning true, based on this the menu item will enabled / disabled
      11:  End Sub

  3. Assign the Commands to the Command property of our menu items.
    <MenuItem Command="ApplicationCommands.Open" >
    <Separator />
    <MenuItem Command="ApplicationCommands.Close" />

    Compile it run it. You will see “Ctrl + O” added to the right side Open menu item.

    You can see Ctrl + O added into the Open Menu item(Screenshot)

menu_shortcuts_in_wpf

Happy WPF Programming :)

Few days back, I got a requirement to implement Free Hand drawing in .Net. I searched a lot but I didn’t get any good solution, the one I found from code project was Invalidating the picture box every time. So today I got a solution, it not seems to be a perfect solution because Paint events occurs, we need to re-paint the whole lines again.

I am attaching the code here. I will update the post once I get some perfect solution for this.

Private m_Drawing As Boolean = False
    Private m_List As List(Of Point) = Nothing
    Private Sub MyPic_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyPic.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            m_Drawing = True
        End If
    End Sub
    Private Sub MyPic_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyPic.MouseUp
        If m_Drawing AndAlso m_List IsNot Nothing Then
            If m_List.Count >= 2 Then
                MyPic.CreateGraphics.DrawLines(New Pen(Color.Blue, 1), m_List.ToArray())
            End If
            m_Drawing = False
            m_List.Clear()
        End If
    End Sub

    Private Sub MyPic_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyPic.MouseMove
        If m_List IsNot Nothing AndAlso m_Drawing Then
            m_List.Add(e.Location)
        End If
    End Sub
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_List = New List(Of Point)
    End Sub

 
Page view counter