Thursday, September 18, 2008

How to animate a Line WPF


Private Sub AnimateLine(ByVal myLine As Line, ByVal p1 As Windows.Point, ByVal p2 As Windows.Point)
        Dim daX As DoubleAnimation = New DoubleAnimation(p1.X, p2.X, New Windows.Duration(TimeSpan.FromSeconds(3)))
        daX.RepeatBehavior = RepeatBehavior.Forever
        daX.AutoReverse = True
        myLine.BeginAnimation(Line.X2Property, daX)

        Dim daY As DoubleAnimation = New DoubleAnimation(p1.Y, p2.Y, New Windows.Duration(TimeSpan.FromSeconds(3)))
        daY.RepeatBehavior = RepeatBehavior.Forever
        daY.AutoReverse = True
        myLine.BeginAnimation(Line.Y2Property, daY)
    End Sub

'Enjoy IT!

Tuesday, September 16, 2008

Simplest way to Animate a Control in WPF


This may be the simplest way to animate a dependency property in WPF

Button1.BeginAnimation(Button.HeightProperty, New DoubleAnimation(25, 150, New Duration(New TimeSpan(0, 0, 1))))


Enjoy IT!

Saturday, June 21, 2008

NETWORK SERVICE lose the write access to 'C:\WINDOWS\Microsoft.NET\Framework\v2.0

NETWORK SERVICE lose the write access to 'C:\WINDOWS\Microsoft.NET\Framework\v2.0

Run IIS Manager
Right click on Application Pool/DeafultAppPool
Click Properties
Under DefaultAppPool Properties click Identity
Select Predefined/Local System
Click Yes on the warning message

Click ok then restart IIS Service

You should now be able to view your asp web page.

Enjoy IT!

Tuesday, June 10, 2008

Disable screensaver in VB.NET

To enable and disable screen saver through vb.net


Imports Microsoft.Win32


Public Sub ScreenSaverDisable()
Dim regKey As RegistryKey
Dim regVal As String
regKey = Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)
regVal = regKey.GetValue("ScreenSaveActive")
If regVal = 1 Then
regKey.SetValue("ScreenSaveActive", "0")
End If
regKey.Close()
End Sub

Public Sub ScreenSaverEnable()
Dim regKey As RegistryKey
Dim regVal As String
regKey = Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)
regVal = regKey.GetValue("ScreenSaveActive")
If regVal = 0 Then
regKey.SetValue("ScreenSaveActive", "1")
End If
regKey.Close()
End Sub

Enjoy IT!

Monday, April 28, 2008

WPF Listbox Slide Animation in code behind VB.NET

'I just started work on WPF and the first thing i wanted to do was to animate a listbox item on selection change event. And the animation should be simple from left to right and back.
'So this the code to do it in code behind, for example if you have blank window then just paste the code below.
'Enjoy IT!

Imports System.Windows.Media.Animation

Class Window1
Private WithEvents ListBox2 As New ListBox
Private Sub Window1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyDown
If e.Key = Key.Escape Then Me.Close()
End Sub
Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Me.Content = Me.ListBox2
For i As Integer = 0 To 5
Dim tb As New TextBlock
tb.Text = Now.AddDays(i).ToLongDateString + " " + Now.AddMinutes(i).ToLongTimeString
tb.Width = 200
Dim b As New Border
b.Background = Brushes.LightBlue
b.BorderBrush = Brushes.Black
b.BorderThickness = New Thickness(2)
b.CornerRadius = New CornerRadius(45)
b.Padding = New Thickness(25)
'b.Width = 170
b.Child = tb

Dim sp As New StackPanel
sp.Children.Add(b)

Dim li As New ListBoxItem
li.Content = sp
li.Width = 250
li.HorizontalAlignment = Windows.HorizontalAlignment.Right

'AddHandler li.MouseEnter, AddressOf Me.ListBox1_MouseEnter
'AddHandler li.MouseLeave, AddressOf Me.ListBox1_MouseLeave
Me.ListBox2.Items.Add(li)
Next
End Sub

Private Sub ListBox1_MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs)
'Me.DoAnimate(CType(sender, ListBoxItem), 150, 300)
End Sub
Private Sub ListBox1_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs)
'Me.DoAnimate(CType(sender, ListBoxItem), 300, 150)
End Sub
Private Sub DoAnimate(ByVal li As ListBoxItem, ByVal ifrom As Integer, ByVal ito As Integer)
Dim da As DoubleAnimation = New DoubleAnimation()
da.From = ifrom
da.To = ito
da.Duration = New Duration(TimeSpan.FromSeconds(1))
li.BeginAnimation(ListBoxItem.WidthProperty, da)
End Sub
Dim oi As Integer = -1
Private Sub ListBox1_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ListBox2.SelectionChanged
Try
If Me.ListBox2.SelectedIndex <> -1 Then
Dim li As ListBoxItem = Me.ListBox2.Items(Me.ListBox2.SelectedIndex)
Me.DoAnimate(li, 250, 400)
If oi <> -1 Then
Dim li2 As ListBoxItem = Me.ListBox2.Items(oi)
Me.DoAnimate(li2, 400, 250)
End If
Me.oi = Me.ListBox2.SelectedIndex
Me.ListBox2.SelectedIndex = -1
End If
Catch ex As Exception
End Try
End Sub
End Class

