Results 1 to 8 of 8

Thread: Another Noob ): Button in Form 1 Draw Line in Form 2

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    4

    Another Noob ): Button in Form 1 Draw Line in Form 2

    Hi, I'm new to visual basic and I have to do an assignment for my class.

    What I need to know is how to make a button from Form1 to draw a line on Form2

    (I know how to draw the line without paint event and can do it on Form1, so all I need is just the code to make the line show in Form2 instead of Form1)



    Just need this question answered so I can continue on ahead with my assignment.

    Thanks for your time.

  2. #2
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: Another Noob ): Button in Form 1 Draw Line in Form 2

    Lots of ways to go here. I would put my line drawing code in a public sub in Form2 and call it from the button click event in Form1. Just prefix your call with the name of the form - something like "Call Form2.MyDrawingSub()".

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    4

    Re: Another Noob ): Button in Form 1 Draw Line in Form 2

    Quote Originally Posted by paulg4ije View Post
    Lots of ways to go here. I would put my line drawing code in a public sub in Form2 and call it from the button click event in Form1. Just prefix your call with the name of the form - something like "Call Form2.MyDrawingSub()".
    Thanks for the reply but I can't understand what you said very well (as I said I'm new, like 3 weeks of programming new)

    What's happening with the button in Form1 is the line it draws is being drawn in Form1.
    I want that line to instead be drawn in Form2 and not be shown in Form1.

  4. #4
    Lively Member
    Join Date
    Dec 2012
    Posts
    122

    Re: Another Noob ): Button in Form 1 Draw Line in Form 2

    Try this:
    In button click at Form1:
    Code:
    Dim SecondForm as New Form2 'you can change Form2 to your second forms name, but its probably Form2
    SecondForm.Label1.Text = "Text" 'change this to draw some line or w/e (SecondForm must stay)
    And then also if you want to show second form just use SecondForm.Show()

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    4

    Re: Another Noob ): Button in Form 1 Draw Line in Form 2

    Quote Originally Posted by silentus View Post
    Try this:
    In button click at Form1:
    Code:
    Dim SecondForm as New Form2 'you can change Form2 to your second forms name, but its probably Form2
    SecondForm.Label1.Text = "Text" 'change this to draw some line or w/e (SecondForm must stay)
    And then also if you want to show second form just use SecondForm.Show()
    Thanks for the reply.

    I know how to put text from Form1 to Form2.

    What I want to know is how to put a line in not text.

    This is what I wrote in the button for Form 1

    Dim grph as Graphics
    Dim BlackPen as New Pen(Color.Black)
    Dim SecondForm as New Form2

    grph = Me.CreateGraphics()

    Will use this line below as an example for the line

    grph.DrawLine(BlackPen, 0, 0, 500, 500)


    Thanks for your help
    Last edited by Nayami; Feb 3rd, 2013 at 01:28 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    4

    Re: Another Noob ): Button in Form 1 Draw Line in Form 2

    Problem Solved! (Finally)

    Here is the answer to my question in case someone has the same problem (:

    Assuming you have 2 forms up already.

    First Form's name is Form1

    Second Form's name is Form 2

    Put a button in Form1

    Double Click the button in Form1

    Enter this

    Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
    Dim BlackPen As New Pen(Color.Black)
    Form2.Show()
    Form2.CreateGraphics.DrawLine(BlackPen, 0, 0, 500, 500)

  7. #7
    New Member
    Join Date
    Oct 2013
    Posts
    9

    Re: Another Noob ): Button in Form 1 Draw Line in Form 2

    Hello silentus,

    I' am new in programming vb.

    I read this problem because i have the same problem only i have create a bitmap construction.
    I have form1 with a button1
    If i click the button1 then he must drawing the rectangle in form2 that i have add to my project.
    Here what i have making already.
    I have try setting form2 at the beginning of the line thats drawing the rectangle bud i have the error message that g is not a member from form2

    Can you help me please what i can do to draw the rectangel to form2 ????

    thank you for your time

    Regards Bert

    Public Class Form1

    ' Declareren van alle variabelen
    Dim genrevierkant = New Rectangle(0, (475 + 80) - 100, 475, 40)
    Dim titelvierkant = New Rectangle(0, (475 + 80) - 100, 475, 60)

    Dim titelplaatsing As New StringFormat
    Dim genreplaatsing As New StringFormat

    Dim titelkleur As Color = Color.Black
    Dim genrekleur As Color = Color.Black

    Dim titelfont As Font = New Font("Segoe UI", 14, FontStyle.Bold)
    Dim genrefont As Font = New Font("Segoe UI", 12, FontStyle.Bold)

    Dim cdhoes As Bitmap


    WithEvents documentPrinter As New Printing.PrintDocument


    Private Sub btnTekenUitslag_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTekenUitslag.Click

    Dim bmp As New Bitmap(476, 475 + 81)

    Dim g As Graphics = Graphics.FromImage(bmp)

    g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality

    Dim pen As New Pen(Brushes.Black, 1)

    g.FillRectangle(Brushes.White, New Rectangle(0, 0, bmp.Width, bmp.Height))

    g.DrawRectangle(pen, 0, 80, 475, 475)

    Me.BackgroundImage = bmp
    cdhoes = bmp

    End Sub
    End Class

  8. #8
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Another Noob ): Button in Form 1 Draw Line in Form 2

    I think you should oppen a separate thread for your issue. That said, you have all your declarations on Form1 so Form2 does not know what you are talking about. Someone sugested before to put the subs that do the work on form2, with all the variables and declarations, and then call that function or sub from Form1

    Call Form2.Function()
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width