Best way to refresh a textbox

Chong

Regular
Joined
Apr 4, 2003
Messages
79
I need help on refreshing a textbox. I have updated the database with a SQL query and re-run the routine for creating the textbox and querying the information back to the textbox.text and on debug mode, it showed that the querying data are the most recent. However, when it is actually display on the form, the data hasn't refresh or updated. If I close the form and clear the panel and come back again then the textbox is refreshed and show the most recent changes.

Many thanks in advance!

Chong
 
RE:

I'm assuming the textbox is not bound to the data. Can you post the code you have for re-building your textbox?
If you are not re-querying the database for the new data then the textbox should show the old data as the name of the textbox and all of its contents are probably lingering in memory. You could dispose the textbox once the query executes then rebuild it .
 
Here's my code:

Visual Basic:
    Public Sub ScholarshipLoad()
        'Dim txtResult As New TextBox()
        Dim strSelected As String = lstApplicant.Items(lstApplicant.SelectedIndex).ToString()
        Dim plcHolder, strStanding, strSndStanding As String
        Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbPath
        Dim strSQL As String = "SELECT * FROM Scholarship WHERE Scholarship.Scholarship=" & "'" & strSelected & "'"
        Dim shlDataAdapter As New OleDbDataAdapter(strSQL, strConn)
        shlDataAdapter.TableMappings.Add("Table", "Scholarship")
        Dim schlDataSet As New DataSet()
        shlDataAdapter.Fill(schlDataSet)
        Dim schlRow As DataRow
        Dim intScholarAmount As Integer
        Dim tblScholar As DataTable = schlDataSet.Tables(0)
        For Each schlRow In tblScholar.Rows
            strStanding = schlRow("Standing")
            strSndStanding = schlRow("sndstanding")
            If strStanding = "N" Then
                strStanding = "Not Require"
            End If
            If strSndStanding = "N" Then
                strSndStanding = "Not Require"
            ElseIf strSndStanding = "Above" Then
                strSndStanding = "Senior"
            End If
            If IsDBNull(schlRow("AmntAwarded")) = False Then
                intScholarshipAmount = CInt(schlRow("AmntAwarded"))
                intScholarAmount = CInt(schlRow("Amount")) - CInt(schlRow("AmntAwarded"))
            Else
                intScholarAmount = schlRow("Amount")
            End If
            plcHolder = "Require GPA: " & schlRow("GPA") & vbCrLf & "Require Major: " & schlRow("Major") & vbCrLf & "Must be: " & _
                        strStanding & " to: " & strSndStanding & vbCrLf & "Total Amount: $" & intScholarAmount

        Next schlRow
        mtplResult(plcHolder)

    End Sub

    Public Sub mtplResult(ByRef plcHolder As String)
        Dim txtResult As New TextBox()
        Dim txtAward As New TextBox()
        Dim lblAmount As New Label()
        Dim strTextBoxInc As String
        Dim strTextBoxResult As String

        x = 8
        n += 1

        If blnSchlTF = True And blnQlfy = False Then
            h = 60 'from 50 to 60
            x = 8
            y = 8
            strTextBoxResult = "txtBoxScholarship"
        ElseIf blnSchlTF = True And blnQlfy = True Then
            If h = 60 AndAlso y = 8 Then
                h = 165
                x = 8
                y = 90 'from 80 to 90
            Else
                y += 175
            End If
            strTextBoxResult = "txtQualifiedApplicant"
        End If
        pnlAppResult.SuspendLayout()
        With txtResult
            .Name = strTextBoxResult
            .Width = 264
            .Height = h
            .AllowDrop = True
            .Location = New Point(x, y)
            .AutoSize = True
            .Multiline = True
            .ScrollBars = ScrollBars.Vertical
            .Text = plcHolder
            .BackColor = System.Drawing.Color.White
            .ReadOnly = True
            .Visible = True
            .Show()
        End With
        AddHandler txtResult.DragEnter, AddressOf txtResult_DragEnter
        AddHandler txtResult.DragDrop, AddressOf txtResult_DragDrop
        pnlAppResult.Controls.Add(txtResult)
        pnlAppResult.ResumeLayout(True)

    End Sub

And here is the routine for updating and calling the ScholarshipLoad() to re-display the textbox.name = "txtBoxScholarship" again and refresh this textbox.

