Form Show not Called After Second Show - vb.net

So I am using Visual Basic to create a windows form application. I have buttons on the side to select a table like this. When the 'bookings' button is pressed, the user it taken to a form that looks like this. I need the following code to run every time the form is shown (the form is hidden shown multiple times). I put the below code inside both the form load and the form shown actions and both of them will only run the code on the first time. The code isn't really relevant, I just need to know how I can make this run every time the form is shown, not just once. The only other way I have thought about which works is to make the code run every second which is just unnecessary and a waste of processing power. Also, if I wanted another action to run after this, the action that occurs every second will take priority.
Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If GlobalVariables.GlobalFlag = "1" Then
Table1Sleep.Visible = False
Table1Selected.Visible = True
Table1Selected.BringToFront()
table1text.BringToFront()
table1textdesc.BringToFront()
ElseIf GlobalVariables.GlobalFlag = "2" Then
Table2Sleep.Visible = False
Table2Selected.Visible = True
Table2Selected.BringToFront()
table2textdesc.BringToFront()
table2text.BringToFront()
ElseIf GlobalVariables.GlobalFlag = "3" Then
Table3Sleep.Visible = False
Table3Selected.Visible = True
Table3Selected.BringToFront()
table3text.BringToFront()
table3textdesc.BringToFront()
ElseIf GlobalVariables.GlobalFlag = "4" Then
Table4Selected.Visible = True
Table4Selected.BringToFront()
tabel4text.BringToFront()
table4textdesc.BringToFront()
table4sleep.Visible = False
ElseIf GlobalVariables.GlobalFlag = "5" Then
Table5Selected.Visible = True
table5sleep.Visible = False
Table5Selected.BringToFront()
table5text.BringToFront()
table5textdesc.BringToFront()
ElseIf GlobalVariables.GlobalFlag = "6" Then
table6sleep.Visible = False
Table6Selected.Visible = True
Table6Selected.BringToFront()
table6text.BringToFront()
table6textdesc.BringToFront()
Else
MessageBox.Show("Something went wrong. When you press 'OK', you will be taken back to the Main Menu.")
MainMenuForm.Show()
Me.Close()
End If
End Sub
Thanks :)

Related

How to mark a radio button as checked with no action performed?

I have a form with CheckBox and RadioButton controls. When people click on the buttons/boxes it saves the results to my DB. Later, I might want someone else to retrieve the form and make changes or continue using it.
When I pull the info back from the database into a new "search" form (that looks exactly like the form used to submit the data), I want the radio buttons to reflect the state they were in when the form was saved. So if a box was TRUE (checked) when saved, I want it to show TRUE (checked) on the new form.
HOWEVER, when I use
radiobutton.checked = True
It DOES mark my button as checked.... it ALSO acts like the button was clicked again. So I have duplicate results in my database. It will keep doing this every time the form is opened.
So, I want a way to mark the box as checked (the state it was in when saved) but NOT CAUSE THE EVENT AGAIN.
Here's an example:
str = "SELECT * from OpenEvents WHERE EventID = " & eventId & ""
Dim cmd6 As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd6.ExecuteReader()
While dr.Read()
contactType = dr("ContactType").ToString
abend = dr("Abend").ToString
paged = dr("Paged").ToString
shortSummary = dr("ShortSummary").ToString
eventNotes = dr("EventNotes").ToString
impacting = dr("Impacting").ToString
L1Engaged = dr("L1Engaged").ToString
L2Engaged = dr("L2Engaged").ToString
managerEngaged = dr("ManagerEngaged")
IncTicket = dr("IncTicket").ToString
End While
myConnection.Close()
If contactType = "Call" Then
CallRadioButton.Checked = True
ElseIf contactType = "IM" Then
IMRadioButton.Checked = True
ElseIf contactType = "Other" Then
OtherRadioButton.Checked = True
ElseIf contactType = "Alert" Then
AlertRadioButton.Checked = True
Else
End If
If abend = "True" Then AbendCheckBox.Checked = True
If paged = "True" Then PagedYes.Checked = True
ShortSummaryTextBox.Text = shortSummary
DetailsTextBox.Text = eventNotes
If impacting = "True" Then ImpactingYesRadioButton.Checked = True
If L1Engaged = "True" Then L1YesRadioButton.Checked = True
If L2Engaged = "True" Then L2YesRadioButton.Checked = True
If managerEngaged = "True" Then ManagerYesRadioButton.Checked = True
IncTicketTextBox.Text = IncTicket
Just to turn it into an answer:
Create a boolean to be used at the form load event. Start it's value as true by default and when the load finishes, set it's value to false.
At every radio/check event you check the value, if it's true just exit the sub, otherwise do whatever it's supposed to do.
Best regards.
You could use Click event instead of CheckedChanged event.
Doing so, if you want to raise the event linked to the RadioButton you can use PerformClick instead of Checked = True.
If you want to change checked status without raising the event you can use Checked = True.
Here a little example:
Private Sub RadioButton1_Click(sender As Object, e As EventArgs) Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click
Select Case sender.name
Case Is = "RadioButton1"
Me.Label1.BackgroundColor = Color.Yellow
Me.Label2.BackgroundColor = Color.Gray
Me.Label3.BackgroundColor = Color.Gray
Case Is = "RadioButton2"
Me.Label1.BackgroundColor = Color.Gray
Me.Label2.BackgroundColor = Color.Yellow
Me.Label3.BackgroundColor = Color.Gray
Case Is = "RadioButton3"
Me.Label1.BackgroundColor = Color.Gray
Me.Label2.BackgroundColor = Color.Gray
Me.Label3.BackgroundColor = Color.Yellow
End Select
End Sub
Private Sub Btn_ChangeSelected_Click(sender As Object, e As EventArgs) Handles Btn_ChangeSelected.Click
Me.RadioButton1.Checked = True
End Sub
Private Sub Btn_PerformClick_Click(sender As Object, e As EventArgs) Handles Btn_PerformClick.Click
Me.RadioButton1.PerformClick()
End Sub
If you click Btn_ChangeSelected RadioButton1 will be checked but no event will be raised.
If you click Btn_PerformClick RadioButton1 will be checked and RadioButton1.click event will be raised.
I would save the state of the radiobuttons at form closing event to my.settings and read all back on the form load event. This way you keep the last settings of your radiobuttons before exiting the program.
In my case, I ended up moving my code from
_CheckedChanged
event, that was the default when clicking on the control from design view, to a
_MouseClick
event. Solved all my problems. Seems it was a bit silly that was the default, but lessons learned. And all working as I need now.
Thanks for all the input!!!

