PictureBox Image assignment leads to Paint event loop - vb.net

I am creating a Bitmap to load into a PictureBox during the Paint event like this:-
Private Sub MyBox_Paint (ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBox.Paint
MyBox.Image = FunctionReturningBitMap (MyBox.Width, MyBox.Height)
End Sub
This causes the bitmap to appear on the picture box as intended, but for some reason causes the Paint event to be invoked again, leading to a tight loop. What am I doing wrong?
Edit: I have also tried the above but handling the MyBox.Invalidated event. The result is the same.

Related

smooth scroll + auto scroll richtextbox in vb.net

so I tried to making quick text viewer, but...
After ton of Google search, tried this following code:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
RichTextBox1.SelectionStart = RichTextBox1.TextLength
RichTextBox1.ScrollToCaret()
End Sub
Still, they didn't scroll.
So I put this code to Form1_load, they scroll to the end instead.
All I want is auto scrolling, repeatedly.
Just like ACiDview actually
I think you have to add this :
RichTextBox1.focus()

Dynamically changing image in picture box not working

I've been trying to figure this out off-and-on for weeks.
In my VB 2010 Forms application, I have a number of picture boxes which are populated with images from other picture boxes using the drag-and-drop method. That's no problem, it works fine. The picture boxes are all in a groupbox container.
The problem is trying to swap images between two picture boxes on a drag-and drop operation. In other words pBox1 has image.x and pBox2 has image.y. Drag the image from pBox2 to pBox1, and drop it; pBox1 will then have image.y from pBox2 and pBox2 will have image.x from pBox1.
With this example, here's the code I have so far:
Private Sub pBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pBox1.MouseDown
strImageSource = "pBox2" 'strImageSource is a global string variable
[other stuff]
end sub
^ This saves the name of the source picture box to a global string.
Private Sub pBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles pBox1.DragDrop
For Each Control as PictureBox in GroupBox1.Controls.OfType(of PictureBox)()
if Control.Name = strImageSource then
Control.Image = pBox1.Image
end if
next
dim imgTarget as Image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap)
pBox1.image = imgTarget
End Sub
^ This searches for the picture box as named by the strImageSource ("pBox2") and copies the contents of pBox1 into it, and then drops the image that was in pBox2 into pBox1.
I hope this makes sense.
This correctly places the image from pBox2 into pBox1, but it does not switch the image from pBox1 into pBox2. pBox2 is just blank. However, debugging shows that the image in pBox2 is not nothing; it does contain a bitmap. It's just not visible.
Now, just as a test, I added a line to the For Each section that would change the background color of the picture box:
For Each Control as PictureBox in GroupBox1.Controls.OfType(of PictureBox)()
if Control.Name = strImageSource then
Control.Image = pBox1.Image
Control.BackColor = color.red
end if
next
And the back color does change. This tells me that the For Each section is working -- it's finding the control and changing the back color. It's just not showing the image.
Is there something I am overlooking?
Thanks!
Instead of using strImageSource, use a global variable defined as a Picturebox
Private tmpPictureBox As PictureBox
Then store that picturebox reference so you can set its image on DragDrop
Private Sub pBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pBox1.MouseDown
'store the picturebox reference
tmpPictureBox = sender
[other stuff]
end sub
Private Sub pBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles pBox1.DragDrop
'set the first image to the second
tmpPictureBox.Image = sender.image
'set the second image to the first
pBox1.image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap)
End Sub
You should call Control.Refresh() on the PictureBox controls in order to update the images.
OK, this was stupid.
I was doing everything right with one really boneheaded exception. In another part of the code, inexplicably, I was clearing the picture boxes of content after the image was replaced. It may have been a remnant of something I was trying to do unrelated to this problem, and I just never corrected it.
My apologies for that, and thanks for all the responses.

A function is called twice in VB.NET

I have a function that is called twice and I don't know what to do.
This is the code that is called when I press an input button on a WebBrowser:
Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
Document = sender.Document
AddHandler Document.Click, New HtmlElementEventHandler(AddressOf Document_Click)
End Sub
Private Sub Document_Click(sender As Object, e As HtmlElementEventArgs)
Select Case Document.ActiveElement.Id.ToLower
Case "global" : prueba()
Case Else
End Select
End Sub
If you want to see the function called prueba() here it is: http://pastebin.com/Fi5LLX2N
I have a video where I show it, but the annotations are in Spanish: http://www.youtube.com/watch?v=OCJXk3qJwVA
Well I have another problem with my function, as you can see, on the bottom I have put this:
Else
MsgBox("Este ModPack ya lo tienes instalado!")
End If
But it doesn't work. :(
Try this:
PS: It's written on the fly maybe can have some syntax error.
Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
Document = sender.Document
try : removehandler Document.Click, addressof(Document_Click): catch : end try
AddHandler Document.Click, New HtmlElementEventHandler(AddressOf Document_Click)
End Sub
My immediate reaction is a sticky mouse button, but really it's likely due to the web page you are loading having multiple pages being loaded, thus adding the duplicate event handler. Put a breakpoint on this line of code:
AddHandler Document.Click, New HtmlElementEventHandler(AddressOf Document_Click)
You'll likely see it being hit twice. Make sure to only wire up one HtmlElementEventHandler to avoid the double firing of the click event handler. You can check e.Url for a match before wiring up as a possible solution.
in vb.net there is no need to define onclick in html for button because it is handled automatically.so if you are doing this then the click event will fire twise.

