Skip to main content
5 of 5
deleted 39 characters in body; edited title
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Modified TextBox for forms generating SQL statements

An overview of what I've done:

I've inherited System.Windows.Forms.TextBox and added a few properties to help me out in creating forms that generate SQL statements.

I use to create a large function that would check for changes in the TextBox compared to a string. Then it would take text and concatenate it to a SQL statement. Now it's simple and easier to use.

I've added four properties:

  • A String property to hold the default string that the textbox is initially set to and will reset back if the text is left empty.
  • A String property to hold the text's associated SQL statement.
  • A Boolean to check if the text has changed from the default.
  • Finally, an Integer to hold an ID if there is a reason to need one, such as saving and loading text from a file by the ID.

I have not fully implemented the new textbox in my code, but I am working on it.

It may only be slightly modified, but it should cut down my code from 500+ lines of code down to less than +-30 if ran in a for-loop.

SQLTextBox.vb

Public Class ModifiedTextBox
    Inherits System.Windows.Forms.TextBox

    Private _strDefaultText As String
    Private _strSqlText As String
    Private _nID As Integer
    Private _bModified As Boolean = False

    Property ID()
        Set(nID)
            _nID = nID
        End Set
        Get
            Return _nID
        End Get
    End Property

    Property TextModified()
        Set(bModified)
            _bModified = bModified
        End Set
        Get
            Return _bModified
        End Get
    End Property

    Property SqlText()
        Set(strSqlText)
            _strSqlText = strSqlText
        End Set
        Get
            Return _strSqlText
        End Get
    End Property

    Property DefaultText()
        Set(strDefaultText)
            _strDefaultText = strDefaultText
            Me.Text = _strDefaultText
        End Set
        Get
            Return _strDefaultText
        End Get
    End Property

End Class

I've pulled an excerpt from my code. It's not much, but here is the new textbox vs the old one.

In the SQL text, I have something like ""Material Type"" in('INSERTTEXT'). SQLTextBox.Modified gets set when the user leaves the textbox (set to false b default).

'This should run every new textbox (untested)
For i As Integer = 0 To Me._icControls.txtMain.Length - 1
    If Me._icControls.txtMain(i).Modified = True Then
        sqlWhere += " AND " + Me._icControls.txtMain(i).SqlText
        Replace(sqlWhere, "INSERTTEXT", Me._icControls.txtMain(i).Text)
    End If
Next

'Old, long way around where I had split up
'the textboxes into 7 groups they belonged too.
If Me._icControls.txtMaterial(0).Text.ToString() <> DefaultStrings.Material(0) Then
    sqlWhere += " AND ""Material Type"" in('" + Me._icControls.txtMaterial(0).Text.ToString() + "')"
End If

If Me._icControls.txtMaterial(4).Text.ToString() <> DefaultStrings.Material(4) Then
    sqlWhere += " AND ""Grade"" in('" + Me._icControls.txtMaterial(4).Text.ToString() + "')"
End If

If Me._icControls.txtMaterial(5).Text.ToString() <> DefaultStrings.Material(5) Then
    sqlWhere += " AND ""PIW"" in('" + Me._icControls.txtMaterial(5).Text.ToString() + "')"
End If

I went from 200 lines of code to 6, with a minor change. That does not include the lines spent setting up the default text.

Let me know what you think of this. I'm not sure if it's really anything someone could use, but it's been very useful to me.

CodeMonkey
  • 451
  • 1
  • 4
  • 12