Visual Basic:
    Private Sub txtResult_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txtResult.DragDrop
        popAmount()
        Dim strHolder As String
        Dim strName As String
        Dim strSSNsubstring As String
        Dim strSeperator As String
        Dim tb As TextBox = CType(sender, TextBox)
        Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbPath
        Dim strSQL, strSQLInsert As String
        Dim rdr As OleDbDataReader
        Dim cn As New OleDbConnection(strConn)
        Dim cmd As OleDbCommand = cn.CreateCommand()
        Dim intAmountLeft As Integer
        Dim strSelected As String = lstApplicant.Items(lstApplicant.SelectedIndex).ToString()

        strName = tb.Text()
        strSSNsubstring = strName.Substring(5, 9)
        intSSNnumb = CInt(strSSNsubstring)
        strSeperator = "*************************************************"

        'strHolder = e.Data.GetData(lstApplicant.Text().GetType.ToString())
        strHolder = lstApplicant.Items(lstApplicant.SelectedIndex).ToString()

        tb.Text = strName & vbCrLf & strSeperator & vbCrLf & "Awarded: " & strHolder & " scholarship" & _
                  vbCrLf & "SSN#: " & intSSNnumb & vbCrLf & "Amount Award: $" & strAmount
        cn.Open()
        strSQL = "SELECT COUNT(*) FROM Awarded WHERE awrdID=" & intSSNnumb
        Dim cmdSQL As New OleDbCommand(strSQL, cn)

        Try
            Dim count As Integer = cmdSQL.ExecuteScalar
            If count > 0 Then
                'records found so update
                cmd.CommandText = "UPDATE Awarded SET awrdName=" & "'" & strHolder & "'" & " awrdAmount =" & strAmount & " WHERE awrdID=" & intSSNnumb
                cmd.ExecuteNonQuery()
            Else
                'records not found so add to database
                strSQLInsert = "INSERT INTO Awarded (awrdID, awrdName, awrdAmount) VALUES(" & intSSNnumb & ", '" & strHolder & "', '" & strAmount & "')"
                Dim cmdQuery As New OleDbCommand(strSQLInsert, cn)
                cmdQuery.ExecuteNonQuery()
            End If
            intAmountLeft = intScholarshipAmount + CInt(strAmount)
            'intAmountLeft = CInt(strAmount)
            If intAmountLeft > 0 Then
                cmd.CommandText = "UPDATE Scholarship SET AmntAwarded=" & intAmountLeft & " WHERE Scholarship LIKE " & "'" & strSelected & "'"
                cmd.ExecuteNonQuery()
                'blnSchlTF = True
                blnQlfy = False
                'txtResult.Clear()
                ScholarshipLoad()
                blnQlfy = True
            End If
        Catch dbException As Exception
            MessageBox.Show(dbException.Message)
        End Try
        cn.Close()
    End Sub

Thanks for trying to help!

Chong

edited: Used [ vb ] tags instead of [ quote ]
 
Last edited by a moderator:
RE:

ok i see it here
Code:
blnQlfy = False
'txtResult.Clear()
ScholarshipLoad()
then it jumps back to the sub scholarshipload() without removing the textbox from memory. So when you DIM it again the values are still there.

try somthing like:
Code:
if txtResult.text >"" then txtResult.Dispose
ScholarshipLoad()
 
This is the error I received after I do the if if txtResult.text >"" then txtResult.Dispose

Error:
Object reference not set to an instance of an object.

Would it make any difference that there are multiple instances of the textbox txtResult? As you look at the script, the first time the code was called, the txtResult textbox has a txtResult.Name="txtBoxScholarship" and the rest of the instances of the txtResult textbox have the name of "txtQualifiedApplicant" because the first IF statement:

If blnSchlTF = True And blnQlfy = False Then
h = 60 'from 50 to 60
x = 8
y = 8
strTextBoxResult = "txtBoxScholarship"
is true but after the txtResult textbox is create with the above And the rest of the h,x,y specification, the following IF statement is true.

ElseIf blnSchlTF = True And blnQlfy = True Then
If h = 60 AndAlso y = 8 Then
h = 165
x = 8
y = 90 'from 80 to 90
Else
y += 175
End If
strTextBoxResult = "txtQualifiedApplicant"
End If

I think that the best way is to find the txtResult with the name txtBoxScholarship and only work with this text box. The problem is I don't know how to find or the call the txtResult with the name txtBoxScholarship back because the txtResult.Name-"txtBoxScholarship" has been replaced by txtResult.Name="txtQualifiedApplicant" with the additional creation of the textbox.

Chong
 
RE:

I didnt see where the name of the textbox was being changed in the code.
You could trap the name of txtResult and dispose the whole textbox that way since you wont be certain what the name is exactly. Just an idea

i tried it in a button click event here

Code:
Public str_b As String ' store the textbox name

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

        Dim txt_box as New Textbox
          str_b = txtResult.Name()
       txt_box.Name = str_b
       txt_box.dispose
       End Sub

then call your sub and clear the textbox right after it is built

Code:
 txtResult.ResetText()
I only tried this in debug mode so im not positive it will produce the desired result but there was no value in that textbox afterwards
 
Back
Top