Oliver Kucharzewski
Oliver Kucharzewski

Reputation: 2655

Labels & Text wont update accordingly

I developed an application which is like a digital comprehension sheet activity.

The order of which this works is:

  1. A user double clicks an underscored text area on a text box
  2. A form opens which asks them to place an answer
  3. The answer is validated, if true it goes back to the original form

The issue here is that for some odd reason my labels don't update on step 3, as- well as the text. I have a label named lbAnswerStatus which notifies the user if they have a correct answer, the text does not update, and the 'SelectedText' on Form 1 should be replaced with the answer (if correct)

Here is my code:

Form 1 (when textbox is clicked):

   Form2.Answer(answers(counter))

answers(counter) represents the correct answer being passed on, to compare with users answer in form 2

Form 2:

   If tbAnswer.Text = theanswer Then
   Form1.answerStatus(True, theanswer)

Form 1:

Public Sub answerStatus(status, answer)
    If status = true Then
        Form2.Close()
        rtb.SelectedText = answer
        lbAnswerStatus.ForeColor = Color.Green
    End If
End Sub

My first assumption was that the Rich text box's SelectedText wasn't changing because it didn't have focus on it however, the lbAnswerStatus color didn't change either, so I figured that there was issues with making modifications to the form.

I used a message box to test whether lbAnswerStatus.Text would pop up and it did, so it can read but not write.

I also attempted to change the text of the label and selectedtext of the textbox in step 1 and it worked fine.

Any ideas what may be causing this issue? Thanks.

Upvotes: 1

Views: 194

Answers (1)

Andrew Morton
Andrew Morton

Reputation: 25057

I guess that you want your Form2 (AnswerForm) presented as a modal dialog. Doing that, you can return a result. You didn't say what you want to happen to the answer status label or the answer form if the supplied answer is incorrect.

Just as an example of how it can be done, create a new Windows Forms project. On Form1, place a button (Button1) and a label ("lblAnswerStatus"). Add a new form named "AnswerForm" to the project, and add a TextBox ("TextBox1") and a button ("bnDone").

As code for AnswerForm:

Public Class AnswerForm

    Private statusLabel As Label
    Private answerText As String

    Private Sub bnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnDone.Click

        If TextBox1.Text.Equals(answerText, StringComparison.CurrentCultureIgnoreCase) Then
            ' Alternative 1: set the color here if you want to
            statusLabel.ForeColor = Color.Green

            ' return a value indicating success
            Me.DialogResult = Windows.Forms.DialogResult.Yes
            Me.Close()
        Else
            ' indicate error
            statusLabel.ForeColor = Color.Red
        End If

    End Sub

    Public Sub New(ByVal statusLabel As Label, ByVal answerText As String)
        InitializeComponent()
        Me.statusLabel = statusLabel
        Me.answerText = answerText

    End Sub

End Class

and as code for Form1:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        lblAnswerStatus.ForeColor = Color.Blue

        Using answerFrm As New AnswerForm(lblAnswerStatus, "X")
            Dim dlgResult = answerFrm.ShowDialog()

            ' Alternative 2: use this if you want to change the color in this handler
            If dlgResult = DialogResult.Yes Then
                lblAnswerStatus.ForeColor = Color.Purple
            End If

        End Using

    End Sub

End Class

If you click Button1 on Form1, a dialog box appears. Type into its TextBox1 and click bnDone. Because the instance of AnswerForm has a reference to lblAnswerStatus (supplied in its constructor), it is easy to update the latter, e.g. it turns red if you enter the wrong answer.

Upvotes: 3

Related Questions