Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

First you get the "sermon". Since you are required to write pseudocode anyway, write it first, then prefix each line of pseudocode with a single quote. There. You have just created all of your code comments. Now add the actual code. End of sermon.

Now a few suggestions.

InputBox("Values must be between 0 and 100 ", "Please enter student grades:")

returns a String, not an Integer so assign the data to a String value.

Integer.TryParse(grade, grad)

will return a value of 0 if the user enters non-numeric input. Your code won't be able to tell if the user has entered "0" (which is valid) or "jabberwocky" (which is not). I suggest you first determine if the input data is numeric. You can do that as follows:

Dim input As String = InputBox("Values must be between 0 and 100 ", "Please enter student grades:")

If IsNumeric(input) And InStr(input, ".") = 0 Then
    Integer.TryParse(input, grade)
Else
    'the data entered was not an integer number
End If

Note that the above doesn't validate beyond ensuring that the input can be converted to an integer value with no loss of precision. You still have to ensure that it falls between 0 and 100. If you have determined that the input is not valid, don't do any of the grade calculations. Your code continues with the calculations even if the input is bad.

Your range checks are inefficient. The test

If grade >= 90 Or grad = 100 …
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Access uses different wildcards than SQL. In Access, an asterisk matches any characters whereas SQL uses the percent sign. To match any single character in Access, use a question mark. SQL uses an underscore. So the statement

select * from myTable where CLASS LIKE '* VI'

will select all records where CLASS ends with " VI"

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

You have to use a ListView in details view if you want to use different colours for each line. You can do it as follows:

Dim item as New ListViewItem()
item.Text = "Bad"
item.ForeColor = Color.Red
ListView1.Items.Add(item)
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

In SQL, to select the largest value from a set of values you use the MAX function as in

select MAX(citation) from myTable
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Well, yeah. If you want to answer the question he actually asked (DOH!)

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

It appeared when I started another code snippet (which I did not submit because it was only a test). But it failed to appear the first time. As I said, I have been experiencing very odd behaviour which I am attributing to problems with my ISP here at the cottage. I never have these problems at home so I'll just put up with them until September.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

It didn't show that way when I entered the post. I didn't get the "Enter code snippet" window until later. I'll chalk that up to my continuing network problems while here at the cottage. There are several which can't be DaniWeb problems because if they were someone else would have noticied as well.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

I can't imagine how you would do this. When the power goes out your computer goes down immediately. If you have a UPS then the computer doesn't know the power is out. If you have a UPS that can send an event to the operating system then you have to have some way of getting that event into your application. If the application is interactive then the user will know that the power is out and, assuming there IS a UPS, will have several minutes to do a save.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

The answer is "it depends". It depends on the type of database (Access, SQL, mySQL, etc), and it depends on the actual data in the field. You can use wildcards in SQL queries such that if your query looks like

select * from myTable where MovieName like 'Rocky%'

then the returned records will be Rocky, Rocky II, etc. If you only want movies with VI (but not VII, etc) then the query

select * from myTable where MovieName like '% VI'

Note that you need the blank after '%' so that you don't match (for example) 'Halloween XVI'

kkhembrom commented: Thank you Sir.. +0
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

I just submitted a code snippet for the competition here. I entered my description/documentation, then entered the code after clicking on "Code" and pasting in the code window. I submitted the post and was then presented with another window saying "a fully working code snippet must be entered". So I pasted the code into that window. Now the post has two copies of the code. I had to go back and edit the post to remove the duplicate code. Isn't this just a tad confusing?

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Sometimes sorting data can be easy and sometimes it can be difficult. If you have data in a listview you can spend a great deal of time writing ICompare functions. This is appropriate for non-trivial applications but can be a little intimidating for inexperienced programmers. Also, it is a problem where the format of the input data changes. Detached recordsets provide a method which allows ad hoc sorting of data. With a little planning, they can be used to provide sorting for data where the number of fields is not known until runtime. Once you get the "messy" stuff out of the way (defining the fields), the actual operation is relatively simple. Also, sorting of columns with multiple criteria is trivial. Another benefit is that sorting can be done without having to write custom sort code. The following code requires a form with three buttons (named btnByName, btnByDate and btnBySalary), and one ListView (named ListView1). ListView1 should be set to Details view and have three columns with the header text "Name", "Date", and "Salary".

Sorting of the data in the recordset is done by assigning a string to the Sort property. Sorts can be in ascending (ASC) or descending (DESC) order. A separate order can be given for each field. You give the sort string as follows

recordset.Sort = "fldname direction, fldname direction, etc"