Wednesday, March 19, 2008

How to put your applications shortcut in Startup in VB.NET


'imports
Imports IWshRuntimeLibrary
'code to execute
Dim ws As New WshShell
Dim StartupDir As String = My.Computer.FileSystem.SpecialDirectories.Programs + "\Startup"
If Not IO.Directory.Exists(StartupDir) Then
IO.Directory.CreateDirectory(StartupDir)
End If
If Not IO.File.Exists(StartupDir + "\" + My.Application.Info.AssemblyName + ".lnk") Then
Dim shortCut As IWshRuntimeLibrary.IWshShortcut
' short cut files have a .lnk extension
shortCut = CType(ws.CreateShortcut(StartupDir + "\" + My.Application.Info.AssemblyName + ".lnk"), IWshRuntimeLibrary.IWshShortcut)

' set the shortcut properties
With shortCut
.TargetPath = System.Reflection.Assembly.GetExecutingAssembly.Location()
.WindowStyle = 1
.Description = "App to run at Windows Startup"
.WorkingDirectory = StartupDir
' the next line gets the first Icon from the executing program
.IconLocation = System.Reflection.Assembly.GetExecutingAssembly.Location() & ", 0"
.Save() ' save the shortcut file
End With
End If

Backup SQL Server DB in VB.NET


Dim saveFileDialog1 As New SaveFileDialog()
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Dim pathToBackUpFile As String = saveFileDialog1.FileName
Dim sql As String = " use master;"
sql += " declare @p varchar(300);"
sql += " set @p = '" + pathToBackUpFile + "';"
sql += " SET NOCOUNT OFF;"
sql += " BACKUP DATABASE [YourDatabaseName] TO DISK = @p "
sql += " WITH NOINIT, NOUNLOAD, NAME = N'Database Backup', "
sql += " NOSKIP , STATS = 10, NOFORMAT ;"

Dim cnStr As String = "Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=master"'put your connection string to master db

Dim cnn As OleDb.OleDbConnection
cnn = New OleDb.OleDbConnection(cnStr)
cnn.Open()
Dim cmd As New OleDb.OleDbCommand(sql, cnn)
Dim i As String = -1
Try
i = cmd.ExecuteNonQuery()
cnn.Close()
cmd.Connection.Close()
Catch ex As Exception
cnn.Close()
cmd.Connection.Close()
i = ex.Message
End Try
If i = "-1" Then
MsgBox("BackedUp Database Successfully!", MsgBoxStyle.Information)
Else
MsgBox("Error in Backup!", MsgBoxStyle.Critical)
End If
End If

Restoring SQL Server Database in VB.NET

In VB.NET
Just make a connection to the Master Database of SQL Server
Then run this code against the connection


Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Bak Files|*.bak"
openFileDialog1.Title = "Select a Backup File"

If openFileDialog1.ShowDialog() = DialogResult.OK Then
Dim pathToBackUpFile As String = openFileDialog1.FileName
Dim sql As String = " use master;"
sql += " declare @p varchar(300);"
sql += " set @p = '" + pathToBackUpFile + "';"
sql += " RESTORE DATABASE [YourDatabaseName]FROM DISK = @p ;"
Dim cnStr As String = "Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=master"'put your connection string to master db
Dim cnn As OleDb.OleDbConnection
cnn = New OleDb.OleDbConnection(cnStr)
cnn.Open()
Dim cmd As New OleDb.OleDbCommand(sql, cnn)
Dim i As String = -1
Try
i = cmd.ExecuteNonQuery()
cnn.Close()
cmd.Connection.Close()
Catch ex As Exception
cnn.Close()
cmd.Connection.Close()
i = ex.Message
End Try
If i = "-1" Then
MsgBox("Restored Database Successfully!", MsgBoxStyle.Information)
Else
MsgBox("Error in Restore!", MsgBoxStyle.Critical)
End If
End If

Wednesday, January 02, 2008

How to Add a URL column for a GridView in ASP.NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ds As DataSet = New DBClass().SelectQueryforData()
Me.AddGrid(ds.Tables(0))
End Sub
Private Sub AddGrid(ByVal t As DataTable)
For Each c As DataColumn In t.Columns
If c.ColumnName = "PK_ID_Column" Then
Dim y As HyperLinkField
y = New HyperLinkField()
y.DataTextField = c.ColumnName
y.HeaderText = c.ColumnName
y.DataNavigateUrlFormatString = "~/YourUrl.aspx?q={0}"
y.DataNavigateUrlFields = New String() {c.ColumnName}
Me.GridView1.Columns.Add(y)
Else
Dim x As BoundField
x = New BoundField()
x.DataField = c.ColumnName
x.HeaderText = c.ColumnName
Me.GridView1.Columns.Add(x)
End If
Next
Me.GridView1.AutoGenerateColumns = False
Me.GridView1.DataSource = t
Me.GridView1.AlternatingRowStyle.BackColor = Drawing.Color.AliceBlue
Me.GridView1.DataBind()
End Sub

Enjoy IT!