I have two forms . Form1 and Form2
Form1 is a 600x500 and form2 is 300x250.
Form1 is allways open and when i open form2 it should be on top of form1. How to disable form2 to move out of width form1
Like this
Not like this
So it should be re sizable but unable to move out of edges form1.
If you're not planning to use MDI Forms
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TopMost = True
Me.Left = Form1.Location.X
Me.Top = Form1.Location.Y + 30
End Sub
Private Sub Form2_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
'check position
Dim topLeft, topRight, bottomLeft, BottomRight As Point
With Form1
topLeft = New Point(.Location.X, .Location.Y + 30)
topRight = New Point(.Location.X + .Width, .Location.Y + 30)
bottomLeft = New Point(.Location.X, .Location.Y + .Height)
'BottomRight = New Point(.Location.X + .Location.Y + .Height)
End With
Dim mytopLeft, mytopRight, mybottomLeft, myBottomRight As Point
With Me
mytopLeft = New Point(.Location.X, .Location.Y)
mytopRight = New Point(.Location.X + .Width, .Location.Y)
mybottomLeft = New Point(.Location.X, .Location.Y + .Height)
'myBottomRight = New Point(.Location.X + .Location.Y + .Height)
End With
If topLeft.X > mytopLeft.X Then
Me.Location = New Point(topLeft.X, Me.Location.Y)
Me.Text = "topLeftX"
End If
If topLeft.Y > mytopLeft.Y Then
Me.Location = New Point(Me.Location.X, topLeft.Y)
Me.Text = "topLeftY"
End If
If topRight.X < mytopRight.X Then
Me.Location = New Point(topRight.X - Me.Width, Me.Location.Y)
Me.Text = "topRightX"
End If
If bottomLeft.Y < mybottomLeft.Y Then
Me.Location = New Point(Me.Location.X, bottomLeft.Y - Me.Height)
Me.Text = "bottomLeftY"
End If
End Sub
Related
i have three different buttons in a form which have three different widths and heights of a rectangle.each time the button is clicked , the width and height is loaded and eachtime picturebox is clicked rectangle is created on a picturebox.i want the three rectangles to go side by side in X axis but wheneven i click the first rectangle and click on picturebox, three same rectangle is drawn. i want some help with the loop. here is my code.
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim myGraphics As Graphics = PictureBox1.CreateGraphics()
Dim myPen As Pen
myPen = New Pen(Drawing.Color.Black, 1)
myGraphics.DrawLine(myPen, pt1.X, pt1.Y, pt2.X, pt2.Y)
myGraphics.DrawLine(myPen, pt3.X, pt3.Y, pt4.X, pt4.Y)
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles PictureBox1.Click 'MessageBox.Show(e.Clicks)
Dim myGraphics As Graphics = PictureBox1.CreateGraphics
midpoint.X = (pt1.X + pt2.X) / 2
midpoint.Y = (pt3.Y + pt4.Y) / 2
Dim l(2) As Point
l(0).X = midpoint.X
l(0).Y = midpoint.Y
If e.X >= midpoint.X Then
Dim i As Integer
For i = 0 To 2
If (i > 0) Then
l(i).X += l(i - 1).X + myRect.Width
l(i).Y = midpoint.Y - myRect.Height
Dim myPen As Pen
myPen = New Pen(Drawing.Color.Black, 1)
myGraphics.DrawRectangle(myPen, l(i).X, l(i).Y, myRect.Width, myRect.Height)
Else
l(i).X = midpoint.X
l(i).Y = midpoint.Y - myRect.Height
Dim myPen As Pen
myPen = New Pen(Drawing.Color.Black, 1)
myGraphics.DrawRectangle(myPen, l(i).X, l(i).Y, myRect.Width, myRect.Height)
End If
Next
End If
Here's an example of what jmcilhinney is talking about:
Public Class Form1
' these represent the x-axis?
Private pt1 As New Point(0, 100)
Private pt2 As New Point(200, 100)
' these represent the y-axis?
Private pt3 As New Point(100, 0)
Private pt4 As New Point(100, 200)
' represents the size of the box to draw
Private myRect As New Size(30, 20)
Private numClicks As Integer
Private rects As New List(Of Rectangle)
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
' draw the x and y axis
e.Graphics.DrawLine(Pens.Black, pt1, pt2)
e.Graphics.DrawLine(Pens.Black, pt3, pt4)
' draw all the rectangles in our list
For Each rc As Rectangle In rects
e.Graphics.DrawRectangle(Pens.Blue, rc)
Next
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Dim origin As New Point((pt1.X + pt2.X) / 2, (pt3.Y + pt4.Y) / 2)
Dim clickedPt As Point = PictureBox1.PointToClient(Cursor.Position)
If clickedPt.X > origin.X Then
If numClicks < 3 Then
Dim rc As New Rectangle(New Point(origin.X + (numClicks * myRect.Width), origin.Y - myRect.Height), myRect)
rects.Add(rc)
numClicks = numClicks + 1
PictureBox1.Invalidate()
End If
End If
End Sub
End Class
I created an Borderless Windows Form and added the functionalities of the default System Header:
Moving:
Private m_blnMouseDown As Boolean = False
Private m_utdMouseOffset As Point
' Left mouse button pressed
Private Sub BorderlessMove_MouseDown(sender As Object, e As MouseEventArgs) Handles tlpHeader.MouseDown
If e.Button = MouseButtons.Left Then
' Get the new position
Dim mouseRelative As Point = Me.PointToClient(Cursor.Position)
m_utdMouseOffset = New Point(-mouseRelative.X, -mouseRelative.Y)
' Set that left button is pressed
m_blnMouseDown = True
End If
End Sub
' MouseMove used to check if mouse cursor is moving
Private Sub BorderlessMove_MouseMove(sender As Object, e As MouseEventArgs) Handles tlpHeader.MouseMove
If m_blnMouseDown Then
Dim mousePos As Point = Control.MousePosition
' Get the new form position
mousePos.Offset(m_utdMouseOffset.X, m_utdMouseOffset.Y)
Me.Location = mousePos
End If
End Sub
' Left mouse button released, form should stop moving
Private Sub BorderlessMove_MouseUp(sender As Object, e As MouseEventArgs) Handles tlpHeader.MouseUp
If e.Button = MouseButtons.Left Then
m_blnMouseDown = False
End If
End Sub
Resizing:
Private Enum ResizeState
North
NE
East
SE
South
SW
West
NW
None
End Enum
Private m_utdResizeState As ResizeState = ResizeState.None
Private Const GRIP_SIZE As Int32 = 5
Private Sub BorderlessResize_FormLoad(sender As Object, e As EventArgs) Handles Me.Load
Const DGRIP_SIZE As Int32 = GRIP_SIZE * 2
Dim Sizes As New Dictionary(Of String, Size)
Sizes.Add("Horizontal", New Size(Me.Width - DGRIP_SIZE * 2, GRIP_SIZE))
Sizes.Add("Vertical", New Size(GRIP_SIZE, Me.Height - DGRIP_SIZE * 2))
Sizes.Add("HEdge", New Size(DGRIP_SIZE, GRIP_SIZE))
Sizes.Add("VEdge", New Size(GRIP_SIZE, DGRIP_SIZE))
GenerateTransparentPanel("pnlResizeNorth", ResizeState.North, Sizes.Item("Horizontal"), New Point(DGRIP_SIZE, 0), (AnchorStyles.Top Or AnchorStyles.Right) Or AnchorStyles.Left)
GenerateTransparentPanel("pnlResizeNE_H", ResizeState.NE, Sizes.Item("HEdge"), New Point(Me.Width - DGRIP_SIZE, 0), AnchorStyles.Top Or AnchorStyles.Right)
GenerateTransparentPanel("pnlResizeNE_V", ResizeState.NE, Sizes.Item("VEdge"), New Point(Me.Width - GRIP_SIZE, 0), AnchorStyles.Top Or AnchorStyles.Right)
GenerateTransparentPanel("pnlResizeEast", ResizeState.East, Sizes.Item("Vertical"), New Point(Me.Width - GRIP_SIZE, DGRIP_SIZE), (AnchorStyles.Top Or AnchorStyles.Right) Or AnchorStyles.Bottom)
GenerateTransparentPanel("pnlResizeSE_H", ResizeState.SE, Sizes.Item("HEdge"), New Point(Me.Width - DGRIP_SIZE, Me.Height - GRIP_SIZE), AnchorStyles.Right Or AnchorStyles.Bottom)
GenerateTransparentPanel("pnlResizeSE_V", ResizeState.SE, Sizes.Item("VEdge"), New Point(Me.Width - GRIP_SIZE, Me.Height - DGRIP_SIZE), AnchorStyles.Right Or AnchorStyles.Bottom)
GenerateTransparentPanel("pnlResizeSouth", ResizeState.South, Sizes.Item("Horizontal"), New Point(DGRIP_SIZE, Me.Height - GRIP_SIZE), (AnchorStyles.Bottom Or AnchorStyles.Right) Or AnchorStyles.Left)
GenerateTransparentPanel("pnlResizeSW_H", ResizeState.SW, Sizes.Item("HEdge"), New Point(0, Me.Height - GRIP_SIZE), AnchorStyles.Bottom Or AnchorStyles.Left)
GenerateTransparentPanel("pnlResizeSW_V", ResizeState.SW, Sizes.Item("VEdge"), New Point(0, Me.Height - DGRIP_SIZE), AnchorStyles.Bottom Or AnchorStyles.Left)
GenerateTransparentPanel("pnlResizeWest", ResizeState.West, Sizes.Item("Vertical"), New Point(0, DGRIP_SIZE), (AnchorStyles.Top Or AnchorStyles.Left) Or AnchorStyles.Bottom)
GenerateTransparentPanel("pnlResizeNW_H", ResizeState.NW, Sizes.Item("HEdge"), New Point(0, 0), AnchorStyles.Top Or AnchorStyles.Left)
GenerateTransparentPanel("pnlResizeNW_V", ResizeState.NW, Sizes.Item("VEdge"), New Point(0, 0), AnchorStyles.Top Or AnchorStyles.Left)
End Sub
Private Sub GenerateTransparentPanel(name As String, tag As ResizeState, size As Size, location As Point, anchors As AnchorStyles)
Dim panel As TransparentPanel = New TransparentPanel()
panel.Anchor = anchors
panel.Location = location
panel.Name = name
panel.Size = size
panel.Tag = tag
AddHandler panel.MouseDown, AddressOf BorderlessResize_MouseDown
AddHandler panel.MouseMove, AddressOf BorderlessResize_MouseMove
AddHandler panel.MouseUp, AddressOf BorderlessResize_MouseUp
AddHandler panel.MouseEnter, AddressOf BorderlessResize_MouseEnter
AddHandler panel.MouseLeave, AddressOf BorderlessResize_MouseLeave
Me.Controls.Add(panel)
panel.BringToFront()
End Sub
Private Sub BorderlessResize_MouseDown(sender As Object, e As MouseEventArgs)
Debug.Write("MouseDown")
If e.Button = MouseButtons.Left Then
m_utdResizeState = DirectCast(sender, Control).Tag
End If
End Sub
Protected Sub BorderlessResize_MouseMove(sender As Object, e As MouseEventArgs)
If m_utdResizeState <> ResizeState.None Then
BorderlessResize_ResizeForm(m_utdResizeState)
Refresh()
End If
End Sub
Protected Sub BorderlessResize_MouseEnter(sender As Object, e As EventArgs)
BorderlessResize_UpdateCursor(DirectCast(sender, Control).Tag)
End Sub
Protected Sub BorderlessResize_MouseLeave(sender As Object, e As EventArgs)
Me.Cursor = Cursors.Default
End Sub
Private Sub BorderlessResize_UpdateCursor(state As ResizeState)
If (state = ResizeState.East OrElse state = ResizeState.West) Then
Me.Cursor = Cursors.SizeWE
ElseIf (state = ResizeState.North OrElse state = ResizeState.South) Then
Me.Cursor = Cursors.SizeNS
ElseIf (state = ResizeState.SE OrElse state = ResizeState.NW) Then
Me.Cursor = Cursors.SizeNWSE
ElseIf (state = ResizeState.NE OrElse state = ResizeState.SW) Then
Me.Cursor = Cursors.SizeNESW
Else Me.Cursor = Cursors.Default
End If
End Sub
Private Sub BorderlessResize_ResizeForm(ResizeVal As ResizeState)
Dim Location As Point = New Point(Cursor.Position.X - Me.Left, Cursor.Position.Y - Me.Top)
Select Case ResizeVal
Case ResizeState.North
If Me.Height - Location.Y <= Me.MinimumSize.Height Then Return
Me.Height = Me.Height - Location.Y
Me.Top = Me.Top + Location.Y
Exit Select
Case ResizeState.South
Me.Height = Location.Y
Exit Select
Case ResizeState.East
Me.Width = Location.X
Exit Select
Case ResizeState.West
If Me.Width - Location.X <= Me.MinimumSize.Width Then Return
Me.Width = Me.Width - Location.X
Me.Left = Me.Left + Location.X
Exit Select
Case ResizeState.NE
BorderlessResize_ResizeForm(ResizeState.North)
BorderlessResize_ResizeForm(ResizeState.East)
Exit Select
Case ResizeState.SE
BorderlessResize_ResizeForm(ResizeState.South)
BorderlessResize_ResizeForm(ResizeState.East)
Exit Select
Case ResizeState.SW
BorderlessResize_ResizeForm(ResizeState.South)
BorderlessResize_ResizeForm(ResizeState.West)
Exit Select
Case ResizeState.NW
BorderlessResize_ResizeForm(ResizeState.North)
BorderlessResize_ResizeForm(ResizeState.West)
Exit Select
End Select
End Sub
Protected Sub BorderlessResize_MouseUp(sender As Object, e As MouseEventArgs)
If e.Button = MouseButtons.Left Then
m_utdResizeState = ResizeState.None
End If
End Sub
Default Context Menu
Private Const WS_SYSMENU As Integer = &H80000
Private Const WS_MINIMIZEBOX As Integer = &H20000
Private Const WS_MAXIMIZEBOX As Integer = &H10000
Protected Overrides ReadOnly Property CreateParams As System.Windows.Forms.CreateParams
Get
Dim p = MyBase.CreateParams
p.Style = WS_SYSMENU + WS_MINIMIZEBOX + WS_MAXIMIZEBOX
Return p
End Get
End Property
<DllImport("user32.dll")>
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer,
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
Private Const WM_POPUPSYSTEMMENU = &H313
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)
Dim p = MousePosition.X + (MousePosition.Y * &H10000)
SendMessage(Me.Handle, WM_POPUPSYSTEMMENU, 0, p)
End Sub
Moving and Resizing works just fine, but my Context Menu is always displayed in the top left corner and MOVE and SIZE Options are not working because they need the Default Window Frame which I removed.
So is there a possibility to implement my solutions for Moving and Resizing into the Context Menu?
Is
SOLUTION 1
with WndPrc Overwrite
Private Sub Header_MouseDown(sender As Object, ByVal e As MouseEventArgs)
If e.Button = MouseButtons.Right Then
Dim p As IntPtr = ((MousePosition.Y * WindowStyles.WS_MAXIMIZEBOX) + (MousePosition.X And &HFFFF))
Dim test As IntPtr = SendMessage(Me.Handle, WindowMessages.WM_POPUPSYSMENU, 0, p)
End If
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
If (m.Msg = WindowMessages.WM_SYSCOMMAND)
'Do SOmething with m.wParam
Else
MyBase.WndProc(m)
End If
End Sub
SOLUTION 2
with TrackMenuBar
Private Sub Header_MouseDown(sender As Object, ByVal e As MouseEventArgs)
If e.Button = MouseButtons.Right Then'Alternate Solution
Dim sysMenu As IntPtr = GetSystemMenu(Me.Handle, False)
Dim cmd As IntPtr = TrackPopupMenu(
sysMenu,
TrackPopupMenuOptions.TOPALIGN Or
TrackPopupMenuOptions.NOANIMATION Or
TrackPopupMenuOptions.RETURNCMD Or
TrackPopupMenuOptions.LEFTALIGN Or
TrackPopupMenuOptions.RIGHTBUTTON,
MousePosition.X,
MousePosition.Y,
IntPtr.Zero,
Me.Handle,
IntPtr.Zero)
'Do Something with cmd
End If
End Sub
i have created a label within a panel and want to move it vertically .
moving horizontally works using:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Start()
If lbl1.Location.X + lbl1.Width < 0 Then
lbl1.Location = New Point(Panel5.Width, lbl1.Location.Y)
Else
lbl1.Location = New Point(lbl1.Location.X - 4, lbl1.Location.Y)
End If
End Sub
as u see in the above code (X) and (Y) is used for left and right drawing point. what is the drawing point for top and bottom
Your Horizontal routine:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If lbl1.Location.X + lbl1.Width < 0 Then
lbl1.Location = New Point(Panel5.Width, lbl1.Location.Y)
Else
lbl1.Location = New Point(lbl1.Location.X - 4, lbl1.Location.Y)
End If
End Sub
Converted to a Vertical routine:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If lbl1.Location.Y + lbl1.Height < 0 Then
lbl1.Location = New Point(lbl1.Location.X, Panel5.Height)
Else
lbl1.Location = New Point(lbl1.Location.X, lbl1.Location.Y - 4)
End If
End Sub
take your idea n modify for my own requirement:
Private isFirstTime As Boolean = True
Public Sub RunningText(ByVal lbl As Label, ByVal pnl As Panel, Optional fromRightBottom As Boolean = True, Optional ByVal isHorizontal As Boolean = True)
If isFirstTime Then
If isHorizontal Then
If fromRightBottom Then
lbl.Location = New Point(pnl.Width, lbl.Location.Y)
Else
lbl.Location = New Point(-1 * lbl.Width, lbl.Location.Y)
End If
Else
If fromRightBottom Then
lbl.Location = New Point(lbl.Location.X, pnl.Height)
Else
lbl.Location = New Point(lbl.Location.X, 0)
End If
End If
isFirstTime = False
End If
If isHorizontal Then
If fromRightBottom Then
If lbl.Location.X + lbl.Width < 0 Then
lbl.Location = New Point(pnl.Width, lbl.Location.Y)
Else
lbl.Location = New Point(lbl.Location.X - 4, lbl.Location.Y)
End If
Else
If lbl.Location.X + pnl.Width > lbl.Width + pnl.Width Then
lbl.Location = New Point(-1 * lbl.Width, lbl.Location.Y)
Else
lbl.Location = New Point(lbl.Location.X + 4, lbl.Location.Y)
End If
End If
Else
If fromRightBottom Then
If lbl.Location.Y + lbl.Height < 0 Then
lbl.Location = New Point(lbl.Location.X, pnl.Height)
Else
lbl.Location = New Point(lbl.Location.X, lbl.Location.Y - 4)
End If
Else
If lbl.Location.Y + lbl.Height > lbl.Height + pnl.Height Then
lbl.Location = New Point(lbl.Location.X, 0)
Else
lbl.Location = New Point(lbl.Location.X, lbl.Location.Y + 4)
End If
End If
End If
End Sub
i want to maintain the location of picturebox2 which is inside a panel. in my case, the image looks like this.. https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ8SUu7ZXBJVXrhic-Xou9OsW4h7QDd8yH5xhYtV3DlnJ0Q1UVJiw (there's a map /picturebox1/ and the color green locator or pointer is another picturebox /picturebox2/)
is it possible to zoom in and zoom out the image without losing the right coordinates? Because i want to maintain the location of the locator(picturebox2) in the map (picturebox1)
so far, i can now zoom in and zoom out the image in the scrollable panel using trackbar. but my only problem is that, the picturebox2 (another image above the picturebox1) needs to move its location as picturebox1 is zooming.
Public ClassForm1
Private img original As Image
Private m_PanStartPoint As New Point
Private n_PanStartPoint As New Point
Private Sub Form1_Load(ByVal sender AsSystem.Object, ByVal e AsSystem.EventArgs) Handles MyBase.Load
imgoriginal = Image.FromFile("C:\New Folder\picture1.jpg")
PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
zoomSlider.Minimum = 1
zoomSlider.Maximum = 5
zoomSlider.SmallChange = 1
zoomSlider.LargeChange = 1
zoomSlider.UseWaitCursor = False
Me.DoubleBuffered = True
Panel1.AutoScroll = True
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
PictureBox1.Parent = PictureBox1
PictureBox2.Parent = PictureBox1
PictureBox1.BackColor = Color.Transparent
Dim mstream As NewSystem.IO.MemoryStream()
PictureBox1.Image = Image.FromStream(mstream)
PictureBox2.Location = NewSystem.Drawing.Point(100, 100)
End Sub
Public Function pictureboxzoom(ByValimgAsImage, ByVal size AsSize) AsImage
Dim bm As Bitmap = New Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height))
Dim grap As Graphics = Graphics.FromImage(bm)
grap.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
Return bm
End Function
Private Sub zoomSlider_Scroll(ByVal sender AsSystem.Object, ByVal e AsSystem.EventArgs) Handles zoomSlider.Scroll
If zoomSlider.Value> 0 Then
PictureBox1.Image = Nothing
PictureBox1.Image = pictureboxzoom(imgoriginal, New Size(zoomSlider.Value, zoomSlider.Value))
End If
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender AsObject, ByVal e AsSystem.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
m_PanStartPoint = NewPoint(e.X, e.Y)
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender AsObject, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim DeltaX As Integer = (m_PanStartPoint.X - e.X)
Dim DeltaY As Integer = (m_PanStartPoint.Y - e.Y)
Panel1.AutoScrollPosition = _
New Drawing.Point((DeltaX - Panel1.AutoScrollPosition.X), _
(DeltaY - Panel1.AutoScrollPosition.Y))
Button1.Location = New System.Drawing.Point(0, 0)
End If
End Sub
End Class
I have this program that when I typed the same text on the form and to the other form will add one (+1) to the label of my last form..There were no errors but when I wrote the correct answers on the textboxes it just gave me a number 1 instead of 10. Please help me guys. Thanks in advance :)
here is my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frm4 = New Form4()
frm1 = New Form1()
Dim Textbox1 As New TextBox
Dim Label3 As New Label
If Textbox1.Text = frm1.TextBox2.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox2.Text = frm1.TextBox4.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox3.Text = frm1.TextBox6.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox4.Text = frm1.TextBox8.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox5.Text = frm1.TextBox10.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox6.Text = frm1.TextBox12.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox7.Text = frm1.TextBox14.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox8.Text = frm1.TextBox16.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox9.Text = frm1.TextBox18.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox10.Text = frm1.TextBox20.Text Then
frm4.Label3.Text = (frm4.Label3.Text) + 1
End If
End If
End If
End If
End If
End If
End If
End If
End If
frm4.Show()
Else
frm4.Label3.Text = frm4.Label3.Text
frm4.Show()
End If
End Sub
Form 2 codes:
Public Class Form2
Private frm1 As Form1 ' remove the new here because it creates a new instance of Form1'
Private frm3 As Form3
Public lbl As New Label ' not needed?'
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(lbl)
End Sub
Public Sub New(ByVal callerInstance As Form1)
' Call required if you add your constructor manually
InitializeComponent()
' save the instance of the Me variable passed to this constructor
frm1 = callerInstance
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Dim Label21 As Label = New Label ' this label is not needed'
' Dim Textbox1 As TextBox = New TextBox ' this textbox is not needed'
' Create a new instance of Form3, do not use the automatic Form3 instance
' automatically created by VB.NET
frm3 = New Form3()
' now you are referring to the caller instance of Form1 '
' where there is the textbox filled with your text '
frm3.Label21.Text = frm1.TextBox1.Text
frm3.Label22.Text = frm1.TextBox3.Text
frm3.Label23.Text = frm1.TextBox5.Text
frm3.Label24.Text = frm1.TextBox7.Text
frm3.Label25.Text = frm1.TextBox9.Text
frm3.Label26.Text = frm1.TextBox11.Text
frm3.Label27.Text = frm1.TextBox13.Text
frm3.Label28.Text = frm1.TextBox15.Text
frm3.Label29.Text = frm1.TextBox17.Text
frm3.Label30.Text = frm1.TextBox19.Text
frm3.Show()
End Sub
End Class
Form1 Codes:
Public Class Form1
Private frm2 As Form2
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If frm2 Is Nothing Then
frm2 = New Form2(Me)
AddHandler frm2.FormClosed, AddressOf Me.Form2HasBeenClosed
Dim Label21 As Label = New Label
frm2.Label21.Text = TextBox1.Text
frm2.Label21.ForeColor = Color.Black
Dim Label22 As Label = New Label
frm2.Label22.Text = TextBox2.Text
frm2.Label22.ForeColor = Color.Black
Dim Label23 As Label = New Label
frm2.Label23.Text = TextBox3.Text
frm2.Label23.ForeColor = Color.Black
Dim Label24 As Label = New Label
frm2.Label24.Text = TextBox4.Text
frm2.Label24.ForeColor = Color.Black
Dim Label25 As Label = New Label
frm2.Label25.Text = TextBox5.Text
frm2.Label25.ForeColor = Color.Black
Dim Label26 As Label = New Label
frm2.Label26.Text = TextBox6.Text
frm2.Label26.ForeColor = Color.Black
Dim Label27 As Label = New Label
frm2.Label27.Text = TextBox7.Text
frm2.Label27.ForeColor = Color.Black
Dim Label28 As Label = New Label
frm2.Label28.Text = TextBox8.Text
frm2.Label28.ForeColor = Color.Black
Dim Label29 As Label = New Label
frm2.Label29.Text = TextBox9.Text
frm2.Label29.ForeColor = Color.Black
Dim Label30 As Label = New Label
frm2.Label30.Text = TextBox10.Text
frm2.Label30.ForeColor = Color.Black
Dim Label31 As Label = New Label
frm2.Label31.Text = TextBox11.Text
frm2.Label31.ForeColor = Color.Black
Dim Label32 As Label = New Label
frm2.Label32.Text = TextBox12.Text
frm2.Label32.ForeColor = Color.Black
Dim Label33 As Label = New Label
frm2.Label33.Text = TextBox13.Text
frm2.Label33.ForeColor = Color.Black
Dim Label34 As Label = New Label
frm2.Label34.Text = TextBox14.Text
frm2.Label34.ForeColor = Color.Black
Dim Label35 As Label = New Label
frm2.Label35.Text = TextBox15.Text
frm2.Label35.ForeColor = Color.Black
Dim Label36 As Label = New Label
frm2.Label36.Text = TextBox16.Text
frm2.Label36.ForeColor = Color.Black
Dim Label37 As Label = New Label
frm2.Label37.Text = TextBox17.Text
frm2.Label37.ForeColor = Color.Black
Dim Label38 As Label = New Label
frm2.Label38.Text = TextBox18.Text
frm2.Label38.ForeColor = Color.Black
Dim Label39 As Label = New Label
frm2.Label39.Text = TextBox19.Text
frm2.Label39.ForeColor = Color.Black
Dim Label40 As Label = New Label
frm2.Label40.Text = TextBox20.Text
frm2.Label40.ForeColor = Color.Black
End If
If frm2 IsNot Nothing Then
frm2.Show(Me) 'Show Second Form
Me.Hide()
End If
End Sub
Sub Form2HasBeenClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
frm2 = Nothing
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Form 3 Code:(instance calling to form1)
Public Class Form3
Private frm3 As Form3
Private frm1 As Form1
Private frm4 As Form4
Public Sub New1(ByVal callerInstance As Form1)
' Call required if you add your constructor manually
InitializeComponent()
' save the instance of the Me variable passed to this constructor
frm1 = callerInstance
End Sub
End Class
If you want show in label number of right answers then
You need compare all user answers with right answers
And count times when answers are same:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frm4 = New Form4()
frm1 = New Form1()
Dim Textbox1 As New TextBox
Dim Label3 As New Label
Dim sameQnt As Int32 = 0
If Textbox1.Text = frm1.TextBox2.Text Then sameQnt += 1
If Textbox2.Text = frm1.TextBox4.Text Then sameQnt += 1
If Textbox3.Text = frm1.TextBox6.Text Then sameQnt += 1
If Textbox4.Text = frm1.TextBox8.Text Then sameQnt += 1
If Textbox5.Text = frm1.TextBox10.Text Then sameQnt += 1
If Textbox6.Text = frm1.TextBox12.Text Then sameQnt += 1
If Textbox7.Text = frm1.TextBox14.Text Then sameQnt += 1
If Textbox8.Text = frm1.TextBox16.Text Then sameQnt += 1
If Textbox9.Text = frm1.TextBox18.Text Then sameQnt += 1
If Textbox10.Text = frm1.TextBox20.Text Then sameQnt += 1
'Print reslut
frm4.Label3.Text = sameQnt.ToString()
frm4.Show()
End Sub
Here is my new answer
Public Class MainForm '<----Your main form class name
Dim frm4 = New Form4()'<----declare it here
Dim frm1 = New Form1()'<----declare it here
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Textbox1 As New TextBox
Dim Label3 As New Label
Dim i As Integer = 0
If Textbox1.Text = frm1.TextBox2.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox2.Text = frm1.TextBox4.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox3.Text = frm1.TextBox6.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox4.Text = frm1.TextBox8.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox5.Text = frm1.TextBox10.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox6.Text = frm1.TextBox12.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox7.Text = frm1.TextBox14.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox8.Text = frm1.TextBox16.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox9.Text = frm1.TextBox18.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
If TextBox10.Text = frm1.TextBox20.Text Then i += 1 'frm4.Label3.Text = (frm4.Label3.Text) + 1
frm4.Label3.Text = i.ToString()
frm4.Show()
End Sub
End Class