You can specify one field or multiple fields. The default sort order is ASC so the following

recordset.Sort = "fldname, fldname DESC"
Begginnerdev commented: Good tutorial +5
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

If you want to get the second to last entry you could do

mydata.MoveLast()
mydata.MovePrevious()

Assuming you have at least two records, mydata.BOF will be false and you will be pointing at the second to last record.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

To get a particular slot you can use the Keys collection. mylist.Keys(3) gets the key in the fourth (zero-relative) position and the value at that position is mylist(mylist.Keys(3)). If you want to sort on different fields then you will have to do it via code. Unless you want to get a little fancier. If so you can create what is called a detached recordset. The following code shows you how to do that. Along with the "Imports ADODB" line, you will also have to add a reference to adodb. To do that, go to the Project menu and select "Add Reference". You will find adodb under the .NET tab (you may have to sort on component name first).

Imports ADODB

Public Class Form1

    Private mydata As ADODB.Recordset

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mydata.Close()
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'Create a recordset object. Cursors can be server side or client side.
        'Because the recordset will not connect to a source (it is detached) we
        'have to specify a client side cursor (and no active connection).

        mydata = New ADODB.Recordset
        mydata.CursorLocation = CursorLocationEnum.adUseClient
        mydata.LockType = LockTypeEnum.adLockBatchOptimistic
        mydata.CursorType = CursorTypeEnum.adOpenStatic
        mydata.ActiveConnection = Nothing

        'Add one field for each field of data in the text file

        mydata.Fields.Append("fld1", DataTypeEnum.adDate)
        mydata.Fields.Append("fld2", DataTypeEnum.adInteger)

        mydata.Open()

        Dim first As Boolean = True

        For Each line As String In System.IO.File.ReadAllLines("d:\temp\test.txt")

            'split the input line inito two fields at the comma separator

            Dim flds() As String = line.Split(",") …
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Yes.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

In fact, the upper bound of the Gen.Next will never appear as one of the random numbers so if you want a number from 1 to 100 you must give 101 as the upper bound. As far as I am concerned it is brain dead but there you have it.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Use & instead of + to concatenate strings

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

I think it would be less confusing and more consistent to use "Reverend Jim" in both cases.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

@TnTinMN - I think you are overcomplicating things. Also, I don't see any floating point values so loss of accuracy isn't a concern.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

I have heard good things about PC Decrappifier. It was recommended both by Windows Secrets and Ask Leo

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

I happened to be browsing the code snippets in vb.net and I noticed that in the thread list, a code snippet I contributed a while back is credited to "Reverend Jim", however when you go to the actual snippet, it is listed as

By It's just a nickname on May 20th, 2012 10:05 am

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

As I've said before, let the computer do as much for you as possible. In this case it means TnTinMN's suggestion is clearer and less prone to bugs.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

All you have to do is add a flag so that the first line gets special processing.

        Dim mylist As New SortedList(Of Date, String)
        Dim firstLine As Boolean = True

        For Each line As String In System.IO.File.ReadAllLines("d:\temp\test.txt")
            Dim flds() As String = line.Split(",")
            If firstLine Then
                Label1.Text = line
                firstLine = False
            Else
                mylist.Add(CDate(flds(0)), flds(1))
            End If
        Next

        For Each key As Date In mylist.Keys
            TextBox1.AppendText(key.ToString("dd/MM/yyyy") & vbTab & mylist(key) & vbCrLf)
        Next
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

My.Application.CommandLineArgs

returns an array of strings, one string per argument

Pride commented: Good +0
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

You just have to change the name of the server in the connection string. You may have to get the DBAdmin to set up access rights for you.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
        Dim mylist As New SortedList(Of Date, String)

        For Each line As String In System.IO.File.ReadAllLines("d:\temp\test.txt")
            Dim flds() As String = line.Split(",")
            mylist.Add(CDate(flds(0)), flds(1))
        Next

        For Each key As Date In mylist.Keys
            TextBox1.AppendText(key.ToString("dd/MM/yyyy") & vbTab & mylist(key) & vbCrLf)
        Next

You have to reformat the date for display as shown. If you just use ToString with no arguments you will get " 00:00:00" added to the date.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

lenovo is just in the intial stage in the laptop industry

Lenovo took over IBM's laptop division so to say they are in the initial stage is misleading. They took over a well estqblished line. I have found Dell laptops to be reliable and the service is excellent. I have bought four Dells in the last few years. One was for my older son who used it from graduation through his PhD. He still uses it as his home laptop although he now has an Asus for work. My younger son has an Alienware that he uses for gaming. He bought a lower end Dell for casual use when the hinge on the ThinkPad broke (it still works fine, but the screen is permanently locked open). I have an Inspiron 1720 that I bought in spring 2008. I would not hesitate to buy another Dell.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

