Visual Basic Multiple Button to show Mulitple Picturebox/Richtext - vb.net

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.

Related

Controlling program-order when using a TabControl

Here is a simple VB.Net Forms program. The form contains a TabControl, which has 2 pages. On TabPage1 there is a Button, and a PictureBox, which contains a small 'Waiting' image. Initially the PictureBox is hidden. The TabControl starts by showing TabPage1.
What I would like to happen is that when the Button is pressed, the PictureBox is made visible, then, my SlowRoutine() is called, then the PictureBox is hidden, then I swap to TabPage2.
What actually happens is that when the Button is pressed, we wait 2 seconds, then we swap to TabPage2. I never see the PictureBox.
If I uncomment the MessageBox line, just to add a halt to the program-flow, then press the Button, the following occurs: 2 seconds passes, and then the PictureBox and the MessageBox appear. Clicking on the MessageBox closes it, and we go to TabPage2. Flipping back to TabPage1 shows that the PictureBox has been hidden.
The order of events is not happening in a logical way. How can I fix this, please?
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
PictureBox1.Visible = False
PictureBox1.Hide()
PictureBox1.SendToBack()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Visible = True
PictureBox1.Show()
PictureBox1.BringToFront()
SlowRoutine()
' MessageBox.Show("I am waiting")
PictureBox1.Visible = False
PictureBox1.Hide()
PictureBox1.SendToBack()
TabControl1.SelectTab("TabPage2")
End Sub
Private Sub SlowRoutine()
' My SLOW ROUTINE: wait 2 seconds
Threading.Thread.Sleep(2000)
End Sub
End Class
Thanks to all. Here is the working code based on those comments, in case anyone else needs to do a similar task:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
PictureBox1.Visible = False
End Sub
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Visible = True
Await SlowRoutineAsync()
PictureBox1.Visible = False
TabControl1.SelectTab("TabPage2")
End Sub
Private Async Function SlowRoutineAsync() As Task
' My SLOW ROUTINE: wait 2 seconds
Await Task.Delay(2000)
End Function
End Class

VB .net Change text of selected textbox on button click

I have 10 buttons and I want to click on a button so it changes text of the focused textbox and switch to next textbox.
i tried this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Focus()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Focus Then
TextBox1.Text = "1"
TextBox1.Enabled = False
TextBox2.Focus()
TextBox2.Enabled = True
End If
If TextBox2.Focus Then
TextBox2.Text = "1"
TextBox2.Enabled = False
TextBox3.Focus()
TextBox3.Enabled = True
End If
If TextBox3.Focus Then
TextBox3.Text = "1"
TextBox3.Enebled= False
TextBox4.Focus()
TextBox4.Enabled = True
End If
End Sub
But it writes the value in every textbox instead of just going to the next textboxt
This code does not do what you think it does:
If TextBox1.Focus Then
Focus is not a boolean property. In VB.Net, you can call methods without parentheses, and that's what you're doing here. The conditional block actually tries to set the focus. And since this is always gonna succeed unless you explicitly handle the event and block it, all of those If conditions result in True.
To find which control has focus, do this:
Public Shared Function FindFocusedControl(control As Control) As Control
Dim container = TryCast(control,IContainerControl)
While container IsNot Nothing
control = container.ActiveControl
container = TryCast(control, IContainerControl)
End While
Return control
End Function
In your Click event handler, you are calling the Focus method of each TextBox in turn and then populating them if it succeeds. It will succeed every time so you populate every TextBox.
I suspect that what you meant to do was test the Focused property rather than call the Focus method. That would make more sense because then it would only populate the TextBox that had focus. That is still flawed though, because the Button that you just clicked will have focus, so you won't actually populate any TextBox.
You have two main choices here. Firstly, you could use a custom Button control that will not take focus when it is clicked. That way, the TextBox that had focus when you clicked will still have focus. Alternatively, you could remember which control last had focus by assigning it to a field and using that. That's probably the way that I'd go.
Here's a quick (i.e. not rigorous) example of the second option:
Private lastActiveControl As Control
Private Sub TextBoxes_Leave(sender As Object, e As EventArgs) Handles TextBox4.Leave,
TextBox3.Leave,
TextBox2.Leave,
TextBox1.Leave
lastActiveControl = DirectCast(sender, Control)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim textBoxes = New Control() {TextBox1, TextBox2, TextBox3, TextBox4}
Dim lastTextBoxIndex = Array.IndexOf(textBoxes, lastActiveControl)
If lastTextBoxIndex <> -1 Then
Dim nextTextBoxIndex = (lastTextBoxIndex + 1) Mod textBoxes.Length
Dim nextTextBox = textBoxes(nextTextBoxIndex)
lastActiveControl.Text = "1"
lastActiveControl.Enabled = False
nextTextBox.Enabled = True
nextTextBox.Select()
End If
End Sub

