Control flickers while its location is continuously changed - vb.net

I have a button inside a form. The button moves horizontally from one point to another by continuously varying the Button.Location.X from 0 to 1000 using a timer. See code below.
Timer1.Interval = 100
Dim i as Integer = -1
Private Sub Timer1_tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Button1.Location = New Point(i, 50)
If i = 1000
i = 0
End If
i = i + 1
End Sub
Now the problem is while the button is moving it flickers. How do I stop the flickering?
Tried using Hans Passant's answer to this question but it's not working.

Related

I want the picture box to move every second to the right by one place

Here is where I have tried to implement the code to change the location of the picture box but it doesn't seem to be working, I want the picture to move to the right:
Public Class Form1
Dim mypicturebox As New PictureBox
Dim randval As Integer
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Label1.Text = "hello " & TextBox1.Text
Timer1.Enabled = True
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Timer1.Interval = 2000
Timer1.Enabled = True
mypicturebox.Location = New Point(mypicturebox.Location.X + 5, mypicturebox.Location.Y + 5)
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Label1.Font = New Font(Label1.Font.FontFamily, Label1.Font.Size +
mypicturebox.Location = New Point(mypicturebox.Location.X + 5, mypicturebox.Location.Y)
End Sub
End Class
Your current Timer handler - if the Timer is actually firing - is just going to drift your PictureBox off to the right by 5px each Timer period.
If you want a random position you need a way to get a random number. Random is your best bet.
You also need to ensure that the generated random position is "valid" for both the size of your screen and the size of the PictureBox otherwise the PictureBox might be either entirely invisible (off screen) or partially visible.
Here's some basic code to get two random numbers between -100 and 100 to get you started:
Dim myRand As Random = New Random(Now.Second)
Dim newX As Integer = myRand.Next(-100, 100)
Dim newY As Integer = myRand.Next(-100, 100)
(Now.Second seeds the generation of myRandom so that it should generate different starting numbers each time.)
Then you can use these as your PictureBox location points:
mypicturebox.Location = New Point(newX, newY)
Play around with limiting your locations and you should get what you want.
UPDATE
Ok, scrap all that as you've changed your requirement description ...
Just add whatever offset you need to your X position and you're done, provided your Timer is firing. You still need to watch your screen limits.

PictureBox scrolling past edges of form erases contents

I am trying to create a simple winform application in VB. I have a form, a picturebox with a background image and a button. When I press the button, I want the picturebox to move left across the form until it disappears off the left side. Then I want it to reappear on the right side and move to the left again.
Initial condition:
The problem seems to be that the areas of the picturebox that exceed the boundaries of the form get erased. So the first time around the images slides nicely off the left side of the form and never comes back to the right side. Or rather, it does come back, I just can see it.
Second scroll around, parts of image in picturebox that exceeded the boundaries of the form have been erased (left) or distorted (right):
In the code below I have deliberately changed the boundaries for the picturebox so that I can see that it is actually looping back around. But as you can see in this image, the parts of the picture box that exceeded the boundaries of the form are either erased or distorted.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Do
PictureBox1.Left -= 1
Threading.Thread.Sleep(2)
If PictureBox1.Left <= -20 Then 'If PictureBox1.Left <= PictureBox1.Width Then
PictureBox1.Left = Me.Width - 40 'PictureBox1.Left = Me.Width
End If
Loop
End Sub
End Class
I have tried this with both VS2019 and VS2017 on two different computers and the same thing happens. I have also tried replacing the picturebox with a textbox and much the same thing happens.
I changed my code to this, and now it works perfectly.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 25
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PictureBox1.Left -= 1
If PictureBox1.Left <= -PictureBox1.Width Then
PictureBox1.Left = Me.Width
End If
End Sub
End Class

Visual Basic Multiple Button to show Mulitple Picturebox/Richtext

So here's what I want. When i click on a button it will show 1 PictureBox and 1 RichtextBox (Which is btw Visible=False so that it will not show unless clicked when the program is opened) as you can see here on my screenshot:
enter image description here
But when I click other button it doesn't change it stays with the first button i clicked when i opened the program. I really don't know the code since I'm new with VB.
Here's the code I used:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Visible = True
RichTextBox1.Visible = True
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PictureBox2.Visible = True
RichTextBox2.Visible = True
End Sub
End Class
Thanks!
Since (I guess) both of the images are right on top of each other, you need to set the first image to invisible again. If not controlled which image is on top depends on the order the images where added.
You could do something like this :
Private Sub hideElements()
For i as Integer = 1 to 6
Me.Controls("PictureBox" & i).Visible = False
Me.Controls("RichTextBox" & i).Visible = False
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Call hideElements()
PictureBox1.Visible = True
RichTextBox1.Visible = True
End Sub
This loop sets all PictureBox1 - Picturebox6 and RichtextBox1 - RichTextBox6 to invisible, now you can set the one you want to show to visible.
So just call hideElements at the beginning of all of your button handler.
If u want to change the amount of images/richtextboxes, you only need to adjust the 6 in the loop.
Hope I could help.

How to Pass Image in ImageList to PictureBox in Reverse Index Order

I have a VB.Net application which has spelling words that a child can type in and it verifies the spelling. On the form, I have a Next and Back button, which advances a PictureBox with items contained in an ImageList. I have 3 items in the ImageList collection with an index[2].
Here's the code that I'm using to advance the Next button to display the next image in the collection:
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
'Get images and place them in the Imagebox on each click.
Static count As Integer = 1
If count > ImageList1.Images.Count - 1 Then
count = 0
End If
PictureBox1.Image = ImageList1.Images(count)
count += 1
End Sub
While this works, I cannot figure out how to get this to work in the reverse order. Here's the OnLoad event handler that starts with the first image in the collection:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Set picturebox with initial picture
PictureBox1.Image = ImageList1.Images(0)
txtSpell.Focus()
End Sub
How can I get the Back button to go backwards in the index from it's current point in the index which is shown in the PicutureBox control? This is where I'm stumped. Tried to re-write the code several times, if I click Next once, I go to the next image index[1] and if click the Back button, it takes me to index[0].
But if click next again, PictureBox jumps to index[2] the last image, rather than going back to index[1]. If I click Back again, the code is blowing up.
Move your count variable (which is badly named, BTW - I've used CurrIdx instead) to a higher visibility. In the previous button's click handler, decrement the index; if it drops below 0, reset it to the ImageList.Images.Count - 1 again. I'd also move the code that actually sets the image index to its own procedure, so that you're not repeating yourself and it's more clear. Something like this should work for you:
Private CurrIdx As Integer = 0
Private Sub UpdateImage()
PictureBox1.Image = ImageList1.Images(CurrIdx)
End Sub
Private Sub PrevButton_Click(sender As Object, e As EventArgs) Handles PrevButton.Click
CurrIdx -= 1
If CurrIdx < 0 Then
CurrIdx = ImageList1.Images.Count - 1
End If
UpdateImage()
End Sub
Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
CurrIdx += 1
If CurrIdx > ImageList1.Images.Count - 1 Then
CurrIdx = 0
End If
UpdateImage()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
UpdateImage()
End Sub
First of all, I would like to give thanks to both Ken White, and valter, for assisting me and opening up my thinking in this approach. I used Ken's advice on my variable naming and valter's approach for my first solution of the btnNext problem.
Thus, as I had a working btnNext to flip through my images in the imagelist1 collection and push them to the picturebox1, I took Ken's advice arranged my code and renamed variables to get btnPrevious to work as such:
Private CounterVar As Integer = 0
Private Sub ImageUpdate()
'Sets the picture box to the first image of the series.
PictureBox1.Image = ImageList1.Images(CounterVar)
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
'Allow the Back button to respond the appropriate PictureBox item.
If CounterVar = 1 Then 'This is the second imagelist item, index[1].
CounterVar = 0 'This is the first imagelist item, index[0].
ImageUpdate()
ElseIf CounterVar = 2 Then 'This is the last imagelist item, index[2].
CounterVar = 1 'This is the second imagelist item, index[1].
ImageUpdate()
Else
CounterVar = CounterVar 'Otherwise, the count remains where it is till Back is clicked.
End If
ImageUpdate()
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
'Setup counter variable to begin the button processing.
CounterVar += 1
'Get Imagebox items and place them in the PictureBox, in order, on each click.
If CounterVar > ImageList1.Images.Count - 1 Then
CounterVar = 0
End If
ImageUpdate()
'On a correct answer, clear the textbox, spell label, and hide the result label,
'then set the focus of the textbox.
txtSpell.Text = String.Empty
lblAnsResult.Text = String.Empty
lblAnsResult.Visible = False
txtSpell.Focus()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Set imagebox to first index image.
ImageUpdate()
'Set focus to the textbox.
txtSpell.Focus()
End Sub
This is what I was trying to accomplish with btnPrevious, without it skipping around or out of order. Although the code can skip around via Next, the user cannot go backward beyond the first image in the Imagebox1 index[0]. This is what I wanted. Again, a BIG THANKS to those who helped me to think it out. That's what being on Stack is all about... learn from the answers you get, and run free developing in confidence... like a happy baby w/out a diaper! Thanks fellas!

Animation of tabpages in vb.net

Is it possible to do animation of tabpages as they switch in VB.NET?
So, when one person switches tabpages the current one might slide to reveal the next.
Is this possible to do, or is it complicated?? Thanks in advance.
You could simply put all the controls that you have on a tabpage on a panel. And slide the panel with a 15 msec timer:
Dim tabPageOffset As Integer
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
tabPageOffset -= 10 `` Tweak to change speed
If tabPageOffset <= 0 Then
tabPageOffset = 0
Timer1.Enabled = False
End If
TabControl1.SelectedTab.Controls(0).Left = tabPageOffset
End Sub
Private Sub TabControl1_Selecting(sender As Object, e As TabControlCancelEventArgs) Handles TabControl1.Selecting
tabPageOffset = e.TabPage.ClientSize.Width
Timer1.Enabled = True
End Sub