How would I go back to a certain line Visual Basic 2013?

I am currently using visual basic to create a project and am stuck on a certain point...
I am trying to get back to a certain point once the selected operation was completed.
I would like to repeat the Button1_Click sub if that's possible.
Heres my code so far.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Text = ("Transfer")
TextBox1.Text = ("This program only allows the use of one application at a time in order to prevent corrupt files etc.")
CheckBox1.Show()
CheckBox2.Show()
CheckBox3.Show()
CheckBox4.Show()
If CheckBox1.Checked Then
CheckBox2.Enabled = False
CheckBox3.Enabled = False
CheckBox4.Enabled = False
TextBox2.Show()
TextBox2.Text = ("Completed without any faults : If error occurs resort to readme in download file.")
Button1.Text = ("Quit")
End If
If CheckBox2.Checked Then
CheckBox1.Enabled = False
CheckBox3.Enabled = False
CheckBox4.Enabled = False
TextBox2.Show()
TextBox2.Text = ("Completed without any faults : If error occurs resort to readme in download file.")
Button1.Text = ("Quit")
End If
If CheckBox3.Checked Then
CheckBox1.Enabled = False
CheckBox2.Enabled = False
CheckBox4.Enabled = False
TextBox2.Show()
TextBox2.Text = ("Completed without any faults : If error occurs resort to readme in download file.")
Button1.Text = ("Quit")
End If
If CheckBox4.Checked Then
CheckBox2.Enabled = False
CheckBox3.Enabled = False
CheckBox1.Enabled = False
TextBox2.Show()
TextBox2.Text = ("Completed without any faults : If error occurs resort to readme in download file.")
Button1.Text = ("Quit")
End If
End Sub
Don't.
You've already implemented way too much functionality in that click handler, stop right there!
Extract Methods
You need to refactor a little bit here - extract a method for this part:
TextBox2.Show()
TextBox2.Text = ("Completed without any faults : If error occurs resort to readme in download file.")
Button1.Text = ("Quit")
And then replace all copies of that snippet with a method call.
Then extract another method for each chunk you have there - and name them.
Consider renaming your controls/buttons as well - and thank yourself later :)
When you're done, your click handler should read like a high-level summary of what's happening, instead of like a monolithic script written in 1997 - because that's what your execution flow feels like right now.
Write a method for every single thing you want your code to do, and then call these methods. Simple!
If your code does what it's supposed to be doing (i.e. works as intended), and just looks messy, confusing and inefficient, consider asking on Code Review next time!

How to enable/disable buttons in excel at once based on criteria?

