Control resizing on mouse move or mouse drag - vba

I am creating a simple program, using Excel VBA, that will allow me to resize a control at run-time as the mouse moves or on mouse drag. However, it seems that I'm getting a different result
I'm not sure why I am getting a different final width result, but here's how I'm doing it:
Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
initialx = X
initialy = Y
xmove = True
End Sub
Private Sub TextBox1_MouseMove(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
Dim newx, newy As Single
If xmove = True Then
newx = Math.Abs(X - initialx)
newy = Math.Abs(Y - initialy)
TextBox1.Width = TextBox1.Width + newx
End If
End Sub
And here's how my form looks like:
As you can see in the screenshot, the width of the TextBox should increase by 9 as it is the distance covered by the mouse cursor from the mouse cursor initial click(location) on the control.
And I'm stuck with this. Why isn't it working as expected?

It's needs Global variable initialWidth.
Dim initialX, initialY, xmove, initialWidth
Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As _
Integer, ByVal X As Single, ByVal Y As Single)
initialX = X
initialY = Y
initialWidth = TextBox1.Width '<~~setting initialWidth
xmove = True
TextBox2.Value = initialX
TextBox3.Value = initialY
End Sub
Private Sub TextBox1_MouseMove(ByVal Button As Integer, ByVal Shift As _
Integer, ByVal X As Single, ByVal Y As Single)
Dim newx, newy As Single
If xmove = True Then
newx = Math.Abs(X - initialX)
newy = Math.Abs(Y - initialY)
'TextBox1.Width = TextBox1.Width + newx
TextBox1.Width = initialWidth + newx
TextBox5.Value = newx
TextBox6.Value = newy
End If
End Sub

Related

vb.net runtime buttons and "AddressOf " parenthese