He didn't say "the filter criteria does not match any records in the database". He said, "the code is not in the datatable", and an error in conversion from dbnull to string sounds more to me like what I suggested, however, my experience with databases has been at the admin level, and interfacing with ADO which puts a lot fewer layers between my code and the DBMS. In either case one of our suggestions is bound to help.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

I'm a big fan of letting the computer do as much as it can for you. Generally, the less code I have to write, the fewer chances I have to introduce a bug. In that case:

Dim mylist As New SortedList(Of Date, String)

For Each line As String In System.IO.File.ReadAllLines("d:\temp\test.txt")
    Dim flds() As String = line.Split(",")
    mylist.Add(CDate(flds(0)), flds(1))
Next

converts the values from string to Date which can be automatically kept in sorted order by the SortedList class. Once all of the values have been read in you can copy them, in sorted order, to whatever is appropriate - textbox, database, file, etc. Note that this code depends on the system short date format being set to dd/mm/yyyy.

@TnTinMN - I am aware that reading the file in one fell swoop may not be appropriate for exceedingly large files (I can't see this really being a problem - who stores data like the above in multi-gig text files), but for the sake of illustration I think it makes the sample code clearer by focusing on the technique rather than adding exttaneous file i/o interface code.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

@Hani

First you're speaking about what you obviously don't seem to know.

Ad hominem attack - if you can't dispute the facts then attack the person.

Many principles in Islam do not apply at all to other religions.

Can you state a few of these principles so that they can be addressed?

I'm only showing you the facts about Islam that Muslim scholars know and teach.

No. You are stating an opinion and that opinion is not backed by the facts. As lawyers say, res ipsa loquitur. The Qur'an speaks for itself. @Mike has referenced verses in the Qur'an that incite violence against non-believers. I suspect you did not respond to that because no response is possible.

It does not matter what Muslim scholars teach. What is under dispute is the basis for your beliefs. If a mathematician teaches that the value of pi is 3.0 that does not make it so. If all mathematicians teach the same thing it still does not make it so.

When I was in elementary school, each day began with a reading from the Old Testament. It was only after I grew older and read that hate and violence filled book for myself that I found out that the daily readings were carefully picked so as not to frighten. Never having attended a mosque I can only imagine that the same holds true for the Qur'an. Except of course for the extremists.

In order to become a …

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

I was on about how western people a lot convert to Islam

Got any figures to go with that claim ?

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

@Rouf mir

Can anyone please tell me the purpose of life and all this drama in this universe?

No. It's up to you to determine your purpose on your own. If anyone else tries to tell you your purpose, walk away or better yet, run. As far as I am concerned, as good a personal philosophy as any has been proposed by Wil Wheaton. That philosophy is "don't be a dick".

Why was this earth created?

The universe is not concerned with "why". "Why" is a concept created by people. The universe just is. I am more interested in "how" than "why".

Why we were born?

You were born because at some point your parents had intercourse. Don't read anything more into it.

Why so much harderships in the life?

A few billion years of evolution clearly indicates that for most living things, life is not easy. Except for a privileged few, hardship is the norm.

What will happen after death? Where will we be?

Remember what it was like before you were born? Same thing.

It is not thaat 'Hani' is trying to defy his religion. It is the fact that he is sincerely trying to protect us from the fire of hell as per his religion.

I have found that the same people who tell me that my religious beliefs are wrong (in order to save my immortal soul, of course) seem to get very upset …

mike_2000_17 commented: Awesome! +0
stultuske commented: love the neutral and well thought answers, allthough I think the real "Reverend Jim Ignatowski" would have a completely different set of answers :) +0
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

You don't say what database you are using. If it is SQL then you can use the COALESCE function in the query. This function is used to replace NULL values with default values. For example:

select LastName,MiddleName,FirstName from myTable

May return NULL values for MiddleName, however, the query

select LastName,COALESCE(MiddleName,"(none)"),FirstName from myTable

Would return the string "(none)" where no middle name was entered in the database. There is an equivalent function in Access named NZ.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

The easiest way to check is to use a regular expression. Here is an example for your particular requirements:

Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub btnTest_Click(sender As System.Object, e As System.EventArgs) Handles btnTest.Click

        If Regex.IsMatch(txtTestStr.Text, "^[2-9]\d{2}-\d{3}-\d{4}$") Then
            txtResult.Text = "Match"
        Else
            txtResult.Text = "No Match"
        End If

    End Sub