I have an excel file with full command buttons, I want to disable some buttons with one click.
I'll appreciate your help.
Thanks
With the details you provided in your question, I would say that what you look for is:
By default, all the buttons are saved with ButtonX.Enabled = False (some of them might default with the Enabled = True property if they are meant to be available to all the users);
When the user inputs username and password in the form, you might want to run a code like this:
If userNameBox.Text = "Ahmad" And passwordBox.Text = "1234" Then
Button1.Enabled = True
Button3.Enabled = True
Button5.Enabled = True
'and so on with the buttons you want to activate
End If
Of course, this should be the code of the event-macro related to the submission of the Username/Password user form, let's say:
Private Sub StartUserFormButton_Click()
'code here
End Sub

MS-Access hidding and showing a button conditionally through vba

I am developing a quality control system for my company, and I i want to connect it to a label printer, which is already taken care off, the problem now is with the button itself.
I want the print label button to only be enabled and visible after the whole check was made, what i've got now is this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Button_label.Visible = False
Button_label.Enabled = False
End Sub
Private Sub Motor_OK_Change()
Dim ok As Boolean
ok = Motor_OK.Value
If ok = 1 Then
Button_label.Visible = True
Button_label.Enabled = True
End If
End Sub
It does work on conceiling the button, but it fails when trying to enable it again and making it visible upon making the check. It's important to refer that I have tried using If ok = True instead of If ok= 1, I dont know how important that is.
Thanks
You are not testing for both conditions (ok=0), perhaps the following will work:
Replace:
If ok = 1 Then
Button_label.Visible = True
Button_label.Enabled = True
End If
With:
Button_label.Visible = ok
Button_label.Enabled = ok
New Solution:
Instead of using the Form Before_Update event, use Form_Current event. If there is only 1 record, you can also use the Form_Load event. I would also suggest that you remove the Visible property and use only the Enabled one so users can see their is print functionality.
Private Sub Form_Current()
'button_Label.Visible = False
button_Label.Enabled = False
End Sub
Instead of using the Motor_OK Before_Updateevent, use After_Update event
Private Sub Motor_OK_AfterUpdate()
Dim ok As Boolean ' defaults to False
If IsNumeric(Motor_OK.Value) Then ok = True ' remove this statement if Motor_OM is
' alpha numeric.
If Not IsNull(Motor_OK.Value) Then ok = True ' otherwise following statement
' to avoid NULL error
'button_Label.Visible = ok
button_Label.Enabled = ok
End Sub
Let me know if I can be of further help.

Button color change does not work on other computers

I have a strange problem that I can't seem to figure out. I have a timer that runs every 3 seconds and changes a button color to yellow, black, or green depending on if there are schedules pending, no schedules, or schedule currently running, respectively.
It works fine on my computer when I go into debug mode and add a schedule or have one run, it changes color like it should. I put this program onto a virtual machine and made the same schedule with the same data parameters, but the button doesn't change colors. I have .net 4.0 installed on both machines.
Public Sub createTimer()
buttonTimer = New Timer()
buttonTimer.Start()
buttonTimer.Interval = 3000
AddHandler buttonTimer.Tick, AddressOf buttonTimer_Tick
createTimer() is called from a runonce function when the page is loaded.
Public Sub buttonTimer_Tick(sender As Object, e As EventArgs)
If Scheduler.AutomationRunning = True Then
btnAutoStartMenu.ForeColor = Color.Green
ElseIf Scheduler.AutomationRunning = False And Automation.ScheduleList.Count > 0 Then
btnAutoStartMenu.ForeColor = Color.Yellow
ElseIf Scheduler.AutomationRunning = False And Automation.ScheduleList.Count = 0 Then
btnAutoStartMenu.ForeColor = Color.Black
End If
I am wondering could it possibly be a dll I'm not including in the installer? But color change seems like something easily built into .net framework, so having 4.0 on both should take care of that correct?
EDIT:
Also, tried it on another virtual machine instance and the same problem remains.
#RoadBump is correct. What happens if Scheduler is Nothing?
You should get some more clue by adding some error handling:
Try
If Scheduler.AutomationRunning = True Then
btnAutoStartMenu.ForeColor = Color.Green
ElseIf Scheduler.AutomationRunning = False And Automation.ScheduleList.Count > 0 Then
btnAutoStartMenu.ForeColor = Color.Yellow
ElseIf Scheduler.AutomationRunning = False And Automation.ScheduleList.Count = 0 Then
btnAutoStartMenu.ForeColor = Color.Black
End If
Catch ex as Exception
btnAutoStartMenu.ForeColor = Color.Red
End Try
So if your button goes red then you need to start some further investigation