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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating and resizing labels in a form.

Status
Not open for further replies.

noaco

Programmer
Joined
Jun 25, 2007
Messages
10
Location
IT
Hello,

Today I was commissioned a new task in Visual Studio 2005 with VB .NET, my purpose is creating a form that enables to create and resize new labels inside it in runtime mode including, in one array, infos about the cohordinates and the type of data to insert into the document, would you kindly help me in finding the right way to take?

Thanks.

Andrea.
 
before i start, Welcome to Tek-Tips.


I am assuming you are going to need to be able to come back to this layout at somepoint, which means you will need to save the folling bits of data:
name(option)
text
size
location
id(to group labels together)

you have to decide how persistent you need this data.
per runtime (variable)
per user (XML)
across all levels (DB)

You might want to get some more detailed specs on what the user is trying to do.

When you have answers to those questions, someone here can begin to point you in the right direction.

Thanks,


-The answer to your problem may not be the answer to your question.
 
Thanks,

yes exactly i have to come back to the layout, at a user level, so i was thinking to save it as a xml file to save the label name, size, position, and id.

In practise i import a document image (an hospital first aid form), and above it i trace the labels to transpose the box, the cells etc. containing datas like name surname birthdate age etc. on the electronic doc.

So the user should have a control in which he can create and resize the labels, give them the proper type of data (text, number, checkbox, date, etc.) and save the work in an xml doc.

Thanks for your kind help.

Wish you a good evening.

Ciao.

Andrea.
 
FYi, if you plan on having the user create the controls, then you will need/want some form of "Toolbox" for them, so you know how to put data in the labels/controls when they come back.

-The answer to your problem may not be the answer to your question.
 
Yes, that's exactly what i need....
is it possible to see some code? i'm new in this and got in serious trouble having not idea on how to do it.....
Andrea.
 
Look for:

creating Dynamic controls.
Mouse Event Listeners
XML data storage
user defined controls

FYI: If you got in serious trouble, i would suggest doing some research at home (so you can start working right away)

-The answer to your problem may not be the answer to your question.
 
Actually what i succeeded to code is this:

Public Class Form3
Dim mouseclikresization As Boolean = False
' Class variable that handles all instances of Label control.
Private newLabel As System.Windows.Forms.Label

' Class members to control dragging behavior.
Private dragNow As Boolean
Private mouseLocation As Point

Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Form2.Show()
' Not dragging now.
dragNow = False

'Setting up the Form canvas.
Me.Size = New Size(1024, 768)
Me.Button1.SetBounds(794, 688, 200, 25)
Me.Button1.Text = "&Cliccami per instanziare e spostare nuove label."
End Sub

' You click this button to instantiate a new Label control and set initial properties.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mouseclikresization = False
' Instantiate and set properties.
Me.newLabel = New System.Windows.Forms.Label()
Me.newLabel.Location = New System.Drawing.Point(100, 100)
Me.newLabel.Name = "newLabel"
Me.newLabel.Text = " "
Me.newLabel.Size = New Size(8, 8)
Me.newLabel.BorderStyle = BorderStyle.FixedSingle

' Add generic handlers to handle these three mouse events.
AddHandler Me.newLabel.MouseDown, AddressOf Label_GenericMouseDown
AddHandler Me.newLabel.MouseMove, AddressOf Label_GenericMouseMove
AddHandler Me.newLabel.MouseUp, AddressOf Label_GenericMouseUp

' Add label to Form's control collection.
Me.Controls.Add(Me.newLabel)
End Sub

' Handles generic MouseDown event for labels.
Private Sub Label_GenericMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
' Mr. Label, if left mouse button is down then get ready to be dragged.
If e.Button = Windows.Forms.MouseButtons.Left Then
dragNow = True
mouseLocation = e.Location
End If
End Sub

' Handles generic MouseMove event for labels.
Private Sub Label_GenericMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If (dragNow = True) Then
' This little equation do the actual job.
CType(sender, Label).Location -= mouseLocation - e.Location
End If
End Sub

' Handles generic MouseUp event for labels.
Private Sub Label_GenericMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
' Now you can relax Mr. Label. Dragging is over now.
If dragNow = True Then dragNow = False
End Sub


'







Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
dragNow = False
End Sub
End Class

and in another form i coded this:

Public Class Form2
Dim mouseclikresization As Boolean = False


Private Sub newLabel_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles newLabel.MouseMove

If mouseclikresization = True Then
newLabel.Width = e.X
Else

If e.X >= newLabel.Width - 3 Then
newLabel.Cursor = Cursors.Cross
Else
newLabel.Cursor = Me.Cursor
End If

End If

If mouseclikresization = True Then
newLabel.Height = e.Y
Else

If e.Y >= newLabel.Height - 3 Then
newLabel.Cursor = Cursors.Cross
Else
newLabel.Cursor = Me.Cursor
End If

End If

End Sub

Private Sub newLabel_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles newLabel.MouseDown

mouseclikresization = True
newLabel.Cursor = Cursors.Cross
End Sub

Private Sub newLabel_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles newLabel.MouseUp

mouseclikresization = False
newLabel.Cursor = Me.Cursor
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class

