Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String longer than viewable area of ToolStripStatusLabel 1

Status
Not open for further replies.

ousoonerjoe

Programmer
Jun 12, 2007
925
US
Using VB.Net 2005
StatusStrip with ToolStripStatusLabel

We use the StatusStrip to display current progress of the form as well as error messages that typically pertain to data entry errors or data constraint errors. The users screen size varies from 800 x 600 to 1440 x 900, so simply choosing shorter messages will not always be an option.

Is there a way to make the ToolStripStatusLabel AutoEllipse like a standard Label or is this something that would have to be manually calculated? If so, any recommendations on how to find the space between words prior to end of the viewable area so that a vbCrLf or "..." can be inserted?

Thank you.

--------------------------------------------------
Bluto: What? Over? Did you say "over"? Nothing is over until we decide it is! Was it over when the Germans bombed Pearl Harbor? No!
Otter: Germans?
Boon: Forget it, he's rolling.
--------------------------------------------------
 
I've never need the ellipses so I can't totally answer your question other than to say I don't know of a way to have it AutoEllipse. However you can make just about any control a StatusStrip Control. I would suggest trying this faq796-7149 and just use a lable instead. It should work. I still use a version of that updown control and use it for a custom progress bar. I don't know an easy way to calculate what you want. My first though is you could split the text by the blanks in to an array and then add up the length of each of the words and if it exceeds a number then truncate there. If you are unsure when to stop it then you will need to measure the words and spaces with something like what I do.

Code:
                Dim TotalErrorMessage As String = "Error Message failed with: " & EmailErrorMessage.ErrorMessage
                Dim strSize As System.Drawing.SizeF = Me.CreateGraphics.MeasureString(TotalErrorMessage, StatusStrip1.Font, StatusStrip1.ClientSize.Width)

                If strSize.Width <= StatusStrip1.Width - 20 Then
                    sMessage(TotalErrorMessage)
                Else
                    ttMessage("More...", TotalErrorMessage)
                End If
sMessage just sends it to the ToolStripStatusLable and ttMessage instead create a ToolTip that it attaches to the ToolStripStatusLable. Not the best solution, but I prefer an all or nothing approach.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorwen, thanks for the kick in the right direction.
Code:
    Private Sub txtMsg_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles txtMsg.Paint
        Dim TotalErrorMessage As String = "No Assembly Lines have been created for Division " & cmbDivision.Text & ". One must be created before any jobs can be assigned to it."
        Dim strSize As System.Drawing.SizeF = Me.CreateGraphics.MeasureString(TotalErrorMessage, txtMsg.Font, txtMsg.Width)
        Dim DispSize As System.Drawing.SizeF = Nothing
        Dim Str() As String
        Dim Display As String = ""
        Dim x As Integer = 0
        Try
            Str = TotalErrorMessage.Split(" ")
            For x = 0 To (UBound(Str) - 1) Step 1
                strSize = New System.Drawing.SizeF(Me.CreateGraphics.MeasureString(Str(x), txtMsg.Font, txtMsg.Width))
                DispSize = New System.Drawing.SizeF(Me.CreateGraphics.MeasureString(Display, txtMsg.Font, txtMsg.Width))
                If (strSize.Width + DispSize.Width) < (txtMsg.Width) Then
                    Display = Display & " " & Str(x)
                Else
                    Display = Display & " ..."
                    Exit Try
                End If
            Next
        Catch ex As Exception
            txtMsg.Text = "Failed. Fix it."
        Finally
            txtMsg.Text = Display
        End Try
    End Sub

It still has a few rough edges to iron out, but you can see the direction I'm headed.

Note: txtMsg is a ToolStripStatusLabel

--------------------------------------------------
Bluto: What? Over? Did you say "over"? Nothing is over until we decide it is! Was it over when the Germans bombed Pearl Harbor? No!
Otter: Germans?
Boon: Forget it, he's rolling.
--------------------------------------------------
 
You really don't want to use the paint event for this. If you need to use an event I would suggest at least using the TextChanged event instead. You shouldn't need to run that bit of code each time the control is painted. Also for some future proofing I would suggest a delegate approach. This allows if you start using threading, want to call tsslMessage from another window, or basically any instance where you could cause a cross thread issue.
Code:
    Private Sub TestMessage() [green]'Just to illustrate, but you would place the line at some time in your code.[/green]
        Dim TotalMessage As String = "No Assembly Lines have been created for Division " & _
                                     cmbDivision.Text & ". One must be created before any jobs can be assigned to it."

        tsslMessage(TotalMessage)
    End Sub

    Public Sub tsslMessage(ByVal TextValue As String)
        If Me.InvokeRequired Then
            Me.BeginInvoke(New delTextToTssl(AddressOf TextToTssl), TextValue)
        Else
            TextToTssl(TextValue)
        End If
    End Sub

    Private Delegate Sub delTextToTssl(ByVal TextValue As String)
    Private Sub TextToTssl(ByVal TextValue As String)
        [green]'In this instance we shouldn't use the area.[/green]
        Dim strSize As System.Drawing.SizeF = Me.CreateGraphics.MeasureString(TextValue, txtMsg.Font)
        Dim DispSize As System.Drawing.SizeF = Nothing
        Dim Str() As String
        Dim Display As String = ""
        Dim x As Integer = 0

        Try
            If strSize.Width > txtMsg.Width Then 'We don't need to do any of it if it fits
                Str = TextValue.Split(" ")
                For x = 0 To ([red]Str.Length[/red] - 1) Step 1 [green]'More .Net[/green]
                    [green]'No need for New SizeF[/green]
                    strSize = Me.CreateGraphics.MeasureString(Str(x), txtMsg.Font)
                    DispSize = Me.CreateGraphics.MeasureString(Display & "...", txtMsg.Font)

                    If (strSize.Width + DispSize.Width) < (txtMsg.Width) Then
                        Display = Display & Str(x) & " "
                    Else
                        Display = Display & "..."
                        Exit Try
                    End If
                Next
            End If
        Catch ex As Exception
            txtMsg.Text = "Failed. Fix it."
        Finally
            txtMsg.Text = Display
        End Try
    End Sub

This also shows I should double check myself as in an instance like this we don't want the size within an area. I showed it this way incase you might want to pass other messages, but if the text is never changing other than the cmbDivision.Text then you can not pass the TextValue and put the TotalMessage back into the TextToTssl sub.

If you did want to use it in a TextChanged event I would use the cmbDivision TextChanged event. Assuming cmbDivision is a TextBox then:
Code:
    Private Sub cmbDivision_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDivision.TextChanged
        Dim ctrl As Control = CType(sender, Control)

        If ctrl.Parent IsNot Nothing AndAlso ctrl.Parent.Visible = True Then
            Dim TotalMessage As String = "No Assembly Lines have been created for Division " & _
                                         ctrl.Text & ". One must be created before any jobs can be assigned to it."

            tsslMessage(TotalMessage)
        End If
    End Sub

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top