Solved

Text Orientation Vertical

Posted on 2004-08-09
7
1,340 Views
Last Modified: 2008-01-09
Hi,

I have a label, on a Windows application in VB.NET using Visual Studio.NET (2003) and would like to have the text orientation to be vertical, ie. rotated 90deg from default/standard.

Is there a way to do that.

I did find this in VB6 - http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20400548.html?query=orientation+text+vertical&topics=93

Thanks.

Mike
0
Comment
Question by:tlfeet
  • 4
  • 3
7 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 11756659
Here is how to draw rotated text: http://www.vb-helper.com/howto_net_rotated_text.html

Idle_Mind
0
 

Author Comment

by:tlfeet
ID: 11756860
Hi Idle_Mind,

I guess that will work.  

I am not familiar with many of the objects/classes used in the example you referenced, e.g. GraphicsPath and others
(should have mentioned I guess, that I am very unfamiliar with Drawing & Graphics methods, functions, classess in VB.NET (or VB for that matter).

After I posted, I did some more searching in VB.NET help and found this, too:

   Public Sub DrawVerticalString()
        Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
        Dim drawString As String = "Sample Text"
        Dim drawFont As New System.Drawing.Font("Arial", 16)
        Dim drawBrush As New _
            System.Drawing.SolidBrush(System.Drawing.Color.Black)
        Dim x As Single = 150.0
        Dim y As Single = 50.0
        Dim drawFormat As New System.Drawing.StringFormat
        drawFormat.FormatFlags = StringFormatFlags.DirectionVertical
        g.DrawString(drawString, drawFont, drawBrush, _
        x, y, drawFormat)
        drawFont.Dispose()
        drawBrush.Dispose()
        formGraphics.Dispose()
    End Sub

The above works, in my app, kinda.

If the only object I draw is above, it draws it just fine.

If however, I try to draw additional, other objects, e.g. lines, rectangles etc. my app goes into an infinite loop drawing the objects over & over & over, as the OnPaint event handler keeps getting called each time an object gets drawn which cause the whole drawing loop to start again, which raise the OnPaint event, which calles the drawing of the objects, which raise the OnPaint event, etc.

Anyway I can get VB.NET to draw more than one object, w/o going into an infinite loop?
0
 

Author Comment

by:tlfeet
ID: 11756873
PS Thanks Idle Mind!
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11757046
Post your code so we can see what you are doing.  
=)
Idle_Mind
0
 

Author Comment

by:tlfeet
ID: 11757332
Hi Idle_Mind,

You asked: "Post your code so we can see what you are doing."

To which I reply:

In short, I did have, as a debug move, a MsgBox in the middle of the code.  I killed the MsgBox and now I do not get the infinite loop thingy going on.

Would a MsgBox cause rePainting over & over?

Long answer, here is my code, in pseudo form:

I have a VB.NET Windows application, using Visual Studio.NET (2003)

It has some TabPages on it, and most of it works fine.

On one of the TabPages is wish to draw, optionally 1 to 4 graphs (based upon data crunched process by the app)

So I set up a Custom Control, which I thought would contain the info/data/controls/lines, etc. necessary for the graph(s).

In a sense so far so good.  I have very little experience w the System.Drawing.Graphics namespace in VB.NET (or even VB).

Anyway, program will instantiate mycustomcontrol, and draw it on the TabPage . . . which is good.

I can also get it to draw a line (which would be/is the x-axis) . . . again no problem.

However, I need more than just one line, the y-axis, a bunch of labels, boxes/rectangles for the data (a histogram is the type, if that matters)

So, I thought I would go ahead and add one of my Labels, namely the one for the YAxis, which would have text = "Y AXIS DATA" or such, just to move on to another thing I need on the graph(s).

This is where the problem comes up.

If I run what I have so far, just drawing the x-axis, a line, OK fine.

If I have it attempt to draw the YAxisLabel, it goes thru the OnPaint event handler over & over until it bombs out yielding an error message "System.ComponentModel.Win32Exception occurred in system.windows.forms.dll"

I kinda get why it is going thru the OnPaint handler over and over, but have no idea as to how to stop it.

In pseudo code form here is my code:

Public Class MyMainForm

    'bunch of code

    Private Sub ButtonDrawGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDrawGraph.Click
        'this handles the user clicking a button on the MainForm, actually one of the TabPages, which would hopefully draw the Graph
        'requested, except it gets stuck in a loop, drawing over & over ;)
       
       If Not (MyControl Is Nothing) Then
           MyControl.Dispose
           'if the graph already exists, clear it, dispose of it, so as to be able to draw the next one/new one
       End If

       MyControl = New MyControlClass(some parameters)

       Me.TabPage5.Controls.Add(MyControl)
    End Private Sub
