none
Label displaying Message. RRS feed

  • Question

  • Can you someone help me? I am trying to display messages using a label but i want the message to update every time i click a button (Next or Previous button). I was thinking about using Switch Case However, it is not quite working. All i want to is on a single windows form display messages using a previous and next button. These message will be in a form of tree or flowchart. What that means is all the message are related and there will some yes or no answer in order to proceed to the next level.  If someone have an idea on how to approach this please let me know I would much appreciate it. Thank you
    Monday, December 18, 2017 9:30 PM

All replies

  • I am trying to display messages using a label but i want the message to update every time i click a button (Next or Previous button).

    I am assuming you know that
       Label1.Text = "Message"

    included  in the button click event handler method would do what you have described. So the problem must be in "all the message are related and there will some yes or no answer in order to proceed to the next level" which implies that not only does the message change, but there must be some change in the application logic to allow/prevent some further action.   But the information you have provided is far too vague to allow useful advice.  Take one part of the problem only and provide full details for what you need to do, what you have already tried, and why it didn't work.

    • Proposed as answer by Frank L. Smith Monday, December 18, 2017 10:58 PM
    Monday, December 18, 2017 9:41 PM
  • The following requires a label and two buttons. The list is hard coded but you could load messages say from a database table or perhaps a text file as a List(Of LabelMessage) there is a Add method.

    Place this class in a class file. This will store messages, Id field is optional.

    Public Class LabelMessage
        Public Property Id As Integer
        Public Property Text As String
    End Class

    Form code

    Public Class Form1
        Private bsMessages As New BindingSource
        Private Sub cmdPrior_Click(sender As Object, e As EventArgs) Handles cmdPrior.Click
            bsMessages.MovePrevious()
        End Sub
        Private Sub cmdNext_Click(sender As Object, e As EventArgs) Handles cmdNext.Click
            bsMessages.MoveNext()
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            bsMessages.DataSource = New List(Of LabelMessage) From
                {
                    New LabelMessage With {.Text = "Apple iPhone"},
                    New LabelMessage With {.Text = "Dell laptop"},
                    New LabelMessage With {.Text = "Another message"},
                    New LabelMessage With {.Text = "Getting near the end"},
                    New LabelMessage With {.Text = "The end"}
                }
    
            Label1.DataBindings.Add("Text", bsMessages, "Text")
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Frank L. Smith Monday, December 18, 2017 10:59 PM
    Monday, December 18, 2017 10:03 PM
    Moderator
  • Karen,

    I see what you're doing there. I had to think about it some. ;-)

    I'm not sure the OP will follow that but it's a good answer.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Monday, December 18, 2017 10:59 PM
  • Karen,

    I see what you're doing there. I had to think about it some. ;-)

    I'm not sure the OP will follow that but it's a good answer.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    It's been a busy day else I would had expanded on the 'why', hopefully I get a chance tonight to edit and explain in simple terms what I did. Thanks for bringing this up.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, December 18, 2017 11:48 PM
    Moderator
  • Okay, you can download and try the full project here.

    In the project I load messages from a text file e.g.

    This is the first message.
    Second line
    The third line
    This was done in VB.NET
    Using Visual Studio 2015
    The last line

    Our class is still the same

    Public Class LabelMessage
        Public Property Id As Integer
        Public Property Text As String
    End Class

    The class responsible for loading the text file

    Public Class Operations
        ''' <summary>
        ''' Read in Messages.txt which resides in the same folder
        ''' as the project executable file lives
        ''' </summary>
        ''' <returns></returns>
        Public Function Read() As List(Of LabelMessage)
            ' file to read
            Dim fileName As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Messages.txt")
    
            ' first part reads in the entire file
            ' Next we create an anonymous type to get the line and line number
            ' This is followed by excluding blank lines
            ' Finally create the list (note +1 on Index as it's 0 based and we want 1 based)
            Return IO.File.ReadAllLines(fileName) _
                .Select(Function(line, index) New With {.Line = line, .Index = index}) _
                .Where(Function(item) Not String.IsNullOrWhiteSpace(item.Line)) _
                .Select(Function(item)
                            Return New LabelMessage With {.Id = item.Index + 1, .Text = item.Line}
                        End Function).ToList
    
        End Function
    End Class
    

    Form code with plenty of comments.

    ''' <summary>
    ''' * Form Load
    '''    - Create an instance of the Operation class which has one method
    '''      to read a normal text file and place the lines into a List of
    '''      LabelMessage and assign it to a BindingSource component which
    '''      natively provides the capability to move next, prior, first and
    '''      last through our messages.
    '''      
    '''     - Use a BindingNavigator to traverse the messages where the
    '''       BindingNavigator component "knows" how to access the methods
    '''       to traverse them.
    ''' </summary>
    Public Class Form1
        Private bsMessages As New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ops As New Operations
            bsMessages.DataSource = ops.Read
            Label1.DataBindings.Add("Text", bsMessages, "Text")
            '
            ' If you don't like the BindingNavigator you can place buttons 
            ' on the form as I did in my first reply to manually traverse messages
            ' for example, to move next bsMessages.MoveNext or prior 
            ' bsMessages.MovePrevious
            '
            BindingNavigator1.BindingSource = bsMessages
    
        End Sub
    End Class
    

    This time instead of buttons I used a BindingNavigator, placed it on the form, single clicked on each button I didn't want then pressed delete so the form now looks like this (you could still use regular buttons as I have comments in the code for that).

    At runtime note several buttons are disabled, the BindingNavigator "knows" what position it's at and will disable buttons that would do nothing. If you don't like the images they can be replaced.

    Example for replacing the image (and you could just use text w/o and image)

    Lastly, the magic for showing the messages is in the following line which binds the BindingSource component to the label and to show the Text property of the current LabelMessage which is in the List(Of LabelMessage) which is the DataSource of the BindingSource component bsMessages.

    Label1.DataBindings.Add("Text", bsMessages, "Text")
    Hope this is all clear and useful,


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, December 19, 2017 12:54 AM
    Moderator
  • Hi,

    Would you provide your code, especially related with [Next] or [Previous] button.Click event. That will help us who want to assist you.

    Regards,

    Ashidacchi

    Tuesday, December 19, 2017 3:27 AM
  • Hey Frank I know about label1.text ="Message". However, what i am trying to build is a form that will display multiple message. It is like a road map where you click next to see the next step and back to go back to the previous message. I will post what the form looks like and the code i have so far. Let's say you want to provide instructions on how to trouble shoot an electronic device, and you want to display all the steps on the form using a label and the buttons. Each time you click Next it will show you the next step in the process. below i will demonstrate an example: here is a Phone you want to troubleshoot:

    Step 1: " Check if the device is powered?"

    step 2 : " Push and hold the power button for 5 second"

    Step 3 : "   whatever the step is " 

    ....... 

    Step n : " Your troubleshoot has been successful" 

    All these steps needs to appear on the label and you click Next or Back.

    This is the code i have so far. My work is sloppy. Please ignore some of these variables i declared. 

    Public Class Form9
        Dim clock As Char
        Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo
        Dim Result As DialogResult
        Dim message As String
        'Static CaseNumber As Integer 
    
    
    
    
        '//btnNo and btnYes
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles BtnNO.Click, BtnYES.Click
            If Result = DialogResult.Yes Then
                LblDisplay.Text = LblDisplay.Text & "," & Result
            End If
    
            If Result = DialogResult.No Then
                LblDisplay.Text = "  Is there any dsiplay on the device? " + Environment.NewLine + "Tell me what you are seeing on this device?"
            End If
            Select Case LblDisplay.Text
                Case 1 = DialogResult.No
                    LblDisplay.Text = "Now Check the connection in the back of the device"
                Case 2 = DialogResult.Yes
                    LblDisplay.Text = "What is currently being dipslayed on the device?"
            End Select
        End Sub
        '// btnNext     
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles BtnNext.Click
            Static CaseNumber As Integer
            If Not CaseNumber = LblDisplay.Text.Count - 1 Then
                CaseNumber += 1
            Else CaseNumber = 0
                    
    
            End If
    
            LblDisplay.Text = " Is the Power on? "
            If message = LblDisplay.Text Then
                message = " Clock is powered on. "
            ElseIf message <> LblDisplay.Text Then
                message = " Clock is not powered on."
            End If
        End Sub
    
        Private Sub Form9_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            '//set size of the label
            'LblDisplay.SizeMode = LblDisplay.SizeMode.Strechlabel
            BtnBack.Text = "Back" : BtnNext.Text = "Next"
        End Sub
        '//btnBack or btnPrevious
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles BtnBack.Click
            Static CaseNumber As Integer
            If Not CaseNumber = 0 Then CaseNumber -= 1 Else CaseNumber = LblDisplay.Text.Count
    
    
    
        End Sub
        '// LblDisplay
        Private Sub LblDisplay_Click(sender As Object, e As EventArgs) Handles LblDisplayClockName.Click, LblDisplay.Click
            LblDisplayClockName.Text = "Clock Name   " & clock & "."
        End Sub
        Private Sub setLabel()
            ' LblDisplay.Text = LblDisplay.Text(CaseNumber)
        End Sub
    
    End Class


    AN

    Wednesday, December 20, 2017 10:15 PM
  • GMQ,

    You didn't mention that to start with (or I missed it), but it looks like you want to set up the logic to be based on branching from the previous input then?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, December 20, 2017 10:17 PM
  • Hey Ashidacchi,

    I posted the code above in my response to Frank. i also detail what i am wanting to do. 

    Regards 


    AN

    Wednesday, December 20, 2017 10:19 PM
  • Hey Karen,

    Thank you for answer, I am still trying to figure out what you did here But most of it make sense. I guess my problem was i was trying to hard code the messages in my code instead of just creating a text file or data and then load them as you did. I am evaluating your code and i will let know pretty soon. Thank so much

    Regards 


    AN

    Wednesday, December 20, 2017 10:31 PM
  • It is like a road map where you click next to see the next step and back to go back to the previous message.

    You need to start with the data. Once you have that, then the logic follows.  It doesn't matter what the source of the data is (hard coded, database, text file, etc) but it does matter how you have it organised in your application.  The logic needs to be able to follow the 'path' through the data in the same way that the user will follow a 'path' by choosing Yes or No. In computing terms, it's a tree.

    You can construct a tree using a class that you define.  Each instance of the class will have properties such as some text, a Yes branch and a No branch. The Yes and No branches will be other instances of the class.  It's easy to sea how this creates a tree with multiple paths, the actual path determined by the Yes/No answers.  Because the objects are a class that you define, you can include special types - for instance, the end of the path might be "There are no more choices available - here is a general description that might help" and Nothing for the Yes and No branches to indicate that there are no more questions.  To backtrack you need to maintain a trail of 'breadcrumbs' - a queue is designed for that. Or, you could have a 'Previous' property in the class which is populated with the instance that the user is coming from just before the class information is displayed.

    With this design you shift the work from the user interface to the construction of the data structure. The complexity of that process depends in part of where your data is coming from.   But once it's done, the user interface portion becomes trivial. Breaking the task into two parts like this makes it simpler overall, and more general.

    Wednesday, December 20, 2017 11:37 PM
  • Hey Karen,

    Thank you for answer, I am still trying to figure out what you did here But most of it make sense. I guess my problem was i was trying to hard code the messages in my code instead of just creating a text file or data and then load them as you did. I am evaluating your code and i will let know pretty soon. Thank so much

    Regards 


    AN

    My first reply shows how to hard code the messages


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, December 20, 2017 11:45 PM
    Moderator
  • With this design you shift the work from the user interface to the construction of the data structure. The complexity of that process depends in part of where your data is coming from.   But once it's done, the user interface portion becomes trivial. Breaking the task into two parts like this makes it simpler overall, and more general.

    I agree. It would be quite a lot to do and it would have to flexible -- the latter being the biggest challenge.

    *****

    AN,

    If you want to let's get started on this then let me know and I'll help where I can.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, December 20, 2017 11:46 PM