-6

How will i be able to draw a textbox during runtime and let it have its own index in vb.net? i'm trying to do it like how Microsoft PowerPoint draws its textbox. Can you help me with this?

EDIT: i've attached a picture of what i really want to ask from you. i'm sorry i can't properly put it into words.!This is a photo from a single slide in PP. notice the edges of the textbox, it can be resized and edit the text inside right? that's what i want to do in VB.net. any help guys? Screenshot of a textbox in PP

  • 1
    I'm going to ask the question that the other 12 people who viewed this question thought of but didn't ask; what have you tried? – Jonathon Cowley-Thom Aug 05 '14 at 15:34
  • i've tried creating textboxes with a fixed size and location . but i can drag them afterwards. the source code also came from the other posted question here about creating textboxes so i didn't bother posting it. – Harley Barredo Aug 05 '14 at 15:43
  • @HarleyBarredo maybe link to it so we have some idea what you are talking about. – djv Aug 05 '14 at 16:43
  • Provide your code and provide an hyperlink to the other question, and maybe provide a video/image example of what are you talking about what does PowerPoint iwth a Textbox, try to write an understandable question, we are not magicians. – ElektroStudios Aug 05 '14 at 17:26

2 Answers2

0

Maybe this can get you started...

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim yCoord As Integer

        For count As Integer = 0 To 10
            Dim tBox As New TextBox()
            yCoord += 20
            Me.Controls.Add(tBox)
            tBox.Location = New Point(0, yCoord)
            tBox.Name = "tBox" & count.ToString()
        Next
    End Sub
End Class
Rose
  • 641
  • 7
  • 17
  • 1
    That creates, does not draws. but of course the OP question is very low quality to understand whether he want to instance controls at runtime or really he want to (owner)draw the control parts. – ElektroStudios Aug 05 '14 at 17:24
0

You can try a resizable UserControl.

Create a new UserControl as a template (German, but you will find it):

enter image description here

Set the BorderStyle to FixedSingle.

Add a Label to the UserControl. I called it lblInner:

enter image description here

Now for some coding including the possibility to resize the UserControl at Runtime using Mouse Handles. Please note that the code for resizing is used from the following excellent Stack Overflow answer by Charles P.:

Custom Resize Handle in Border-less Form C#

Public Class MyBoxLabel

#Region "Constructor"
    Public Sub New()

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.


        Me.lblInner.AutoSize = False
        Me.lblInner.TextAlign = ContentAlignment.MiddleCenter
        'Need a free border at the sides, or the mouse actions will not work properly
        Me.lblInner.Location = New Point(3, 3)
        Me.lblInner.Size = New Size(Me.ClientSize.Width - 7, Me.ClientSize.Height - 7)
        Me.lblInner.Anchor = AnchorStyles.Left Or AnchorStyles.Bottom Or AnchorStyles.Right Or AnchorStyles.Top
    End Sub
#End Region

#Region "Properties"
    ''' <summary>
    ''' Allows you to change the inner text
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property InnerText As String
        Set(value As String)
            Me.lblInner.Text = value
        End Set
        Get
            Return Me.lblInner.Text
        End Get
    End Property

    ''' <summary>
    ''' Allows you to change the inner font
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property InnerFont As Font
        Set(value As Font)
            Me.lblInner.Font = value
        End Set
        Get
            Return Me.lblInner.Font
        End Get
    End Property

    'Add further properties as needed
#End Region

#Region "Constants"
    Const WM_NCHITTEST As UInt32 = &H84
    Const WM_MOUSEMOVE As UInt32 = &H200

    Const HTLEFT As UInt32 = 10
    Const HTRIGHT As UInt32 = 11
    Const HTBOTTOMRIGHT As UInt32 = 17
    Const HTBOTTOM As UInt32 = 15
    Const HTBOTTOMLEFT As UInt32 = 16
    Const HTTOP As UInt32 = 12
    Const HTTOPLEFT As UInt32 = 13
    Const HTTOPRIGHT As UInt32 = 14

    Const RESIZE_HANDLE_SIZE As Integer = 10
#End Region

#Region "Handling of Window Messages to allow resizing of the UserControl"
    Protected Overrides Sub WndProc(ByRef m As Message)

        Dim handled As Boolean = False
        If m.Msg = WM_NCHITTEST OrElse m.Msg = WM_MOUSEMOVE Then
            Dim formSize As Size = Me.Size
            Dim screenPoint As New Point(m.LParam.ToInt32())
            Dim clientPoint As Point = Me.PointToClient(screenPoint)

            Dim boxes As New Dictionary(Of UInt32, Rectangle)() From { _
                {HTBOTTOMLEFT, New Rectangle(0, formSize.Height - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)}, _
                {HTBOTTOM, New Rectangle(RESIZE_HANDLE_SIZE, formSize.Height - RESIZE_HANDLE_SIZE, formSize.Width - 2 * RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)}, _
                {HTBOTTOMRIGHT, New Rectangle(formSize.Width - RESIZE_HANDLE_SIZE, formSize.Height - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)}, _
                {HTRIGHT, New Rectangle(formSize.Width - RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, formSize.Height - 2 * RESIZE_HANDLE_SIZE)}, _
                {HTTOPRIGHT, New Rectangle(formSize.Width - RESIZE_HANDLE_SIZE, 0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)}, _
                {HTTOP, New Rectangle(RESIZE_HANDLE_SIZE, 0, formSize.Width - 2 * RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)}, _
                {HTTOPLEFT, New Rectangle(0, 0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE)}, _
                {HTLEFT, New Rectangle(0, RESIZE_HANDLE_SIZE, RESIZE_HANDLE_SIZE, formSize.Height - 2 * RESIZE_HANDLE_SIZE)} _
            }

            For Each hitBox As KeyValuePair(Of UInt32, Rectangle) In boxes
                If hitBox.Value.Contains(clientPoint) Then
                    m.Result = CType(hitBox.Key, IntPtr)
                    handled = True
                    Exit For
                End If
            Next
        End If

        If Not handled Then
            MyBase.WndProc(m)
        End If
    End Sub
#End Region

End Class

Compile your project.

In your ToolBox you will now find the new UserControl. Add it to your form and launch your application. When you move your mouse to the edges of the control you will see the typical resize mouse cursors. Use these to resize the control. The text will stay centered due to the Anchor properties of the Label and the TextAlign.

Community
  • 1
  • 1
Jens
  • 6,275
  • 2
  • 25
  • 51
  • thank you for your answer. i really appreciate it! this will be a big help in my study. – Harley Barredo Aug 06 '14 at 08:38
  • No problem. If it answers your question you should mark it as the accepted answer using the checkmark in order to mark the question as answered. – Jens Aug 06 '14 at 08:47
  • hey jens, i just tried this algorithm and tested its output. is it possible to resize JUST the LABEL? how will i manipulate it? sorry for the dumb question though thanks for answering. – Harley Barredo Aug 06 '14 at 16:15
  • I don't understand the difference, there is nothing more than the label. If you want to resize the text, change the Font Size of the Label in the UserControl's resize event. To change the text you can handle for example the DoubleClick event and show an inputbox for the new text or add a context menu, etc..etc... many roads to Rome. There was nothing in the original question about it all, you need to be way more specific in your questions, this is not twitter, you have all the room you need. Take the time. – Jens Aug 06 '14 at 17:45