End Class    'MyMainForm

Imports System.Drawing.Graphics
Public MyControlClass
     Inherits System.Windows.Forms.UserControl

     Protected Overides Sub OnPaint (ByVal e As PaintEventArgs)
        g = e.Graphics
 
       DrawXAxis()
       DrawVerticalText()
     End Sub

     Private Sub DrawXAxis()
          Dim BlackPen As New Pen(Color.Black, 1)
          Dim startPoint As New Point(5, Me.Height - 20)
          Dim endPoint As New Point(Me.Width - 5, Me.Height - 20)
          g.DrawLine(BlackPen, startPoint, endPoint)
    End Sub

Public Sub DrawVerticalText()
        Dim drawString As String = "Sample Text"
        Dim drawFont As New System.Drawing.Font("Arial", 16)
        Dim drawBrush As New _
            System.Drawing.SolidBrush(System.Drawing.Color.Black)
        Dim x As Single = 150.0
        Dim y As Single = 50.0
        Dim drawFormat As New System.Drawing.StringFormat
        drawFormat.FormatFlags = StringFormatFlags.DirectionVertical
        g.DrawString(drawString, drawFont, drawBrush, _
        x, y, drawFormat)
        drawFont.Dispose()
        drawBrush.Dispose()
    End Sub
End Class

But, as I mentioned, I think the problem was the MsgBox I had in the code.  Anyway deleting the MsgBox, eliminated the loop issue.

But trying to get an understanding of Drawing, OnPaint, etc. etc.

Thanks
Any help is greatly appreciated.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11757619
I did a small test and this worked fine for me.  Below is a simple interface along with the user control code:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Panel1
        '
        Me.Panel1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Panel1.Location = New System.Drawing.Point(8, 8)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(248, 192)
        Me.Panel1.TabIndex = 0
        '
        'Button1
        '
        Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Button1.Location = New System.Drawing.Point(192, 208)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(64, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(264, 238)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Panel1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private mycontrol As MyControlClass

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not (MyControl Is Nothing) Then
            MyControl.Dispose()
        End If

        mycontrol = New MyControlClass
        Panel1.Controls.Add(mycontrol)
        mycontrol.Dock = DockStyle.Fill
    End Sub

End Class

' //////////////////////////////////////////////////////////////////////////////////
' //////////////////////////////////////////////////////////////////////////////////
' //////////////////////////////////////////////////////////////////////////////////

Public Class MyControlClass
    Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'MyControlClass
        '
        Me.Name = "MyControlClass"
        Me.Size = New System.Drawing.Size(350, 250)

    End Sub

#End Region

    Private Sub MyControlClass_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
        Me.Refresh()
    End Sub

    Private Sub MyControlClass_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics

        DrawXAxis(g)
        DrawVerticalText(g)
    End Sub

    Private Sub DrawXAxis(ByVal g As Graphics)
        Dim BlackPen As New Pen(Color.Black, 1)
        Dim startPoint As New Point(5, Me.Height - 20)
        Dim endPoint As New Point(Me.Width - 5, Me.Height - 20)
        g.DrawLine(BlackPen, startPoint, endPoint)
        BlackPen.Dispose()
    End Sub

    Public Sub DrawVerticalText(ByVal g As Graphics)
        Dim drawString As String = "Sample Text"
        Dim drawFont As New System.Drawing.Font("Arial", 16)
        Dim drawBrush As New _
            System.Drawing.SolidBrush(System.Drawing.Color.Black)
        Dim x As Single = 150.0
        Dim y As Single = 50.0
        Dim drawFormat As New System.Drawing.StringFormat
        drawFormat.FormatFlags = StringFormatFlags.DirectionVertical
        g.DrawString(drawString, drawFont, drawBrush, _
        x, y, drawFormat)
        drawFont.Dispose()
        drawBrush.Dispose()
    End Sub

End Class
0
 

Author Comment

by:tlfeet
ID: 11757831
Hi Idle_Mind,

I accepted your first post as the answer, as my second question was not really (infinite redraw problem), nor directly related to topic.

Your first post does answer the q of how to do vertical text.

Regarding your post: Date: 08/09/2004 02:55PM PDT

Yes, that is what I have ended up with, after removing extraneous, unnecessary & problem code.

...I should have just waited for your to write my code ;-) ;-)

Your use of Resize & MyBasePaint handlers might indeed be a better way to go . . . I will have to play with them and associated events . . . but very instructive.

Thank you muchly,

While you may be Idle_Mind, sign me Addled_Brain ;-)

Mike
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now