now i should merge them, but when i copy the second piece to the first form i am said that the newLabel variable is not valid.....why?

Andrea.
 
Code:
Public Class Form3
    Dim mouseclikresization As Boolean = False
    ' Class variable that handles all instances of Label control.
    [b]Private newLabel As System.Windows.Forms.Label[/b]
Code:
Public Class Form2
    Dim mouseclikresization As Boolean = False


    Private Sub newLabel_MouseMove(ByVal sender As Object, _

you forgot to copy the variable line
You may want to consider generalizing some of that into helper functions.

-The answer to your problem may not be the answer to your question.
 
If your looking to make some controls resizable check out AJAXTOOLKIT. and look for ResizableControl.

Ordinary Programmer
 
I succeeded in doing if with mouseeventsargs, if i click a button i can create and resize a label in runtime.
now i need another button to move and resize the labels already created, so if i click the button and position the cursor over the label, the cursor changes, in hand if i have to move it, in arrows if i over the bottom right border so i can resize it.
the labels are collected in a system.collection class so i have to recall the controls by the function to use them and their areas, any suggestions?
how does for each work with arrays in vb .net?
tomorrow i will post the code.
thanks.
andrea.
 

Psuedo code:

For each oControl as Label in myControlCollection.Items

Next

it will skips all the things that aren't labels

-The answer to your problem may not be the answer to your question.
 
Public Class Form1

Dim MyControlArray As LabelArray

Dim numero As Integer

'variabile per creare dei label
Dim crea As Boolean = False

Dim sposta As Boolean = False

Private newLabel As System.Windows.Forms.Label


'variabile per la prima locazione con le nuove label

Private mouseLocation As Point


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


'Setting up the Form canvas.
Me.Size = New Size(1024, 768)
Me.Button1.SetBounds(794, 688, 200, 25)
MyControlArray = New LabelArray(Me)
End Sub


Private Sub Crea_Label1(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)



If e.Button = Windows.Forms.MouseButtons.Left Then


'Me.newLabel = New System.Windows.Forms.Label()
MyControlArray.AddNewLabel()
numero = MyControlArray.Count - 1
newLabel = MyControlArray(numero)
mouseLocation = e.Location
Me.newLabel.Location = e.Location
Me.newLabel.Name = "newLabel"

Me.newLabel.Size = New Size(1, 1)
Me.newLabel.BorderStyle = BorderStyle.FixedSingle

Me.Controls.Add(Me.newLabel)
crea = True
numero += 1

End If

End Sub
Private Sub Crea_Label2(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

If (crea = True) Then

Me.newLabel.Width = e.X - mouseLocation.X
Me.newLabel.Height = e.Y - mouseLocation.Y
End If

End Sub

Private Sub Crea_Label3(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If (crea = True) Then

Me.newLabel.Width = e.X - mouseLocation.X
Me.newLabel.Height = e.Y - mouseLocation.Y
End If
crea = False

End Sub

Private Sub Modifica_Label1(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

For Each oControl As Label In MyControlArray


If e.Location = oControl.Location Then
Me.Cursor = Cursors.Hand
Else : Me.Cursor = Cursors.Default
End If

Next



End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Cursor = Cursors.Cross

AddHandler Me.MouseDown, AddressOf Crea_Label1
AddHandler Me.MouseMove, AddressOf Crea_Label2
AddHandler Me.MouseUp, AddressOf Crea_Label3

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Me.Cursor = Cursors.Default


AddHandler Me.MouseMove, AddressOf Modifica_Label1

End Sub
End Class

How to change the cursor if i press the button and e.Location on the label area?
thanks.
Andrea.
 
Update: if i click a second time in button1 it adds 2 labels instead of 1, why?
Thanks.
Andrea.
 
The code is doing exactly what you told it. each time you click button 1 you get a new label. You will have to add some logic to "lock" the button until you are finished with the first label.

You can set a label's position at any time, you just need to get the location of the mouse. and then make sure you drop the label at the location of the mouse.

Code:
 Private Sub Form2_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        Dim pt As New Point(e.X, e.Y)
        lblNext.Location = pt
    End Sub

-The answer to your problem may not be the answer to your question.
 
No, as a matter of facts, each time i click button1, and there after i click the mouse i create a new label, so why the second time i click button1, after i clicked button 2, and re click button1 it adds two labels?
 
Have you stepped through the debugger to see where the code is executing?

-The answer to your problem may not be the answer to your question.
 
Hello, problem solved with removehandler and checkedbutton, the next step is being able to create the label over a picturebox, it appears to be not possible and found nothing on the net, do you have any suggestions?
Ok.
Thanks.
Bye.
Ciao.
Andrea.
 
define "over a picturebox"

Does the label just need to
A) be positioned at a place that just happens to overlap with a picturebox (that may or may not be generated on the fly)
B)does it have to "stick" to the picture box?

A) if the picture box is already on the form at design time, then you can just add the picturebox to the handler that you created to handle clicks on the form.

B) you may need to create a custom panel container that holds a picture box and (1 or more) labels, so that when you move the container the labels move with the picture box.


-The answer to your problem may not be the answer to your question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top