Tag Archive for VBNet

VB.NET Log File

Sub Log(ByVal txt As String)
        'Open a file for writing
        Dim FILENAME As String = Server.MapPath("Log.txt")

        'Get a StreamReader class that can be used to read the file
        Dim objStreamWriter As StreamWriter
        
        objStreamWriter = File.AppendText(FILENAME)

        objStreamWriter.WriteLine(DateTime.Now.ToString() + " >> " + txt)
    
        'Close the stream
        objStreamWriter.Close()
    End Sub

source

VB.NET Base64 To Image

Function Base64ToImage(ByVal base64string As String) As System.Drawing.Image
        'Setup image and get data stream together
        Dim img As System.Drawing.Image
        Dim MS As System.IO.MemoryStream = New System.IO.MemoryStream
        Dim b64 As String = base64string.Replace(" ", "+")
        Dim b() As Byte
    
        'Converts the base64 encoded msg to image data
        b = Convert.FromBase64String(b64)
        MS = New System.IO.MemoryStream(b)
   
        'creates image
        img = System.Drawing.Image.FromStream(MS)
        
        Return img
    End Function

source

Show system time into StatusStrip

Private Sub TimerRelogio_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerRelogio.Tick
        Me.lblTime.Text = Date.Now
    End Sub

 Private Sub frmMDIParent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TimerRelogio.Start()
    End Sub

source

ProgressBar code

frmMDIParent.ToolStripProgressBar1.Value = 0
        frmMDIParent.ToolStripProgressBar1.Visible = True
        For i As Integer = 0 To frmMDIParent.ToolStripProgressBar1.Maximum - 1

            frmMDIParent.ToolStripProgressBar1.Value = frmMDIParent.ToolStripProgressBar1.Value + 1
        Next

source

Stripe rows in ASP.NET repeater control without using AlternatingItemTemplate

<div class="<%# if(Container.ItemIndex Mod 2 = 0,"","altRow") %>">

source

获得启动参数

Public Function ParseCommandLineArgs(ByVal args As String()) As Hashtable
Dim argsTable As New Hashtable()

If (args Is Nothing) OrElse (args.Length = 0) Then
Return argsTable
End If

Dim arg As String
Dim key As String
Dim val As String
Dim pos As Integer
Dim tempVal As System.Text.StringBuilder
Dim preChar As String = ""

For i As Integer = 0 To args.Length - 1
arg = args(i)

pos = arg.IndexOf("=")

If pos > 0 Then
key = arg.Substring(0, pos)
pos = pos + 1
val = arg.Substring(pos, arg.Length - pos)
' 両側のダブルクォーテーションをはずす
If (val.Length > 2) AndAlso (val.StartsWith("""") = True) AndAlso (val.EndsWith("""") = True) Then
val = val.Substring(1, val.Length - 2)
End If

tempVal = New System.Text.StringBuilder()
preChar = ""
For j As Integer = 0 To val.Length - 1
If preChar = "" Then
If val.Substring(j, 1) = "" Then
' を発見
tempVal.Append("")
preChar = ""
ElseIf val.Substring(j, 1) = """" Then
' "を発見
tempVal.Append("""")
preChar = """"
Else
tempVal.Append("")
preChar = val.Substring(j, 1)
tempVal.Append(preChar)
End If
Else
If val.Substring(j, 1) <> "" Then
tempVal.Append(val.Substring(j, 1))
End If
preChar = val.Substring(j, 1)
End If
Next
val = tempVal.ToString()
Else
key = arg
val = ""
End If

' キーを大文字にする
key = key.ToUpper()

If argsTable.Contains(key) = False Then
' キーが存在しないとき
argsTable.Add(key, val)
End If
Next

Return argsTable
End Function

Private Function GetCommandLine1(ByVal args() As String) As Boolean
'コマンドラインの引数を受け取る
Dim _ArgsTable As New Hashtable
_ArgsTable = ParseCommandLineArgs(args)

If _ArgsTable IsNot Nothing AndAlso _
_ArgsTable.Keys.Count > 0 AndAlso _
_ArgsTable.Contains("KEY") = True Then
Return String.IsNullOrEmpty(_ArgsTable("KEY").ToString())
Else
Return False
End If

End Function

source

VB.NETで、構造体をファイルに書き込む

Imports System.Runtime.InteropServices

Public Class Form1

' ********************************************************
' * 構造体定義
' ********************************************************
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Class INDIVIDUALINFO
Public dwOriginalSize As Integer = 1
Public dwCompressedSize As Integer = 1
Public dwCRC As Integer = 1
Public uFlag As Integer = 1
Public uOSType As Integer = 1
Public wRatio As Short = 1
Public wDate As Short = 1
Public wTime As Short = 1
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=513)> _
Public szFileName As String = "A"
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> _
Public dummy1 As String = "B"
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=8)> _
Public szAttribute As String = "C"
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=8)> _
Public szMode As String = "D"
End Class

<DllImport("Kernel32.dll", CharSet:=CharSet.Ansi, EntryPoint:="RtlMoveMemory")> _
Private Shared Sub CopyMyStruct( _
ByVal forFile As Byte(), _
<[In](), Out()> ByVal Source As INDIVIDUALINFO, _
ByVal Length As Integer)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

Dim data As New INDIVIDUALINFO()
Dim forFile As Byte() = New Byte(Marshal.SizeOf(data) - 1) {}

CopyMyStruct(forFile, data, Marshal.SizeOf(data))
System.IO.File.WriteAllBytes("struct_test.dat", forFile)

End Sub
End Class

source

Strip Decimal and Zero Fill

Imports System
Namespace StripDecimal_VB
Class RemoveDecimal
Shared Sub Main(ByVal args As String())
Dim testNum As Double = 255.95
Dim noDecimalVal As String = testNum.ToString().Replace(".", "").PadLeft(9, "0"c)

Console.WriteLine("Original Value: " + testNum.ToString())
Console.WriteLine("Value w/No Decimal: " + noDecimalVal)
End Sub
End Class
End Namespace

source

Get URL

Public Function GetUrl() As String
Dim strTemp As String = ""
If (Request.ServerVariables("HTTPS") = "on") Then
strTemp = "https://"
Else
strTemp = "http://"
End If
strTemp = (strTemp + Request.ServerVariables("SERVER_NAME"))
If (Request.ServerVariables("SERVER_PORT") <> "80") Then
strTemp = (strTemp + (":" + Request.ServerVariables("SERVER_PORT")))
End If
strTemp = (strTemp + Request.ApplicationPath)
Return strTemp
End Function

source

HttpContext in external class

Imports system.Web
Public Class log
Shared Sub PrintQueryString()
' usage: lib.PrintQueryString()
Dim Server As Web.HttpServerUtility = Web.HttpContext.Current.Server
Dim Response As Web.HttpResponse = Web.HttpContext.Current.Response
Dim Request As Web.HttpRequest = Web.HttpContext.Current.Request
Dim QueryString As System.Collections.Specialized.NameValueCollection = Request.QueryString
Dim Param As String
For Each Param In QueryString
Response.write("param name: " & Param )
Response.write("<br>")
Response.write("param value: " & QueryString(Param) )
Response.write("<br>")
Next
end Sub
end Class

source