dcsimg
Results 1 to 14 of 14

Thread: [RESOLVED] Drag Textbox to a Picturebox and Save result

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2018
    Posts
    26

    Resolved [RESOLVED] Drag Textbox to a Picturebox and Save result

    Dear Gurus,
    I'm developing an application where I would like to add a Form in which there will be a picturebox and some textboxes.
    What I'm looking for is the code to select one textbox and move it (copying it) into the picture box. Then, I would like to save the image in the picturebox with the textboxes added to it as a picture in an SQL Express table.

    Is it something feasible? Has anybody any suggestion on which code I could use?

    Thanks,
    A.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,166

    Re: Drag Textbox to a Picturebox and Save result

    Would I be correct in assuming that what you actually mean is that you want to copy the text that's in the TextBox onto the PictureBox somehow, rather than the TextBoxes themselves?

    I would suggest that you start here to learn how drag and drop works. Be sure to read at least the first three posts to learn general principles that can be applied to any D&D scenario. Once you know how to D&D, you will then need to learn how to use GDI+ to draw text onto an Image, which is simple enough using the Graphics class. Finally, you'll need to learn how to save an Image into a database. If you follow the CodeBank link in my signature below, you'll find that D&D thread listed, along with threads on GDI+ drawing and saving images in databases.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2018
    Posts
    26

    Re: Drag Textbox to a Picturebox and Save result

    Dear jmcilhinney,
    I would like to display the content of the textboxes in the PictureBox's locations accordingly to the user's choice on where to locate them and then I would like to save the resulting image together with the image defaulted in the PictureBox.
    I just thought of drag and drop method, but maybe there is something smarter and easier.

    Do you have any suggestion?

    Thanks,
    A.

  4. #4
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    325

    Re: Drag Textbox to a Picturebox and Save result

    Here is a drag drop example that makes a textbox on the picturebox at the drop location if you want to try that. Then a new bitmap copy of the picturebox is created for saving.

    Name:  a3.gif