in my system i am creating runtime buttons, i have create one sub to create all buttons which is fine for what i need however they all go to the same "addressOf" i want to create separate handlers, however it doesnt allow with my current method any one know a simple workaround id prefer not to change the actual structure that i have, thanks
sorry dont know why this part is being weird
private Sub Button(ByVal x As Integer, ByVal y As Integer, ByVal name As String, ByVal title As String, ByVal hieght As Integer, ByVal width As Integer, ByVal buttonAddress As String)
Dim btn As Button
btn = New Button
With btn
.Location = New Point(x, y)
.Text = title
.Name = name
.Width = width
.Height = hieght
Controls.Add(btn)
AddHandler btn.Click, AddressOf "BtnOperation_" & buttonAddress
End With
End Sub
Public Sub BtnOperation_AddAppointment(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
Dim name = btn.Name
Select Case name
Case "Cfind_Btn"
'when the Cfind_btn is pressend it create a Csearch textbox at runtime
btn.Visible = False
GetFormType("add_CfindOK")
CreateTxtTypeBox(BoxType.Combo_box, "CSearch_Box")
Case "add_CfindOK"
Case ("Cnew_Btn")
'open the add customer form that connects to the mysql database'
End Select
'fetch the btn.name'
' then with the name use "select case" to get appropreate action of the btn. '
End Sub
Pass the handler into your Button factory method:
private Sub Button(ByVal x As Integer, ByVal y As Integer, ByVal name As String, ByVal title As String, ByVal hieght As Integer, ByVal width As Integer,
clickHandler As System.EventHandler)
Dim btn As Button
btn = New Button
With btn
.Location = New Point(x, y)
.Text = title
.Name = name
.Width = width
.Height = hieght
Controls.Add(btn)
AddHandler btn.Click, clickHandler
End With
End Sub
Then, when you call Button use AddressOf to pass in the correct handler:
Button(0,0,"MyButton".....,AddressOf BtnOperation_AddAppointment)

Printing two TabPages on a TabControl in VB.NET

I am using Visual Studio express 2013, VB. Simplifying the problem as much as I can, I have a form with a tab control that has 2 tab pages. I want to print both tab pages on the click of one button. Currently I am trying to use CreateGraphics on the individual tabs but I just get the first tab printing on both pages. Here is my code, can anyone please see what I am doing wrong or if I am on completely the wrong lines. It looks to me like the CreateGraphics is not retrieving the right tabpages graphics.
Private Declare Auto Function BitBlt Lib "gdi32.dll" (ByVal hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As System.Int32) As Boolean
Private Const SRCCOPY As Integer = &HCC0020
Private PagePrinting As Integer
Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PagePrinting = 0
PrintDocument1.Print()
End If
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
If PagePrinting = 0 Then
e.Graphics.DrawImage(GetImage1(), e.MarginBounds)
Else
e.Graphics.DrawImage(GetImage2(), e.MarginBounds)
End If
PagePrinting = PagePrinting + 1
If PagePrinting = 2 Then e.HasMorePages = False Else e.HasMorePages = True
End Sub
Private Function GetImage1() As Bitmap
Dim me_gr As Graphics = Me.BillTabControl.TabPages("PAGE1").CreateGraphics
Dim bm As New Bitmap(Me.BillTabControl.TabPages("PAGE1").ClientSize.Width, Me.BillTabControl.TabPages("PAGE1").ClientSize.Height, me_gr)
Dim bm_gr As Graphics = Graphics.FromImage(bm)
Dim bm_hdc As IntPtr = bm_gr.GetHdc
Dim me_hdc As IntPtr = me_gr.GetHdc
BitBlt(bm_hdc, 0, 0, Me.BillTabControl.TabPages("PAGE1").ClientSize.Width, Me.BillTabControl.TabPages("PAGE1").ClientSize.Height, me_hdc, 0, 0, SRCCOPY)
me_gr.ReleaseHdc(me_hdc)
bm_gr.ReleaseHdc(bm_hdc)
GetImage1 = bm
End Function
Private Function GetImage2() As Bitmap
Dim me_gr As Graphics = Me.BillTabControl.TabPages("PAGE2").CreateGraphics
Dim bm As New Bitmap(Me.BillTabControl.TabPages("PAGE2").ClientSize.Width, Me.BillTabControl.TabPages("PAGE2").ClientSize.Height, me_gr)
Dim bm_gr As Graphics = Graphics.FromImage(bm)
Dim bm_hdc As IntPtr = bm_gr.GetHdc
Dim me_hdc As IntPtr = me_gr.GetHdc
BitBlt(bm_hdc, 0, 0, Me.BillTabControl.TabPages("PAGE2").ClientSize.Width, Me.BillTabControl.TabPages("PAGE2").ClientSize.Height, me_hdc, 0, 0, SRCCOPY)
me_gr.ReleaseHdc(me_hdc)
bm_gr.ReleaseHdc(bm_hdc)
GetImage2 = bm
End Function
Have you tried selecting the second tab before creating the graphics with SelectTab?
You may also want to add a breakpoint to this line to make sure it is being called:
e.Graphics.DrawImage(GetImage2(), e.MarginBounds)
Here is the code that works.
Private Function GetImage1() As Bitmap
Dim bm As New Bitmap(Me.BillTabControl.TabPages("PAGE1").ClientSize.Width, Me.BillTabControl.TabPages("PAGE1").ClientSize.Height)
Me.BillTabControl.TabPages("PAGE1").DrawToBitmap(bm, Me.BillTabControl.TabPages("PAGE1").ClientRectangle)
GetImage1 = bm
End Function
Private Function GetImage2() As Bitmap
Dim bm As New Bitmap(Me.BillTabControl.TabPages("PAGE2").ClientSize.Width, Me.BillTabControl.TabPages("PAGE2").ClientSize.Height)
Me.BillTabControl.TabPages("PAGE2").DrawToBitmap(bm, Me.BillTabControl.TabPages("PAGE2").ClientRectangle)
GetImage2 = bm
End Function

Click on webpage using cursor coordinates

Can any one help me to click on a web page using cursor coordinates.
Tip: Button don't have ID & name
Here is an example of moving the mouse and clicking using mouse_event:
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, _
ByVal dx As Long, _
ByVal dy As Long, _
ByVal cButtons As Long, _
ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move
Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Private Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
Private Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up
Private Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
Private Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
Private Const MOUSEEVENTF_WHEEL = &H800 ' wheel button rolled
Private Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move
Private Type POINTAPI
x As Long
y As Long
End Type
Sub Click()
Dim pt As POINTAPI
Dim x As Long
Dim y As Long
'(0,0) = top left
'(65535,65535) = bottom right
x = 65535 / 2
y = 65535 / 2
LeftClick x, y
End Sub
Sub LeftClick(x As Long, y As Long)
'Move mouse
mouse_event MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_MOVE, x, y, 0, 0
'Press left click
mouse_event MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
'Release left click
mouse_event MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

round edges of programatic label

I am trying to create labels which have all four corners rounded, the label is being created programatically as seen below:
Dim lbl1 As Label = New Label()
lbl1.AutoSize = False 'allow resizing
lbl1.BackColor = Color.Yellow
lbl1.Text = newid
lbl1.Height = 46
lbl1.Width = 42
lbl1.Padding = New Padding(1, 1, 1, 1)
How would I switch from the square corners to a more XP styled rounding.
Imports System.Runtime.InteropServices
<DllImport("Gdi32.dll", EntryPoint:="CreateRoundRectRgn")> _
Private Shared Function CreateRoundRectRgn(ByVal iLeft As Integer, ByVal iTop As Integer, ByVal iRight As Integer, ByVal iBottom As Integer, ByVal iWidth As Integer, ByVal iHeight As Integer) As IntPtr
End Function
ex.)
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("Gdi32.dll", EntryPoint:="CreateRoundRectRgn")> _
Private Shared Function CreateRoundRectRgn(ByVal iLeft As Integer, ByVal iTop As Integer, ByVal iRight As Integer, ByVal iBottom As Integer, ByVal iWidth As Integer, ByVal iHeight As Integer) As IntPtr
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim newid$ = "mylabel"
Dim lbl1 As Label = New Label()
With lbl1
lbl1.AutoSize = False 'allow resizing
lbl1.BackColor = Color.Yellow
lbl1.Text = newid
lbl1.Height = 46
lbl1.Width = 42
lbl1.Padding = New Padding(1, 1, 1, 1)
lbl1.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(2, 2, lbl1.Width - 2, lbl1.Height - 2, 5, 1))
End With
Me.Controls.Add(lbl1)
End Sub
End Class

