Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: jacj0102 en 14 Mayo 2012, 07:13 am



Título: Ayuda con un Label en C#
Publicado por: jacj0102 en 14 Mayo 2012, 07:13 am
Ante todo que tenga un buen día, quisiera preguntarles si es posible cambiar la forma de muestra en un Label por ejemplo:

normalmente en un label se muestra así: "Hola Mundo"

pero yo quiero que me muestre así:
H
o
l
a

M
u
n
d
o

De arriba hacia abajo no se si sera posible, espero me puedan ayudar gracias de antemano.


Título: Re: Ayuda con un Label en C#
Publicado por: seba123neo en 14 Mayo 2012, 15:59 pm
podes hacerlo con saltos de lineas entre cada letra, pero queda horrible el codigo, es mejor crearte un control que maneje por si solo el texto, aca tenes una clase sacada de internet, la cual dibuja el texto:

Código
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.Text
  4. Imports System.Windows.Forms
  5. Imports System.Drawing
  6.  
  7. Public Class VerticalLabel
  8.    Inherits Label
  9.  
  10.    Public Sub New()
  11.        SetStyle(ControlStyles.UserPaint, True)
  12.        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
  13.        SetStyle(ControlStyles.DoubleBuffer, True)
  14.  
  15.        Me.AutoSize = False
  16.    End Sub
  17.  
  18.    Private Sub CalculateSize(ByVal value As String)
  19.        Me.AutoSize = False
  20.  
  21.        Dim height As Integer = 0
  22.        Dim width As Integer = 0
  23.        Dim sizeTemp As New Size()
  24.  
  25.        For Each c As Char In value
  26.            sizeTemp = TextRenderer.MeasureText(c.ToString(), Me.Font)
  27.            height += sizeTemp.Height
  28.  
  29.            If sizeTemp.Width > width Then
  30.                width = sizeTemp.Width
  31.            End If
  32.        Next
  33.  
  34.        Me.Height = height
  35.        Me.Width = width
  36.    End Sub
  37.  
  38.    Public Overrides Property Text() As String
  39.        Get
  40.            Return MyBase.Text
  41.        End Get
  42.        Set(ByVal value As String)
  43.            MyBase.Text = value
  44.            CalculateSize(value)
  45.        End Set
  46.    End Property
  47.  
  48.    Public Overrides Property Font() As Font
  49.        Get
  50.            Return MyBase.Font
  51.        End Get
  52.        Set(ByVal value As Font)
  53.            MyBase.Font = value
  54.            CalculateSize(MyBase.Text)
  55.        End Set
  56.    End Property
  57.  
  58.    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
  59.        Dim br As New SolidBrush(Me.ForeColor)
  60.        Dim smt As New StringFormat()
  61.  
  62.        smt.Alignment = StringAlignment.Center
  63.  
  64.        Dim sizeTemp As New Size()
  65.        Dim i As Integer = 0
  66.        For Each c As Char In Text
  67.            sizeTemp = TextRenderer.MeasureText(c.ToString(), Me.Font)
  68.            Dim rc As New Rectangle(0, sizeTemp.Height * i, sizeTemp.Width, sizeTemp.Height)
  69.            e.Graphics.DrawString(c.ToString(), Font, br, rc, smt)
  70.            i += 1
  71.        Next
  72.  
  73.        br.Dispose()
  74.    End Sub
  75. End Class
  76.  

eso lo pones en una clase y despues te va a aparecer el control en la barra de herramientas como si fuera un control, lo unico que tenes que hacer es arrastrarlo al formulario, y cambiarle el texto que quieras y se va a mostrar vertical.

saludos.