The above sub compares the text in the TextBox, txtTestStr.Text, with the regular expression "^[2-9]\d{2}-\d{3}-\d{4}$" and displays the result in another text box. The regular expression can be broken down as follows

    ^       the start of the string
    [2-9]   any digit in the range 2 to 9
    \d{2}   exactly two other digits
    -       a dash
    \d{3}   exactly three digits
    -       a dash
    \d{4}   exactly four digits
    $       the end of the string

You can find many other useful patterns here

daniel955 commented: nice +1
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Oh and the Council of Nicaea in A.D. 325 did quite a bit -ha, I said bit lol- of re-writing to the bible.

An interesting book on the subject is "Misquoting Jesus: The Story Behind Who Changed the Bible and Why" by Bart D. Ehrman

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

You can use the second form (List). Just replace intSalaries.Length with intSalaries.Count as in

    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

        ' displays the salary amount associated with a code

        Dim intSalaries As New List(Of Integer)

        For Each line As String In System.IO.File.ReadAllLines("D:\temp\test.txt")
            If IsNumeric(line) Then
                intSalaries.Add(CInt(line))
            End If
        Next

        Dim strInputCode As String
        Dim intCode As Integer
        Dim intSub As Integer

        strInputCode = InputBox("Salary code (1 - 6)", "Salary")
        Integer.TryParse(strInputCode, intCode)

        ' subtract 1 from the code to get the appropriate subscript

        intSub = intCode - 1
        If intSub >= 0 AndAlso intSub < intSalaries.Count Then
            lblSalary.Text = intSalaries(intSub).ToString("C2")
        Else
            lblSalary.Text = String.Empty
            MessageBox.Show("The salary code must be from 1 through 6.",
            "Salary", MessageBoxButtons.OK,
            MessageBoxIcon.Information)
        End If

    End Sub

Except you probably don't want to read the values every time the button is clicked. You'd initialize the List once on Form Load.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

