Author Topic: A few useful VB.net (visual basic 2010) snippets  (Read 6157 times)

0 Members and 1 Guest are viewing this topic.

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
A few useful VB.net (visual basic 2010) snippets
« on: August 02, 2012, 06:06:47 pm »
So recently I've been writing a project in vb.net for work and while it is a horribly messy and pain in the arse language, for little simple things requiring a windows form, it definitely gets the job done. And in the process of finding what all I needed to finish this project (currently on standby until I can touch bases with my boss and see what direction he wants me to go), with lots and lots of googling, I managed to find a few lines of code that might be useful to others in their endeavors. So to save you all the trouble of the exactly perfect phrasing it took me to find these lines, I'll post the harder (for me) to find ones here. Cheers


The following code allows you to remove blank lines from your listbox/combobox/etc. I had a problem in that my program was writing to list box 1 correctly, but list box 2 had every line alternating as blank. So after spending hours trying to figure out why this was, and appealing to smarter minds than my own for the answer without luck, I went searching for removing blank lines. And .Contains ("")  didn't work at all.
Code: (vb) [Select]
For Each Str As String In fileInput
   If String.IsNullOrWhiteSpace(Str) Then
      ListBox2.SelectedItems.Remove(Str)
   End If
Next

Reading from a delimited text file into a listbox. There are many ways to do it, but for some reason they never seemed to finish reading my weirdly formatted text file until I found this code. Now it works perfectly except for the extra blank lines, which is where the code above comes in handy  ;)

Code: (vb) [Select]
Dim parser As New FileIO.TextFieldParser("C:\Users\krazystr1ker\Documents\Visual Studio 2010\Projects\test.prm.txt")
        Dim fileInput As String()
        parser.SetDelimiters("#")   
        parser.TextFieldType = FileIO.FieldType.Delimited
        Try
            fileInput = parser.ReadFields   

            Do While Not parser.EndOfData
                fileInput = parser.ReadFields
                For Each Str As String In fileInput
                    ListBox1.Items.Add(Str)
                Next
            Loop


        Catch ex As FileIO.MalformedLineException
            MessageBox.Show(ex.Message)  'error messages

        Catch ex As Exception
            'Show the Error Message
            MessageBox.Show(ex.Message)
        End Try


This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

Offline glitcher

  • /dev/null
  • *
  • Posts: 14
  • Cookies: -2
    • View Profile
Re: A few useful VB.net (visual basic 2010) snippets
« Reply #1 on: December 25, 2012, 09:35:36 am »
you can also use the replace function  ;)

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: A few useful VB.net (visual basic 2010) snippets
« Reply #2 on: December 27, 2012, 12:25:58 am »
you can also use the replace function  ;)

Good to know, personally I abandoned the project I was coding in VB.net because I think it's a horribad language, I'm gonig to be redoing it in C#
But knowledge is never wasted, thanks for sharing that
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: A few useful VB.net (visual basic 2010) snippets
« Reply #3 on: December 27, 2012, 01:03:42 am »
Good to know, personally I abandoned the project I was coding in VB.net because I think it's a horribad language, I'm gonig to be redoing it in C#
But knowledge is never wasted, thanks for sharing that

+1 for figuring out the horrors of VB. Also a future +1 (if I remember) for C#, I don't really do a bunch in C# but from what I have done and from what others said, it is something Microstuff did right, with XBox anyway.
>>>import this
-----------------------------

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: A few useful VB.net (visual basic 2010) snippets
« Reply #4 on: December 27, 2012, 01:20:26 am »
TBH c# isn't that great, i still believe c++ and c are better languages period. However my company uses c# and asp.net for their software which has a server backend and a web page front end, so I need to learn it to get a job doing full time programming instead of just phone calling all the time. It is a bit easier to learn than c++ from what I've seen, and I do like the syntax much better than VB. But from what I remember of what Xires told me, C# isn't all that great. I'll let him post though since he's the one who told me that and personally I have not even written a complete program in c# lol

I guess really it just depends on the job your using it for, like any other language. If it's just for a computer based app, F c# and do c++ or C, if it requires a lot of web to server based interaction then C# would work out a bit better.

/shrug
Really, I can't say too much about it. Maybe we should open another topic to discuss this?
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: A few useful VB.net (visual basic 2010) snippets
« Reply #5 on: December 27, 2012, 01:51:44 am »
TBH c# isn't that great, i still believe c++ and c are better languages period. However my company uses c# and asp.net for their software which has a server backend and a web page front end, so I need to learn it to get a job doing full time programming instead of just phone calling all the time. It is a bit easier to learn than c++ from what I've seen, and I do like the syntax much better than VB. But from what I remember of what Xires told me, C# isn't all that great. I'll let him post though since he's the one who told me that and personally I have not even written a complete program in c# lol

I guess really it just depends on the job your using it for, like any other language. If it's just for a computer based app, F c# and do c++ or C, if it requires a lot of web to server based interaction then C# would work out a bit better.

/shrug
Really, I can't say too much about it. Maybe we should open another topic to discuss this?

Well, VB and C# was the only thing mentioned up until now. Yes C++ or even C will be like a thousand times better. C++ for regular anything PC. C I see is more for constraint related things like embedded stuff or anything that needs to be tweaked.


As for the what we are talking about VB<C#<C++<C. With C == to all. As for web related things that is a bit diff. But if anything I would go with C++, if not then C#, if not then VB, if not then I would kill myself el oh el.
>>>import this
-----------------------------

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: A few useful VB.net (visual basic 2010) snippets
« Reply #6 on: December 28, 2012, 01:53:11 am »
el oh el indeed, but thanks for clarifying techb, i agree. though, id probably shoot myself by the time i got to vb.net LOL
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply