Group29.com - What did you expect?
  Create an account
:: Home  ::  Downloads  ::  Your Account  ::  Forums  ::
Google Adsense
Modules
  • Home
  • Downloads
  • ExpectationReviews
  • Forums
  • Group29 FAQ
  • MovieReviews
  • OtherReviews
  • Stories Archive
  • Submit News
  • Top 10
  • Topics
  • Web Links
  • New at Group29
    ·Movie Review: Buzz Lightyear Movie [10]
    ·Movie Review: The Marvels [7]
    ·UCLA vs. USC 2022 preview
    ·Expectation Review: Black Adam [22]
    ·Tuna Is Not The Favorite Pizza Topping in Minnesota
    ·Expectation Review: Captain Marvel [25]
    ·Forum Topic: Update your Facebook property
    ·Web Link: WEP Key Converter
    ·Forum Topic: Why does my IPA file get saved as a zip file in IE?
    ·Web Link: BeyondCompare

    read more...
    TheForce.Net
    ·Rebelscum Breast Cancer Awareness Charity Patch
    ·BBC Interviews J.J. Abrams About Trek And Wars
    ·CEII: Jabba's Palace Reunion - Massive Guest Announcements
    ·Fathead's May the Fourth Be With You!
    ·Star Wars Night With The Tampa Bay Storm Reminder
    ·Stephen Hayford Star Wars Weekends Exclusive Art
    ·ForceCast #251: To Spoil or Not to Spoil
    ·New Timothy Zahn Audio Books Coming
    ·SDCC: Exclusive Black Series Boba Fett With Han In Carbonite Set
    ·Star Wars Art Exposition May 4th

    read more...
    Hot trends
    ·Group29.com

    read more...
    Group29 Discussion Board :: View topic - Visual Basic .NET useful code snippets
    Log in Register Forum FAQ Memberlist Search
    Ratings, Links, Free Speech and more

    Group29 Discussion Board Forum Index -> Group29 Tech Tips -> Visual Basic .NET useful code snippets
    Post new topic  Reply to topic View previous topic :: View next topic 
    Visual Basic .NET useful code snippets
    PostPosted: Wed Sep 14, 2005 1:34 pm Reply with quote
    BB
    Regular
     
    Joined: Jun 23, 2004
    Posts: 340


      


    This Topic is to discuss some useful code for Visual Basic .NET
    View user's profile Visit poster's website
    Look up State by Abbreviation Web Cache Example Class
    PostPosted: Wed Sep 14, 2005 1:38 pm Reply with quote
    BB
    Regular
     
    Joined: Jun 23, 2004
    Posts: 340


      


    This was done in Visual Studio .NET 2003 in Visual Basic.
    The code was borrowed from an article: Using XML to Store States and Provinces.
    http://aspnet.4guysfromrolla.com/articles/022004-1.aspx

    I altered it to use a database with two fields: State Full Name and State Abbreviation. It could be used for any key value hash that you might like to cache from a database for a web based application.

    Code:
    Imports System
    Imports System.Web.Caching
    Imports System.Data
    Imports System.Data.OleDb

    Imports Microsoft.VisualBasic

    ' Borrowed from 4guysfromrolla.com
    ' modified to use A Database instead of XML file
    Public Class DBState
        Private m_name As String
        Private m_abbreviation As String

        Public Sub New(ByRef nameArg As String, ByRef abbreviationArg As String)
            m_name = nameArg
            m_abbreviation = abbreviationArg
        End Sub

        Public ReadOnly Property Name() As String
            Get
                Return m_name
            End Get
        End Property

        Public ReadOnly Property Abbreviation() As String
            Get
                Return m_abbreviation
            End Get
        End Property

        Public ReadOnly Property FullAndAbbrev() As String
            Get
                Return m_name & " (" & m_abbreviation & ")"
            End Get
        End Property

    End Class



    ''' <summary>
    ''' Provides the functionality related to retrieving the list
    ''' of states for a system; this is meant for US states,
    ''' territories, and Canadian provinces.  It can also be used
    ''' for other countries that have states or analogous areas.  It
    ''' uses the States.config file as its data source.
    ''' </summary>
    Public Module DBStateManager
        ' Cache object that will be used to store and retrieve items from
        ' the cache and constants used within this object
        Private myCache As Cache = System.Web.HttpRuntime.Cache()
        Private stateKey As String = "StateKey"
        'Public applicationConstantsFileName As String = Replace(System.AppDomain.CurrentDomain.BaseDirectory & "States.config", "/", "\")
        Private stateArray As DBState()
        Private errorList As ArrayList


        ' Tells you whether or not any errors have occurred w/in the module
        Public ReadOnly Property hasErrors() As Boolean
            Get
                If errorList Is Nothing OrElse errorList.Count = 0 Then
                    Return False
                Else
                    Return True
                End If
            End Get
        End Property


        ' Retrieves an array list of Exception objects
        Public ReadOnly Property getErrors() As ArrayList
            Get
                Return errorList
            End Get
        End Property


        ' Private method used to add errors to the errorList
        Private Sub addError(ByRef e As Exception)
            If errorList Is Nothing Then errorList = New ArrayList
            errorList.Add(e)
        End Sub

        ''' <summary>
        ''' Gets all the states
        ''' </summary>
        ''' <returns>An array of State objects</returns>
        Public Function getStates() As DBState()
            If myCache(stateKey) Is Nothing Then
                PopulateCache()
            End If
            Return stateArray
        End Function


        ''' <summary>
        ''' Gets the state object based on the abbreviation
        ''' </summary>
        ''' <param name="abbreviation">The abbreviation of the state
        ''' to retrieve</param>
        ''' <returns>A State object for the abbreviation
        ''' given</returns>
        Public Function getStateByAbbreviation(ByRef abbreviation As String) As DBState
            If myCache(stateKey & abbreviation) Is Nothing Then PopulateCache()
            Return myCache(stateKey & abbreviation)
        End Function


        Private Sub PopulateCache()
            'Dim DB InfoFile As New DB InfoDocument
            'Dim theState As State
            'Dim theNode As DB InfoNode
            Dim mySQL As String

            Dim theName, theAbbreviation As String
            Dim i As Integer = 0

            Dim conStr As String = ConfigurationSettings.AppSettings("PdmlDBConStr")
            Dim con As New OleDbConnection(conStr)
            Dim dr As OleDbDataReader

            mySQL = "SELECT [STATE Abbreviation],[State Full Name] from DBState"
            Try
                con.Open()
                Dim comm As New OleDbCommand(mySQL, con)
                dr = comm.ExecuteReader()

                ' TODO: %%% Magic 100 states in array!
                stateArray = Array.CreateInstance(GetType(DBState), 100)
                i = 0
                While dr.Read
                    theAbbreviation = Trim(dr.GetString(0))
                    theName = Trim(dr.GetString(1))
                    'Populate that location in the array with the
                    ' values for the tag
                    stateArray(i) = New DBState(theName, theAbbreviation)

                    'Insert the state into cache
                    myCache.Insert(stateKey & theAbbreviation, stateArray(i))
                    i = i + 1
                End While

                myCache.Insert(stateKey, stateArray)

                dr.Close()

                con.Close()

            Catch e As Exception
                addError(e)

            End Try
        End Sub
    End Module



    Then use this code to call it.

    Code:
    Dim TheStateObject As DBState
    Dim TheStateLongName As String

    TheStateObject = DBStateManager.getStateByAbbreviation(TheState)

    If TheStateObject Is Nothing Then
       TheStateLongName = ""
    Else
       TheStateLongName = TheStateObject.Name
    End If
    View user's profile Visit poster's website
    System.Drawing.Color Chart
    PostPosted: Thu Sep 15, 2005 8:12 am Reply with quote
    BB
    Regular
     
    Joined: Jun 23, 2004
    Posts: 340


      


    Useful chart at:

    http://www.pardesiservices.com/softomatix/colorchart.asp

    Here is some sample code to generate the color chart.

    http://www.codeproject.com/csharp/reflectioncolorchart.asp

    http://www.pardesiservices.com/Softomatix/Reflection.asp

    In Visual Studio GDI+, for drawing any item we need to pick up a color for either creating a brush or a pen. .NET framework has provided System.Drawing.Color structure which contains 141 colors.

    The .NET framework procides a very powerful feature called Reflection. All the colors in System.Drawing.Color structure are declared as public static properties.

    More about System.Drawing.Color from MSDN
    View user's profile Visit poster's website
    Dynamic Buttons for ASP.NET pages
    PostPosted: Tue Oct 11, 2005 10:17 am Reply with quote
    BB
    Regular
     
    Joined: Jun 23, 2004
    Posts: 340


      


    I had a need to create a set of buttons to match items in an arraylist.

    Add a placeholder control to your ASP.NET web page named placeholder1

    Now put this code in the codebehind page. Add a call to the LoadButtonsToPlaceHolder() subroutine in the page load control. Make sure it also appears on postback (do not put in the IsPostback check.)


    Code:
    Private Sub LoadButtonsToPlaceHolder()
       Dim GroupArray As ArrayList

       Dim ButtonNumber As Int16 = 1
       Dim AButton As System.Web.UI.WebControls.Button
       
       GroupArray.Add("Press Button For Test1")
       GroupArray.Add("Press Button For Test2")
       GroupArray.Add("Press Button For Test3")
       
       For Each GroupName As String In GroupArray
           AButton = New Button
           AButton.Text = "**"
           AButton.ID = Trim(Str(ButtonNumber))

           AddHandler AButton.Click, AddressOf ButtonClickHandler
           AddHandler AButton.Command, AddressOf ButtonCommandHandler
           PlaceHolder1.Controls.Add(AButton)
           PlaceHolder1.Controls.Add(New LiteralControl(GroupName & "<BR>"))
           ButtonNumber = ButtonNumber + 1
       Next
    End Sub


    Add the code to handle the button click. One handler covers any click and makes the decision based upon the button ID.

    Code:
    ' Handle any button click, look up by ID
    Private Sub ButtonClickHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)

       Dim WhichButton As System.Web.UI.WebControls.Button
       Dim WhichTest As Int16
       WhichButton = CType(sender, System.Web.UI.WebControls.Button)
       WhichTest = Val(WhichButton.ID)

       End Sub

    ' Note: For some reason, if you do not assign a buttoncommandhandler with a buttonhandler on a dynamic set of buttons
    ' then it will not work....
    Private Sub ButtonCommandHandler(ByVal sender As System.Object, ByVal e As System.web.ui.WebControls.CommandEventArgs)
       ' Do Nothing
    End Sub


    This will build the dynamic buttons and will be handled by the buttonhandler.
    View user's profile Visit poster's website
    Ten tools to download and Ten add-ins for VSTUDIO developers
    PostPosted: Tue Feb 14, 2006 4:12 pm Reply with quote
    BB
    Regular
     
    Joined: Jun 23, 2004
    Posts: 340


      


    Ten Must-Have Tools Every Developer Should Download Now
    http://msdn.microsoft.com/msdnmag/issues/04/07/MustHaveTools/
      NUnit to write unit tests
      NDoc to create code documentation
      NAnt to build your solutions
      CodeSmith to generate code
      FxCop to police your code
      Snippet Compiler to compile small bits of code
      Two different switcher tools, the ASP.NET Version Switcher and the Visual Studio .NET Project Converter
      Regulator to build regular expressions
      .NET Reflector to examine assemblies


    Visual Studio Add-Ins Every Developer Should Download Now
    http://msdn.microsoft.com/msdnmag/issues/05/12/VisualStudioAddins/default.aspx
      TestDriven.NET to write unit tests
      GhostDoc to create XML comments
      Smart Paster to paste clipboard text as comments or strings
      CodeKeep to reuse snippets of code
      PInvoke.NET PInvoke.NET is a wiki that can be used to document the correct P/Invoke signatures
      VSWindowManager PowerToy to save custom Visual studio IDE layouts
      WSContractFirst create web service schema files
      VSMouseBindings - assign each of your mouse buttons to a Visual Studio command.
      CopySourceAsHTML - Code is exponentially more readable when certain parts of that code are differentiated from the rest by using a different color text
      Cache Visualizer
    View user's profile Visit poster's website
    IsNull in VB 6 to comparing Null/Nothing in VB.NET
    PostPosted: Wed May 10, 2006 12:36 pm Reply with quote
    BB
    Regular
     
    Joined: Jun 23, 2004
    Posts: 340


      


    COmparing null objects in Visual Basic

    In vbscript/vb6 the syntax is:

    Code:
    If Not (obj Is Nothing) Then

    end if

    In VB.NET 8.0 (VB.NET 2005), it's:

    Code:
    If obj IsNot Nothing Then

    end if



    When using the Visual Studio 2005 to upgrade a Visual Basic 6.0 project, you may encounter this warning.

    Code:
    UPGRADE_WARNING: Use of Null/IsNull() detected.

    It will erroneously change the IsNull(variable) function to variable = System.DBNull.Value. This would only apply when comparing a database value. Use one of the above methods to check null strings and objects in VB.NET.


    There is no VB.NET equivalent of the null keyword. And the IsNull function is gone. Instead, VB.NET uses the Nothing keyword.

    Code:
    Dim sTest As String
    If (sTest Is Nothing) Then
    Console.WriteLine("sTest is Null")
    End If



    There is a ReadOnly member of the String Class called Empty.


    Code:
    Dim s As String

        ' Some other code

    If s = String.Empty Then 
        ' Do something
    end if


    There is the IsNothing function available in the Microsoft.VisualBasic namespace. And, there is nothing wrong with using this namespace in VB code. It can be used in C# as well. It is part of the .NET framework

    Code:
    If Not IsNothing(d) Then 
        ' Do something
    end if


    IsNullOrEmpty, new in Visual Basic 2005 for strings, combines both of the above tests and combines them into one new method in the String class - IsNullOrEmpty. It is a Shared function that returns a Boolean value, so the syntax is:


    Code:
    '(where s is a String variable)
    If String.IsNullOrEmpty(s) Then   
        ' Do Something
    end if


    http://msdn2.microsoft.com/en-us/library/system.string.isnullorempty.aspx

    See this (Free) book for more helpful information
    Microsoft Patterns and Practices
    Upgrading Visual Basic 6.0 Applications to Visual Basic .NET and Visual Basic 2005

    http://msdn.microsoft.com/vbrun/staythepath/additionalresources/default.aspx
    View user's profile Visit poster's website
    Visual Basic .NET useful code snippets
      Group29 Discussion Board Forum Index -> Group29 Tech Tips
    You cannot post new topics in this forum
    You cannot reply to topics in this forum
    You cannot edit your posts in this forum
    You cannot delete your posts in this forum
    You cannot vote in polls in this forum
    All times are GMT - 6 Hours  
    Page 1 of 1  

      
      
     Post new topic  Reply to topic  


    Powered by phpBB © 2001-2003 phpBB Group
    Theme created by Vjacheslav Trushkin
    Forums ©
    Group29 Productions

    All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest (c) 2006 by Group29 Productions.


    You can syndicate Group29 Productions news with an RSS Feeder using the file backend.php


    PHP-Nuke Copyright © 2005 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
    Page Generation: 0.20 Seconds

    :: HeliusGray phpbb2 style by CyberAlien :: PHP-Nuke theme by www.nukemods.com ::