Text scrolling up, but after closing the form. The text wont back on the start. vb.net

I was able to scroll the text up, put a stop on it when it reached a certain location on Y.
However, when I was trying to load the credit form again. It will not be back on the start.
Here's the code I'm working on.
Public Class creditsform
Private Sub creditsform_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
Label1.Location = New Point(Label1.Location.X, Label1.Location.Y = 304) ' put back the label where it is.
End Sub
Private Sub creditsform_Load(sender As Object, e As EventArgs) Handles Me.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Location = New Point(Label1.Location.X, Label1.Location.Y - 5) ' scroll up the label up
If (Label1.Location.Y = -1506) Then ' closes the form if he reach the end of credit roll
Timer1.Enabled = False ' disable the timer
Me.Close() ' then close the form
End If
End Sub
End Class
EDIT: HERE IS THE FIRST LOAD AND THE SECOND LOAD OF THE FORM.
Also after the 2nd load, it doesn't execute the if else statement to close automatically the form.
1ST LOAD & 2ND LOAD
Here's where creditsform called.
Public Class EndGame
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim Red, Green, Blue As Integer
Dim RandomNumGenerator As New Random
Red = RandomNumGenerator.Next(0, 255)
Green = RandomNumGenerator.Next(0, 255)
Blue = RandomNumGenerator.Next(0, 255)
Me.Label4.ForeColor = Color.FromArgb(Red, Green, Blue)
Me.Label5.ForeColor = Color.FromArgb(Red, Green, Blue)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click '' this button closes the form and shows the creditsform
Me.Close()
creditsform.ShowDialog()
End Sub
End class
If I understood the problem correctly since it's not very clear what you are trying to do.
This is normal your using a class but not creating new instance of it so your variables don't reset. you should try this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click '' this button closes the form and shows the creditsform
Me.Close()
Dim MyCredits As New creditsform
MyCredits.ShowDialog()
End Sub

VB.NET Create a to a form attached Preview window

I want to create a little window which gets visible when I press a button and is attached to the main Form (like shwon below). I want to use this window to show preview of Images (I gonna have a Listbox and depending on which entry is selected, a picture is shown) How can I do this? And how can I be sure that the window will always be attached to the main Form (not depending on resolution). I tried to do it with a second form, but I can't fix it in the correct position.
regards
Assuming your preview form class is frmPreview and you open it this way:
Private mPreviewForm As frmPreview
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
mPreviewForm = New frmPreview
mPreviewForm.Show()
AttachPreviewForm()
End Sub
Then you must reposition it every time the main form is changing size or location:
Private Sub AttachPreviewForm()
If mPreviewForm IsNot Nothing Then
mPreviewForm.AttachForm(Me)
End If
End Sub
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles Me.SizeChanged
AttachPreviewForm()
End Sub
Private Sub Form1_LocationChanged(sender As Object, e As EventArgs) Handles Me.LocationChanged
AttachPreviewForm()
End Sub
And in the frmPreview:
Public Sub AttachForm(parent As Form)
Location = New Point(parent.Left + parent.Width, parent.Top)
Size = New Size(200, parent.Height)
End Sub

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!