smooth scroll + auto scroll richtextbox in vb.net - 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()

Related

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.

Mousewheel scroll inconsistent with two tablelayoutpanels

I have two tablelayoutpanels side by side.
The scroll events of both panels are linked so the user scrolls one and the other scrolls too. This works fine:
Private Sub Layout_SidePanel_Scroll(sender As Object, e As ScrollEventArgs) Handles Layout_SidePanel.Scroll
If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then
'make the other panel scroll too
Layout_Main.VerticalScroll.Value = Layout_SidePanel.VerticalScroll.Value
End If
End Sub
Private Sub Layout_Main_Scroll(sender As Object, e As ScrollEventArgs) Handles Layout_Main.Scroll
If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then
'make the other panel scroll too
Layout_SidePanel.VerticalScroll.Value = Layout_Main.VerticalScroll.Value
End If
End Sub
However, when the user uses the mousewheel to scroll it doesn't work at all well. One side will scroll and not the other. Or one will scroll a bit more than the other. I've checked the vertical scroll values of the two panels and can see they don't match up. I need to either get the scrolling to work consistently or disable the mousewheel scrolling.
This is my code for handing the mousewheel events:
Private Sub Layout_Main_MouseWheel(sender As Object, e As MouseEventArgs) Handles Layout_Main.MouseWheel
Layout_SidePanel.VerticalScroll.Value = Layout_Main.VerticalScroll.Value
End Sub
Private Sub Layout_Sidepanel_MouseWheel(sender As Object, e As MouseEventArgs) Handles Layout_SidePanel.MouseWheel
Layout_Main.VerticalScroll.Value = Layout_SidePanel.VerticalScroll.Value
End Sub
Found it!
https://stackoverflow.com/a/5565804/3953342
I need to include .PerformLayout
Layout_SidePanel.VerticalScroll.Value = Layout_Calendar.VerticalScroll.Value
Layout_SidePanel.PerformLayout()

PictureBox Image assignment leads to Paint event loop

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.

.NET TrackBar MouseDown follow cursor?

I currently use this code to fix the bug where if you click somewhere on the Horizontal TrackBar it jumps to the middle then to the end of the TrackBar. So this code fixes that bug, which now jumps to the location you click.
But still a problem remains when I keep my mouse down and move it around the TrackBar the slider should follow but it just resets to beginning position, how do I make it follow right on top of cursor? would I need a timer control for that?
Private Sub tbTest_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbTest.MouseDown
Dim dblValue As Double
'Jump to the clicked location, bug FIX.
dblValue = (Convert.ToDouble(e.X) / Convert.ToDouble(tbTest.Width)) * (tbTest.Maximum - tbTest.Minimum)
tbTest.Value = Convert.ToInt32(dblValue)
End Sub
Make the method handle both the MouseDown() and MouseMove() events like this:
Private Sub tbTest_MovePointer(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbTest.MouseDown, tbTest.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim dblValue As Double
'Jump to the clicked location, bug FIX.
dblValue = (Convert.ToDouble(e.X) / Convert.ToDouble(tbTest.Width)) * (tbTest.Maximum - tbTest.Minimum)
tbTest.Value = Convert.ToInt32(dblValue)
End If
End Sub
*Note the multiple events listed after the Handles keyword at the end of the first line. I also added a check to ensure the left mouse button is down.

Tooltip showing twice

I'm showing a ToolTip on a certain button's MouseHover event. If I go over it once it works but if I leave, wait for the tooltip to disapear and come back on the button, it appears twice. I tried cancelling it on MouseLeave but it still appear twice the seocnd time.
Private Sub someButton_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles someButton.MouseHover
Dim tooltipSearch As New ToolTip()
tooltipSearch.Show("I'm a tooltip"), someButton)
End Sub
Private Sub someButton_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles someButton.MouseLeave
Dim toolTip As New ToolTip()
toolTip.GetToolTip(someButton)
toolTip.Hide(someButton)
toolTip.Dispose()
End Sub
Am I missing something obvious?
You are using the ToolTip not in the way it should be used (also look at the documentation :) ). It is just like any other control, start by dragging it onto your form (like you do with other controls).
If you have no dynamic text to be displayed in your tooltip, you can easily set the text in your constructor, using the method SetToolTip. If you do want some dynamic text, you can use this method in your MouseHover event.
Apart from that, you shouldn't do anything anything else. Just set the right delays on your tooltip and it should work all fine.
Private Sub someButton_MouseHover(sender As Object, e As System.EventArgs) _
Handles someButton.MouseHover
ToolTip1.SetToolTip(someButton, "My name is Steve chamba from south Africa")
End Sub
The answer about dragging the ToolTip control to the form is great. There is a subtlety with Button controls though. They seem to automatically set the tool tip to the text of the button, so if you also call ToolTip1.SetToolTip(myButton, "Button Text") then dot-net draws two copies of the tool tip text when you hover over the button.