I'm not as concerned with him posting religion based material. In Geek's Lounge pretty much any topic is fair game. What concerns me is the posting of misleading, and in some cases, completely inaccurate material. From my reading (which includes sections from the Q'uran) Islam is not a religion of peace and tolerance. While it is true that there have been periods in history where Islam was far more tolerant of other religions than others were of Islam, at its core, Islam is fundamentally an intolerant religion. Have you ever heard of a Jewish suicide bomber? Does any other religion retain its membership by threatening to kill anyone who leaves?

Lest I be considered a rabid anti-Islamist, a thorough reading of the Old Testament shows that Christians can hardly take the moral high ground. The Old Testament is just as filled with hatred and violence as the Q'uran.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Note that this does no error checking - it assumes that all of the strings represent numbers.

        Dim strings() As String = System.IO.File.ReadAllLines("D:\temp\test.txt")
        Dim numbers(UBound(strings)) As Integer

        For i = 0 To UBound(strings)
            numbers(i) = CInt(strings(i))
        Next

A version that checks but does not strictly speaking use an array is

        Dim numbers As New List(Of Integer)

        For Each line As String In System.IO.File.ReadAllLines("D:\temp\test.txt")
            If IsNumeric(line) Then
                numbers.Add(CInt(line))
            End If
        Next
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

I think the easiest way would be to create your own msgbox form. Make sure it has a fixed width font like Courier New and display is as Modal.

alexandra.lopez.94043 commented: how do I create a message box form sir? +0
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Law and order are everywhere the law and order which protect the established hierarchy.

Herbert Marcuse

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

For once I find myself in complete agreement with Rashakil. If you are looking to better your job prospects then take a programming job, any job, that will get you experience. Programming in University is nothing like programming in the real world. In my opinion (and it is just my opinion) a Masters is only a stepping stone on the way to a PhD. I stuck around for a year after graduation to take a few more courses, partly because there were some courses I really wanted to take and partly because I wanted to stay in the University environment. Most of my friends were staying on to do a Masters. All continued on to get their doctorates. Three are now professors. I did not enter the Master's program.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Or even

TextBox.Visible = Ctype(sender,CheckBox).Checked
Begginnerdev commented: Nice, I have never used that method. Good to see a shorter, more effecient way of doing it. +5
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Laws are spider webs through which the big flies pass and the little ones get caught.

Honore de Balzac

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

How do you propose to click a button in a form that has not yet been opened? And why would you want to?

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

At 6:27 I tried the non-responsive thread again and it came up.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Web Site Not Responding

I was trying to go to a thread this morning (06:20 Winnipeg time) and DaniWeb wouldn't display it. All I got was

The connection to the server was reset while the page was loading.

I tried several times with the same result. In order to determine if there was a problem with DaniWeb (or my ISP), while waiting for this thread to come up, I requested, in other tabs, both this thread and this thread, both of which were displayed quickly.

Because I have been having ISP problems I was running a monitor in the background which showed that at the time I had continuous connections to my hub, my DNS and www.daniweb.com.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

"Something like" code can't be debugged. What I can do with what you have shown is offer some code that does what I think you want. The following scans the "My Documents" folder on my D: drive and colours the lines based on the age (in months) if the files.

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        For Each file As String In My.Computer.FileSystem.GetFiles("D:\My Documents")

            Dim filepart As String = System.IO.Path.GetFileName(file)
            Dim filedate As Date = My.Computer.FileSystem.GetFileInfo(file).LastWriteTime

            Dim item As New ListViewItem(filepart)

            Select Case DateDiff(DateInterval.Month, filedate, Now)
                Case Is < 10
                    item.BackColor = Color.LightGreen
                Case Is < 20
                    item.BackColor = Color.LightCyan
                Case Is < 50
                    item.BackColor = Color.LightPink
                Case Else
                    item.BackColor = Color.LightSalmon
            End Select

            ListView1.Items.Add(item)

        Next

    End Sub

End Class
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

I have no idea what "phBAH.phTabControl" is. Start a new (blank) project. Add a few textboxes and a few other controls at random. Don't bother setting any properties or names other than the defaults. Now replace the existing code with the following:

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        For Each ctrl As TextBox In Me.Controls.OfType(Of TextBox)()
            AddHandler ctrl.GotFocus, AddressOf TextBox_GotFocus
            AddHandler ctrl.LostFocus, AddressOf TextBox_LostFocus
            ctrl.Tag = ctrl.BackColor
        Next

    End Sub

    Private Sub TextBox_GotFocus(sender As Object, e As System.EventArgs)
        Dim txt As TextBox = sender
        txt.BackColor = Color.LightGray
    End Sub

    Private Sub TextBox_LostFocus(sender As Object, e As System.EventArgs)
        Dim txt As TextBox = sender
        txt.BackColor = txt.Tag
    End Sub

End Class

After consideration I thought it was better to have separate handlers for "got" and "lost" focus. Just my opinion. In any case, try the above code. You can even set the text boxes to have different background colours at design time. The starting colour will be restored after the control loses focus.

iFrolox commented: Works :) +0
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Or a little cleaner

Private Sub FocusChanged(sender As Object, e As EventArgs)

    If CType(sender, TextBox).Focused Then
        CType(sender, TextBox).BackColor = Color.FromArgb(65, 65, 65)
    Else
        CType(sender, TextBox).BackColor = Color.FromArgb(60, 60, 60)
    End If

End Sub

or

Private Sub FocusChanged(sender As Object, e As EventArgs)

    If sender.Focused Then
        sender.Tag = sender.BackColor
        sender.BackColor = Color.LightGray
    Else
        sender.BackColor = sender.Tag
    End If

End Sub    

Which saves the current background colour before setting the focused colour (useful if not all of your textboxes are the same colour). If you are a purist you can do

Private Sub FocusChanged(sender As Object, e As EventArgs)

    Dim txt As TextBox = sender

    If txt.Focused Then
        txt.Tag = txt.BackColor
        txt.BackColor = Color.LightGray
    Else
        txt.BackColor = txt.Tag
    End If

End Sub
Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Home made cinnamon bun and a cup of coffee. I'm laying in bed listening to a thunderstorm and a much needed rainshower at the lake.

Reverend Jim 5,225 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

If you want to handle all textboxes the same way you can create two generic handlers such as

    Private Sub TextBox_GotFocus(sender As Object, e As System.EventArgs)
        sender.BackColor = Color.LightGray
    End Sub

    Private Sub TextBox_LostFocus(sender As Object, e As System.EventArgs)
        sender.BackColor = Color.White
    End Sub

then on form load you can attach these handlers to every textbox as

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        For Each ctrl As TextBox In Me.Controls.OfType(Of TextBox)()
            AddHandler ctrl.GotFocus, AddressOf TextBox_GotFocus
            AddHandler ctrl.LostFocus, AddressOf TextBox_LostFocus
        Next

    End Sub
Begginnerdev commented: I laugh every time I see your picture. +5
iFrolox commented: Thats what i needed +0