Posts Tagged ‘vb.net’

How to replace one or more whitespace in vb.net

  1. Imports System.Text.RegularExpressions</p>
  2. Module Example
  3.    Public Sub Main()
  4.       Dim input As String = "This is   text with   far  too   much   " +
  5.                             "whitespace."
  6.       Dim pattern As String = "\s+"
  7.       Dim replacement As String = " "
  8.       Dim rgx As New Regex(pattern)
  9.       Dim result As String = rgx.Replace(input, replacement)
  10.       Console.WriteLine("Original String: {0}", input)
  11.       Console.WriteLine("Replacement String: {0}", result)
  12.    End Sub
  13. End Module

, , , ,

No Comments


How to use Bitwise in Vb.Net
    Enum eStatusNumber
        One = 1
        Two = 2
        Three = 4
        Four = 16
        Five = 32
    End Enum

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Test(eStatusNumber.Five Or eStatusNumber.Three)
    End Sub

    Private Sub Test(ByVal com As eStatusNumber)
        If (com And eStatusNumber.One) = eStatusNumber.One Then
            MsgBox("One")
        End If
        If (com And eStatusNumber.Two) = eStatusNumber.Two Then
            MsgBox("Two")
        End If

        If (com And eStatusNumber.Three) = eStatusNumber.Three Then
            MsgBox("Three")
        End If
        If (com And eStatusNumber.Four) = eStatusNumber.Four Then
            MsgBox("Four")
        End If

        If (com And eStatusNumber.Five) = eStatusNumber.Five Then
            MsgBox("Five")
        End If

    End Sub

VB.Net code can be downloaded here "EnumBitWise Sample".
Reference 1
Reference 2

				

, ,

No Comments


How to generate random colors in vb.net

Module RandomColors

Public Function GetRandomQBColor() As System.Drawing.Color

Dim rndom As New Random

Dim color_num As Integer = rndom.Next(0, 15)

Return System.Drawing.Color.FromArgb(QBColor(color_num) + &HFF000000)

End Function

Public Function GetRandomRGBColor() As System.Drawing.Color

Dim rndom As New Random

Return System.Drawing.Color.FromArgb(255, rndom.Next(0, 255), rndom.Next(0, 255), rndom.Next(0, 255))

End Function

End Module

, ,

No Comments


How to open the windows explorer programmatically in .net


How to open the windows explorer using vb.net

  1. System.Diagnostics.Process.Start("explorer.exe", "c:\tmp")

, , ,

No Comments


Build error: Load of Property ‘OutputPath’ failed. The entered path is not valid output path.


Open the .vdproj using Notepad or any text editor and search for the specified directory under ‘OutputPath’ element, if you found delete it!

, , ,

No Comments


Build error: Unable to create directory

Check the “Build Events” if there is any scripts, make sure the path entered in that window is exists otherwise you will get the error “Unable to create directory”.

Or change the framework you are using..

Or open the .vproj using a text file editor and search that specified directory.

, , ,

No Comments


How create a Container User Control

  1. Imports System.ComponentModel
  2. Imports System.ComponentModel.Design
  3.  
  4. <Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", GetType(IDesigner))> _
  5. Public Class UserControl1
  6.  
  7. End Class

, , ,

No Comments


How to select a row in GridView
  1. <asp:GridView ID="GridViewCategory" runat="server" AutoGenerateColumns="False"
  2.  DataKeyNames="CategoryID"
  3.  EmptyDataText="There are no data records to display."
  4.  CssClass = "GridViewStyle"
  5.  AlternatingRowStyle-CssClass="alt"
  6.  PagerStyle-CssClass="pgr"
  7.  SelectedRowStyle-CssClass="selected"
  8.  Width="100%" onrowdatabound="GridViewCategory_RowDataBound"
  9.  >
  10.  <Columns>
  11.   <asp:TemplateField HeaderText="ID">
  12.    <ItemTemplate>
  13.     <asp:LinkButton ID="lnkSelect" runat="server" CommandName="Select" CommandArgument='<%# Bind("CategoryID") %>' Text='<%# Bind("CategoryID") %>'></asp:LinkButton>
  14.    </ItemTemplate>
  15.      <ItemStyle Width="50px" />
  16.   </asp:TemplateField>
  17.   <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
  18.  </Columns>
  19.  <PagerStyle CssClass="pgr" />
  20.  <AlternatingRowStyle CssClass="alt" />
  21. </asp:GridView>

Code behind:

  1. protected void GridViewCategory_RowDataBound(object sender, GridViewRowEventArgs e)
  2. {
  3.  if (e.Row.RowType == DataControlRowType.DataRow)
  4.  {
  5.   LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[1];
  6.  
  7.   e.Row.Attributes.Add("onClick", ClientScript.GetPostBackClientHyperlink(lb, ""));
  8.   e.Row.Attributes.Add("onMouseClick", "javascript:this.style.cursor='pointer';");
  9.   e.Row.Attributes.Add("onMouseOut", "javascript:this.style.cursor='normal';");
  10.  }
  11. }

or

if you did not put a SELECT command button in the gridview, you can use the following code to mimic the Select function

  1. protected void GridViewCategory_RowDataBound(object sender, GridViewRowEventArgs e)
  2. {
  3.  if (e.Row.RowType == DataControlRowType.DataRow)
  4.  {
  5.   e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
  6.   e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
  7.  
  8.   e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridViewCategory, "Select$" + e.Row.RowIndex);
  9.  }
  10. }

, , ,

No Comments


How to use the Topmost property
  1. dim f as new frm1()
  2.  
  3. f.Topmost = True
  4. f.Owner = MainForm
  5. f.Show()

, ,

No Comments


How to Debug Window Service without installing


Usually if we want to debug the windows service project to see if it is behaving as we expected, what we do is by installing the windows service and attaching that project to the process.

There is also another way to debug without installing it, what we need to do is by creating another project such as console project then copy and paste the code below.

  1.     Sub Main()
  2. #If Not Debug Then
  3.         Dim servicesToRun As System.ServiceProcess.ServiceBase()
  4.  
  5.         servicesToRun = New System.ServiceProcess.ServiceBase() {New MyService()}
  6.         System.ServiceProcess.ServiceBase.Run(servicesToRun)
  7. #Else
  8.         Dim service As AS2Client.AS2SenderService = New MyService()
  9.         service.myentrysub()
  10.  
  11.         System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)
  12.  
  13. #End If
  14.  
  15.  
  16.        
  17.     End Sub


References:
Run Windows Service as a Console program

Debugging Windows Services under Visual Studio .NET

, , ,

No Comments



SetPageWidth