Change Cursor as Feedback in DragDrop VB.NET 2010

Working on a DragDrop application. I have a class object (built as an object) which has a GroupBox that is enabled for DragDrop.
In the class I have set it up so that the cursor will change when it enters the groupbox, leaves the groupbox, mouse is down within the groupbox and drag/drop is working. I also set up a label in the class object and changed the text in that label as well. Basically, the text changes in the label as expected including 'FEEDBACK' appearing when I am dragging the object BUT the cursor stays stubbornly as a pointer. This is tghe state of affairs either when I debug the class or when, after I build the class, it is run as part of the main program.
Here are the subs set up in the class.
Private Sub GroupBoxSourceMouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBoxSource.MouseDown
GroupBoxSource.DoDragDrop(GroupBoxSource.Text, DragDropEffects.Copy)
lbl1.Text = "DOWN"
End Sub
Private Sub GroupBoxSourceMouseMove(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.MouseEventArgs) Handles GroupBoxSource.MouseMove
If bolDragDropMouseDown Then
' Initiate dragging.
'GroupBoxSource.DoDragDrop(GroupBoxSource.Text, DragDropEffects.Copy)
End If
bolDragDropMouseDown = False
lbl1.Text = "MOVE"
End Sub
Private Sub GroupBoxSourceMouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBoxSource.MouseEnter
Windows.Forms.Cursor.Current = Cursors.Hand
lbl1.Text = "ENTER"
End Sub
Private Sub GroupBoxSourceMouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBoxSource.MouseLeave
Windows.Forms.Cursor.Current = Cursors.Arrow
lbl1.Text = "LEAVE"
End Sub
Private Sub GroupBoxSourceDragDropFeedback(ByVal sender As Object, ByVal e As GiveFeedbackEventArgs) Handles GroupBoxSource.GiveFeedback
Windows.Forms.Cursor.Current = Cursors.SizeAll
lbl1.Text = "FEEDBACK"
End Sub
Now for the unusual stage. I decided to change the cursor during FormLoad of the main program (which has the class object defined on the form) to a hand and nothing changed. In desperation I set up a 1mS timer and coded that to change the cursor to a hand and sure enough the cursor is now a hand but momentarity flicks back to a pointer when I move the cursor so it looks as if something else is causing the cursor to revert to its default value.
You have to set e.UseDefaultCursor = False in your GiveFeedback event handler to make your cursor change visible. Without it, drag and drop always uses the default cursors.
The cursor you get when the mouse hovers a control is set by the Control.Cursor property. Changing the Cursor.Current property has no effect, the property causes the cursor to change back instantly when you move the mouse.
To easily change the cursor, click on the form and bring up the properties. Then go to Cursor and change it to whatever you want.

DataGridView cell click event in VS 2010

I am a tad new to the DataGrid controls, but I am just curious as to why the first code block below works, but the second code block does not? (Only thing I can see is the
Handles DataGridClaims
syntax
Block 1
Private Sub DataGridClaims_CellContentClick_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridClaims.CellContentClick
If e.RowIndex <> -1 Then
Dim frmViewClaims As New objViewClaim
frmViewClaims.ClaimID = DataGridViewClaims.CurrentRow.Cells("ClaimNum").Value
frmViewClaims.Show()
End If
End Sub
Block 2
Private Sub DataGridClaims_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
If e.RowIndex <> -1 Then
Dim frmViewClaims As New objViewClaim
frmViewClaims.ClaimID = DataGridViewClaims.CurrentRow.Cells("ClaimNum").Value
frmViewClaims.Show()
End If
End Sub
The "handles" keyword in VB.net marks the Function as a listener to the given event. Without the "Handles DataGridClaims", the grid has no way to know to fire your function when the event is triggered.
[See MSDN Doc's][1]
http://msdn.microsoft.com/en-us/library/6k46st1y(v=VS.100).aspx
I am not too familiar with the VB.NET, but CellContentClick is an event which occurs when the content within a cell is clicked.
In order for the program to understand that this is an event you use the keyword Handles in VB.NET. It allows you to wire-up the bindings to event handlers on the event handler methods themselves.
This is the equivalent of += in c# and would look something like
DataGridClaims.CellContentClick += DataGridClaims_CellContentClick;