Views: 26
Size:  99.6 KB

    Code:
    Public Class Form8
    
        Private TbList As New List(Of TextBox)
    
        Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            TextBox1.Text = "Saturn"
            TextBox1.Font = New Font("Tahoma", 18, FontStyle.Bold)
            TextBox1.BackColor = Color.AntiqueWhite
            TextBox1.ForeColor = Color.MidnightBlue
    
            TextBox2.Text = "Planet 7"
            TextBox2.Font = New Font("Tahoma", 14, FontStyle.Italic)
            TextBox2.BackColor = Color.MidnightBlue
            TextBox2.ForeColor = Color.AntiqueWhite
    
            PictureBox1.AllowDrop = True
            PictureBox1.BackColor = Color.White
            PictureBox1.BackgroundImage = Image.FromFile("c:\bitmaps\saturn.png")
            PictureBox1.BackgroundImageLayout = ImageLayout.Zoom
    
            PictureBox2.BackColor = Color.Black
            PictureBox2.BackgroundImageLayout = ImageLayout.None
    
        End Sub
    
        Private Sub PictureBox1_DragDrop(sender As Object, e As DragEventArgs) Handles PictureBox1.DragDrop
    
            Dim tb As TextBox = DirectCast(e.Data.GetData(GetType(TextBox)), TextBox)
            Dim t1 As New TextBox
    
            t1.Location = New Point(Me.PointToClient(Control.MousePosition).X - Me.PictureBox1.Left,
                                    Me.PointToClient(Control.MousePosition).Y - Me.PictureBox1.Top)
            t1.Text = tb.Text
            t1.Font = tb.Font
            t1.ForeColor = tb.ForeColor
            t1.BackColor = tb.BackColor
    
            PictureBox1.Controls.Add(t1)
    
        End Sub
    
        Private Sub PictureBox1_DragEnter(sender As Object, e As DragEventArgs) Handles PictureBox1.DragEnter
            If e.Data.GetDataPresent(GetType(TextBox)) Then
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.None
            End If
        End Sub
    
        Private Sub TextBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseDown
            TextBox1.DoDragDrop(TextBox1, DragDropEffects.Copy)
        End Sub
    
        Private Sub TextBox2_MouseDown(sender As Object, e As MouseEventArgs) Handles TextBox2.MouseDown
            TextBox2.DoDragDrop(TextBox2, DragDropEffects.Copy)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim bmp As New Bitmap(PictureBox1.ClientSize.Width, PictureBox1.ClientSize.Height)
    
            PictureBox1.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
    
            PictureBox2.BackgroundImage = bmp
    
            bmp.Save("c:\test\test1.png")
    
        End Sub
    End Class

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,166

    Re: Drag Textbox to a Picturebox and Save result

    Drag'n'drop should work fine. You just have to put some thought into the logic, do some research on how drag'n'drop works and then make an effort to implement the logic you came up with. If you do that and encounter an issue then I'll be more than happy to assist further. If you're hoping that I'll just write the code for you, I'm afraid that you'll be disappointed.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2018
    Posts
    26

    Re: Drag Textbox to a Picturebox and Save result

    Dear tommytwotrain,
    you have been like GOD for me!

    Actually the code you suggested works fine, but there is only one issue. The PictureBox where I'm moving the textboxes is located almost at the center of the screen and when I'm dragging into the textboxes, at the moment in which I release the mouse, the system automatically moves them up and right of the Picturebox.

    I guess the statement to work on is:
    Code:
    t1.Location = New Point(Me.PointToClient(Control.MousePosition).X - Me.PictureBox1.Left,
                                    Me.PointToClient(Control.MousePosition).Y - Me.PictureBox1.Top)
    How can I modify it so that the textboxes will stay in the same place where I locate them?

    Thanks,
    A.

  7. #7
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    325

    Re: Drag Textbox to a Picturebox and Save result

    Quote Originally Posted by AndreB82 View Post
    Dear tommytwotrain,
    you have been like GOD for me!

    Actually the code you suggested works fine, but there is only one issue. The PictureBox where I'm moving the textboxes is located almost at the center of the screen and when I'm dragging into the textboxes, at the moment in which I release the mouse, the system automatically moves them up and right of the Picturebox.

    I guess the statement to work on is:
    Code:
    t1.Location = New Point(Me.PointToClient(Control.MousePosition).X - Me.PictureBox1.Left,
                                    Me.PointToClient(Control.MousePosition).Y - Me.PictureBox1.Top)
    How can I modify it so that the textboxes will stay in the same place where I locate them?

    Thanks,
    A.
    Yes that line of code locates the textbox.

    I am not sure what the problem is. The example works fine for me all over the screen. Does the example work for you?

    Is the picturebox on the form or another panel or something? Instead of ME. for the form in that line Try the parent of the picturebox ie Panel1.PointToClient.

    Play around with it.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2018
    Posts
    26

    Re: Drag Textbox to a Picturebox and Save result

    Dear tommytwotrain,
    in fact the PictureBox was within a Panel.
    Followed your suggestion and now it works fine!

    Thanks a lot for your support!
    A.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2018
    Posts
    26

    Re: Drag Textbox to a Picturebox and Save result

    Dear tommytwotrain,
    probably this is not the right way, but I still have a small issue...

    When I drop the textbox then it seems to be locked; in other words I cannot neither move it further nor cancel the dropping.

    How can I enable these features? Is there a control that I could set to enable them?

    Thanks,
    A.

  10. #10
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    325

    Re: Drag Textbox to a Picturebox and Save result

    Quote Originally Posted by AndreB82 View Post
    Dear tommytwotrain,
    probably this is not the right way, but I still have a small issue...

    When I drop the textbox then it seems to be locked; in other words I cannot neither move it further nor cancel the dropping.

    How can I enable these features? Is there a control that I could set to enable them?

    Thanks,
    A.
    No there is no magic control for that.

    You can add code that will let you move the textbox in the mouse move event. Basically move the textbox to the mouse location.

    However this is what @jmc was talking about. You should study the basic options for doing something and then develop an overall strategy for what and how your application is going to work.

    This choice of what to do needs to be careful and depend on the next thing you will want to do after you do this thing.

    You can also just use the GDI+ vector drawing features of .net to draw the text on the picture box drawing surface. Not even have a clunky old text box you dont even need to drag around.

    Think about it. What is a text box? I will tell you. Its a rectangle drawn on the screen with some text drawn on top of the rectangle. One can draw that easily oneself.

    The problem is you dont know gdi+ drawing and most likely it will be easier for now to drag the text box around in terms of learning and understanding the code.

    The decision again becomes how and why am I doing this? And eventually how much time and money am I willing to spend on this?

    Since I dont know what you are doing I cant decide for you. Its one of those things that one basically needs to try it both ways to know what to do next. I suggest you do that.



    Is this for school or what are you doing exactly?

  11. #11
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    325

    Re: Drag Textbox to a Picturebox and Save result

    PS Here are some examples of both ways. Moving controls vs GDI+.

    Moving controls with the mouse:

    https://social.msdn.microsoft.com/Fo...orum=vbgeneral


    Drawing with GDI+

    https://social.msdn.microsoft.com/Fo...orum=vbgeneral

    https://social.msdn.microsoft.com/Fo...1-3def4c781830

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Sep 2018
    Posts
    26

    Re: Drag Textbox to a Picturebox and Save result

    Dear tommytwotrain,
    Thanks for your examples - I’ll have a look at them for sure and get back in case of doubts.
    Coming to your questions:
    - I’m doing an application for hobby, not for school or business;
    - Actually the application is about basket. I have the list of players on textboxes on the left and right sides of the windowsform and the picturebox with the image of a basket pitch in the center. I simply would like to move the names of the players in the positions they have in the pitch during the match and then save the resulting image to an SQL server db, so that I can reuse it if I need to check. Very easy.

    I know probably, it could be boring for you to support, but please consider that I always check before writing here...as I told you I’m not a professional developer, I’m doing this for hobby and I have a limited availability of time. I’m curious and I always research the stuff I want to code, if I come here is because I cannot get something sorted out...

    Sorry for this.

    A.

  13. #13
    Hyperactive Member
    Join Date
    Jun 2018
    Posts
    325

    Re: Drag Textbox to a Picturebox and Save result

    Quote Originally Posted by AndreB82 View Post
    Dear tommytwotrain,
    Thanks for your examples - I値l have a look at them for sure and get back in case of doubts.
    Coming to your questions:
    - I知 doing an application for hobby, not for school or business;
    - Actually the application is about basket. I have the list of players on textboxes on the left and right sides of the windowsform and the picturebox with the image of a basket pitch in the center. I simply would like to move the names of the players in the positions they have in the pitch during the match and then save the resulting image to an SQL server db, so that I can reuse it if I need to check. Very easy.

    I know probably, it could be boring for you to support, but please consider that I always check before writing here...as I told you I知 not a professional developer, I知 doing this for hobby and I have a limited availability of time. I知 curious and I always research the stuff I want to code, if I come here is because I cannot get something sorted out...

    Sorry for this.

    A.
    I see. Thanks for explaining.

    We help lots of questions and so the questions tend to go in a few patterns. Some just want someone to write the code for them. Which is fine in some cases if someone feels like writing the code. I sometimes do when I like the subject and or need practice. But we dont know and try to ask what you want vs guessing. And if its for school then that requires something else too.

    As you can see I like making examples but we all need to know what you want otherwise its just guessing wrong and that gets old.

    And then in many cases we can just give you our opinion but if you dont understand the basic underlying logic then you are just copying code and after this copy then here comes the next question. Again it gets old for all of us.

    Sometimes what we decide is the best way for you is to learn how to do it. Sometimes we just make you an example to teach you. Sometimes we are wrong or dont really know.


  14. #14

    Thread Starter
    Junior Member
    Join Date
    Sep 2018
    Posts
    26

    Re: Drag Textbox to a Picturebox and Save result

    Dear tommytwotrain,
    Don’t get me wrong, but the application itself now has more then 10k lines of code and I made it starting from scratch without any basics of vb.net programming. I have done all of this searching, reading and trying. That’s why I told you that if I come here is really because I don’t have any clue of how implement something. I don’t need somebody to write the code on my behalf, I need your deep expertise to get the right direction. But I don’t need a reply like: “Go and look around, these are some links...I know how to do, you need to learn”. It’s kind of frustrating...’cause I do invest time to search and read.

    Now, hoping to have clarified, do you think I’m on the right direction for my scope? Do you have any consistent suggestion for me?

    Thanks,
    A.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width