Resize text to fit the text into label - vb.net - vb.net

I want to automatically change the size of font depends on the length of text that fit into fixed size of label in vb.net

Add this event for Label.Paint
Private Sub Label1_Paint(sender As Object, e As PaintEventArgs) Handles Label1.Paint
Dim ratio = Label1.Width / Windows.Forms.TextRenderer.MeasureText(Label1.Text, Label1.Font).Width
Dim newWidth = Math.Floor(Label1.Font.Size * ratio)
Label1.Font = New Font(Label1.Font.FontFamily, CInt(newWidth))
End Sub
It measures the text and resizes it to fit the Label. This will be called when the label is drawn - when the text is changed, label resized, shown, etc.

Related

How to modify an image by inserting another image?

I have an image (800x800p) loaded in a PictureBox. It's slightly interactive, clicking somewhere on it will show tooltips or popup windows.
Occasionally, clicking would result in adding of 100x100 px image on the location where it was clicked. That modification needs to be saved onto the main image.
Any ideas as to how to do this?
Draw the new image over your original image.
For example:
Public Class Form1
Private Sub PictureBox1_Click(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
Dim NewIMG As New Bitmap(100, 100)
Graphics.FromImage(NewIMG).Clear(Color.Black) 'just to make my image black since I don't have any image
'You drawing new image here
Graphics.FromImage(PictureBox1.Image).DrawImage(NewIMG, New Point(e.X - 50, e.Y - 50)) 'mouse position - half new image size so that new image center is placed at cursor location
PictureBox1.Refresh() 'to make picbox redraw and display new image
End Sub
End Class

draw coordinate Two dimensions graph of (x-y) visual basic

hey every one I want to make a program that able to draw coordinate graph of Two dimensions of (x-y).when I enter a value in (x) text box and (y) text box and hit draw button it well draw the graph in the blue picture box . I searched in web sit but I found only one method that draw using the mouse and this not what I want .This is the image of the program and and it supposed to draw in white line like this image
Private Sub PictureBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
Static last As Point
If e.Button = Windows.Forms.MouseButtons.Left Then
PictureBox2.CreateGraphics.DrawLine(Pens.White, last.X, last.Y, e.X, e.Y)
End If
last = e.Location
End Sub
this is the code that I found that draw using the mouse
You should use the Graphic class inside the panel's Paint event
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
' Create pen.
Using blackPen As New Pen(Color.Black, 3)
' Create points that define line.
Dim point1 As New Point(100, 100)
Dim point2 As New Point(500, 100)
' Draw line to screen.
e.Graphics.DrawLine(blackPen, point1, point2)
End Using
End Sub
Then call Panle1.Invalidate() to fire the Paint event
It would be good to have a bit more details of your code, especially how and in what class you store the X and Y coordinate. By the way you draw a line between 2 points so you would need two sets of X and Y coordinate boxes on your form.
After that it is as easy as what you found on the internet using the DrawLine method (https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawline(v=vs.110).aspx), you just needs to fire it from the Click event of your "draw" button.
Hi again Ahmed
Here is a simple sample of a form that would draw a line when button is clicked. Of course it would need more bootstrapping to make sure user only enter integer value for the number of pixels and in its basic form, (0,0) is the top left of the panel but it can easily be converted for a bottom left approach...
and
Class Form1
Private Sub cmdDraw_Click(sender As Object, e As EventArgs) Handles cmdDraw.Click
Dim x1 As Integer = Integer.Parse(txtX1.Text)
Dim y1 As Integer = Integer.Parse(txtY1.Text)
Dim x2 As Integer = Integer.Parse(txtX2.Text)
Dim y2 As Integer = Integer.Parse(txtY2.Text)
pnlMap.CreateGraphics.DrawLine(New Pen(Color.Black), x1, y1, x2, y2)
End Sub
End Class

Change the size of the text and shape using vertical and horizontal scroll

I'm using Visual basic 2012 Ultimate. I want to change the Font size of the text using vertical and horizontal scroll. I tried but the size of the shape changes but not the Text.
Here is the code
Private Sub HScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles HScrollBar1.Scroll
RectangleShape1.Width = HScrollBar1.Value
HScrollBar1.Value = TextBox1.Text.
End Sub
Private Sub VScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles VScrollBar1.Scroll
RectangleShape1.Height = VScrollBar1.Value
VScrollBar1.Value = TextBox1.Text.Size()
End Sub
How about changing this two lines of your code :
HScrollBar1.Value = TextBox1.Text.
...
VScrollBar1.Value = TextBox1.Text.Size()
to something like this :
TextBox1.FontSize = HScrollBar1.Value
...
TextBox1.FontSize = VScrollBar1.Value
Haven't tested that, but I think you will see that changes in scrollbars make changes in text's Font Size now, after this code modification. However, I'm not sure what you are precisely after. In case of Rectangle, it has width and height so you can associate width with horizontall scrollbar and height with vertical scrollbar. But in case of Font Size it has no such a width and height.
PS: if you actually want something different, please elaborate what you are actually after, how do you want the modification in vertical and horizontal srollbar value affect Font Size precisely?

Resizing Window Form control

My problem is that I am unable to resize the form control according to the form size.
I have a form which contains a textbox, a label and a button. On button click I need to open the same form in a particular sized window and I want all the controls of the form should be proportional with the form and the font size inside the control should be proportional too. Can anybody please suggest how to do that with code. Unable to solve this by using the anchor and dock property.
My code:
Private Sub Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim widthRatio As Single = Me.Size.Width / 300.0F
Dim heightRatio As Single = Me.Size.Height / 300.0F
Dim scale As New SizeF(widthRatio, heightRatio)
Me.Scale(scale)
For Each control As Control In Me.Controls
control.Scale(scale)
control.Font = New Font(control.Font.FontFamily, control.Font.SizeInPoints * widthRatio * heightRatio)
Next
End Sub
here for width and height ratio I have divided 300.0F because my original form is size(300,300), but I am not getting all the controls expand equally... any suggestion is appreciated.
Thanks
pallavi
Check these two lines
Dim widthRatio As Single = Me.Size.Width / 300.0F
Dim heightRatio As Single = Me.Size.Height / 300.0F
in your case Me.Size.Width = 300 and Me.Size.Height = 300 that's why its not working, change width or height then check it

How to show a grid on picturebox control?

I want to display a image on picturebox and also i want to show grid on picturebox.So if i zoom the image then i can easily identify the pixel size on picturebox.Can any one help me to do this?For eg.
This normal display of image on picturebox
But i want to display image like this on picturebox
Here's the code to draw grid lines using the Graphics.DrawLine():
Public Class Form1
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim g As Graphics = e.Graphics
Dim pn As New Pen(Color.White) '~~~ color of the lines
Dim x As Integer
Dim y As Integer
Dim intSpacing As Integer = 10 '~~~ spacing between adjacent lines
'~~~ Draw the horizontal lines
x = PictureBox1.Width
For y = 0 To PictureBox1.Height Step intSpacing
g.DrawLine(pn, New Point(0, y), New Point(x, y))
Next
'~~~ Draw the vertical lines
y = PictureBox1.Height
For x = 0 To PictureBox1.Width Step intSpacing
g.DrawLine(pn, New Point(x, 0), New Point(x, y))
Next
End Sub
End Class
To test this, create a new project and add a picturebox (name = PictureBox1). Then select an image for it(you could use the property window to set the image). Then copy-paste the above code and run it. You will see grid lines. We have written the code to draw the gridlines on the paint event of the Picturebox. So, these grids will be redrawn when you set an image on the picturebox at runtime too.
Hope it will give you an idea. BTW, the above was coded and tested using VB.Net.
Wish you good luck...