Why is this not showing any results in PictureBox?

I'm trying to change an image to black and white on a variable threshold for use in an ocr program. My problem is that I'm not seeing any results in the image that is supposedly processed. I do experience a small wait when rendering, so i am to assume that it is actually doing something.
Imports System.Object
Imports System.Drawing.Bitmap
Public Class Form1
Dim x As Integer, y As Integer
Dim imgx As Integer, imgy As Integer
Dim img As Bitmap
Dim thresh As Bitmap
Dim pixelColor As Color
Dim threshcolor As Color
Dim tempcolor As Color
Public Function getpixel(ByRef x As Integer, ByRef y As Integer) As Color
End Function
Public Sub find_img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles find_img.Click
open_img.ShowDialog()
img_dsp.Text = open_img.FileName()
img_loc.Text = open_img.FileName
img_dsp.ImageLocation = img_dsp.Text
End Sub
Public Sub find_img_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles find_img.LostFocus
img = (img_dsp.Image)
img_dsp.Refresh()
img_dsp.Text = open_img.FileName()
img_dsp.ImageLocation = img_dsp.Text
img = (img_dsp.Image)
img_dsp.Refresh()
End Sub
Public Sub render_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles render.Click
Dim myx As Integer
Dim myy As Integer
img_threshold.Image = img
thresh = img_threshold.Image
For myy = 1 To (img.Height - 1)
For myx = 1 To (img.Width - 1)
tempcolor = img.GetPixel(myx, myy)
If tempcolor.GetBrightness < NumericUpDown1.Value Then
thresh.SetPixel(x, y, Color.Black)
End If
If tempcolor.GetBrightness > NumericUpDown1.Value Then
thresh.SetPixel(x, y, Color.White)
End If
Next myx
Next myy
img_threshold.Image = thresh
img_threshold.Refresh()
End Sub
End Class
Do you know what a reference is? writing A = B, and changing A and writing B = A?
if you specify that A and B point the same object (a reference to the same object) changing one changes the other too, they occupy the same storage in memory! Teach yourself basic, before writing programs, please.