Category Archives: WPF

How to Copy a File in vb.net

Copy a file in the file system

Supply a source file name and the destination file name

CopyFile(“c:\temp\firstfile.txt”, c:\temp\secondfile.txt”)

Function Copyfile(ByVal sSource As String, ByVal sDest As String) As Boolean

FileCopy(sSource, sDest)

If Err.Number = 0 Then Copyfile = True

End Function

Fix the SQL Single quote problems in SQL in vb.net

When the sql contains the single quotes then they have to be properly escaped otherwise it will give errors

just pass the field value which you want to check and the following function will return you the properly escaped string.

eg. o’brien will be returned as o”brien and this will not cause your sql to break.

Function FixSql(ByVal sStr As String) As String

FixSql = Replace(sStr, “‘”, “””)

End Function

How to get the file extension from the filepath

The following function returns the file extension only.

eg. using the following function

GetFileExtension(“c:\test\file.aspx”) will return “.aspx”

Here is the complete function, copy and paste it in your form or a module

Function GetFileExtension(ByVal sFilePath As String) As String
' Dim fullPath As String = "c:\MyDirectory\MYFile.txt"
Dim sExtension As String = IO.Path.GetExtension(sFilePath)
Return (sExtension)
End Function

Redirect to a new page in vb.net

Here is an example of a stand alone page which can help you to move your traffic to your new page.

Just copy the following lines into a note pad , remember to change the url to the actual page where you want to redirect.

Save the page with a .aspx extension.

eg. If you save this page as “OLD.ASPX” then every time someone types in this url eg. “http://vbnet.redirect.com.au/old.aspx”

it will redirect to the new page

<%@ Page Language="VB" Debug="true" %>

How to find First Sunday of each month

Here is the funtion in vb.net to find the first Sunday of each month

Function GetFirstSunday(ByVal dt As Date) As String

Dim inputDate As DateTime

Dim outputDate As DateTime

inputDate = Convert.ToDateTime(dt)

‘ Start at the day 1

outputDate = New DateTime(inputDate.Year, inputDate.Month, 1)

‘Loop through the month looking for the first Sunday

For i As Int32 = 0 To Date.DaysInMonth(outputDate.Year, outputDate.Month)

If outputDate.AddDays(i).DayOfWeek = DayOfWeek.Sunday Then

outputDate = outputDate.AddDays(i)

Exit For ‘We’re done.. get out of the loop

End If

Next

GetFirstSunday = FormatDateTime(outputDate, DateFormat.LongDate)

End Function

How to generate a random number in vb.net

How to generate a random number in vb.net

Function GenerateRandomNumber(iLowerRange As Integer, iUpperRange As Integer)
as integer

Dim RandomGenerator As Random
Dim intRandomNumber As Integer

RandomGenerator = New Random()

'Get the next random number
intRandomNumber = RandomGenerator.Next(iLowerRange, iUpperRange+ 1)

'Return the random # as the function's return value
return intRandomNumber

End Function

How to add Nuget Packages

How to Add Packages using NUGET in Visual Studio

Step 1

In Visual Studio Go to Tools > Library Package Manager > Manage Nuget Packages for Solution.

 

How to add Nuget Packages
How to add Nuget Packages

 

Step 2

You would get the following screen, you can check the packages which you have already available in your solution, you can search for new packages and install them to your solution.

Remember:  These packages need to be downloaded , each time you create a new Visual Studio solution

 

Packages in NUGET
Packages in NUGET

How to open second window from first window in wpf in C#?

This is how you would open a second window in WPF.


private void btnAppointmentPage_Click(object sender, RoutedEventArgs e)

{

//In this example we have already created a WPF Window and called
//it AppointmentsMain.xaml

AppointmentsMain AppointmentWindow = newAppointmentsMain();

AppointmentWindow.Show();

this.Close();

}