How to make the second event start after the first one - vb.net

How to make the second event start after the first one.
Below is a code that copies files from 2 different paths set by the user for me. On the other hand, it wants to make the first copy from another path before it goes to the next one.
Public Sub StartCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartCopy.Click
' Reset variables
_stopped = False
' Create the FileCopy class which will initiate the threads
CopyFiles = New FileCopy
CopyFiles.FromPath = My.Settings.pathmemfrom1
CopyFiles.ToPath = My.Settings.pathmemto1
' Initiate the copy, count from the FileCopy class
CopyFiles.StartCopy()
WorkingBar.Minimum = 0
WorkingBar.Maximum = 100
WorkingLabel.Visible = True
WorkingBar.Visible = True
' Reset form controls
setting.Panel.Enabled = False
StopCopy.Enabled = True
ViewLog.Enabled = True
CopyFiles.Dispose()
'Reset veriables
_stopped = False
' Create the FileCopy class which will initiate the threads
CopyFiles2 = New FileCopy
CopyFiles2.FromPath = My.Settings.pathmemfrom2
CopyFiles2.ToPath = My.Settings.pathmemto2
CopyFiles2.StartCopy()
WorkingBar.Minimum = 0
WorkingBar.Maximum = 100
WorkingLabel.Visible = True
WorkingBar.Visible = True
' Reset form controls
setting.Panel.Enabled = False
StopCopy.Enabled = True
ViewLog.Enabled = True
CopyFiles2.Dispose()
'Reset veriables
_stopped = False

Related

Is there an easier/more efficient way to make Visible = False for multiple buttons in VB.NET?

not sure if there is an easier way to do this but I have a LOT of buttons on a form. Different ones are visible for different functions.
Is there a way to have something like this have an easier way to change their visibility without having each button coded to go False/True?
For simplicity sake, I created a quick app to handle the visibility but I want to hide the others when one set of buttons is visible. So if I select Row 1, it will make Visibility FALSE on Row 2 and 3.
Am I stuck with this or is there an easier way/more efficient way? THANKS IN ADVANCE!
Public Class Form1
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedItem.ToString = "Button Row 1" Then
Button1.Visible = True
Button2.Visible = True
Button3.Visible = True
Button4.Visible = True
Button5.Visible = True
Button6.Visible = True
Button7.Visible = True
Button8.Visible = True
Button9.Visible = True
Button10.Visible = True
Button11.Visible = False
Button12.Visible = False
Button13.Visible = False
Button14.Visible = False
Button15.Visible = False
Button16.Visible = False
Button17.Visible = False
Button18.Visible = False
Button19.Visible = False
Button20.Visible = False
Button21.Visible = False
Button22.Visible = False
Button23.Visible = False
Button24.Visible = False
Button25.Visible = False
Button26.Visible = False
Button27.Visible = False
Button28.Visible = False
Button29.Visible = False
Button30.Visible = False
ElseIf ComboBox1.SelectedItem.ToString = "Button Row 2" Then
Button1.Visible = False
Button2.Visible = False
Button3.Visible = False
Button4.Visible = False
Button5.Visible = False
Button6.Visible = False
Button7.Visible = False
Button8.Visible = False
Button9.Visible = False
Button10.Visible = False
Button11.Visible = True
Button12.Visible = True
Button13.Visible = True
Button14.Visible = True
Button15.Visible = True
Button16.Visible = True
Button17.Visible = True
Button18.Visible = True
Button19.Visible = True
Button20.Visible = True
Button21.Visible = False
Button22.Visible = False
Button23.Visible = False
Button24.Visible = False
Button25.Visible = False
Button26.Visible = False
Button27.Visible = False
Button28.Visible = False
Button29.Visible = False
Button30.Visible = False
ElseIf ComboBox1.SelectedItem.ToString = "Button Row 3" Then
Button1.Visible = False
Button2.Visible = False
Button3.Visible = False
Button4.Visible = False
Button5.Visible = False
Button6.Visible = False
Button7.Visible = False
Button8.Visible = False
Button9.Visible = False
Button10.Visible = False
Button11.Visible = False
Button12.Visible = False
Button13.Visible = False
Button14.Visible = False
Button15.Visible = False
Button16.Visible = False
Button17.Visible = False
Button18.Visible = False
Button19.Visible = False
Button20.Visible = False
Button21.Visible = True
Button22.Visible = True
Button23.Visible = True
Button24.Visible = True
Button25.Visible = True
Button26.Visible = True
Button27.Visible = True
Button28.Visible = True
Button29.Visible = True
Button30.Visible = True
End If
End Sub
End Class
Set the tag property of the buttons. You can do it in the properties window. I just show it in code to illustrate the point
Button1.Tag = "Button Row 1"
Then you can do
Dim selectedRow = ComboBox1.SelectedItem.ToString()
For Each c As Control In Controls
If (TypeOf c Is Button) Then
c.Visible = selectedRow.Equals(c.Tag)
End If
Next
Note that this automatically shows the buttons of the selected row and hides the others.
If this affects too many buttons, you can also check if the Tag is not Nothing instead of testing if it is a button.
You could use a for loop and keep track of which textbox is in which row, could put the row in the name, tag, use a custom property, etc.
You could put each row in a groupbox and just change the visibility of the group.
You could make a list of Buttons for each row, add the buttons, and loop through those lists.
Winforms support data binding.
' In form constructor
public Sub New()
InitializeComponent()
cmbEnableButtons.DataSource = New List(Of string) From
{
"Nothing",
"Button Row 1",
"Button row 2"
}
button1.Tag = "Button Row 1"
button2.Tag = "Button Row 1"
button3.Tag = "Button Row 2"
button4.Tag = "Button Row 2"
button1.DataBindings.Add(CreateBindingForVisible())
button2.DataBindings.Add(CreateBindingForVisible())
button3.DataBindings.Add(CreateBindingForVisible())
button4.DataBindings.Add(CreateBindingForVisible())
}
Private Function CreateBindingForVisible() As Binding
Dim buttonBinding =
New Binding("Visible",
cmbEnableButtons,
"SelectedValue",
true,
DataSourceUpdateMode.OnPropertyChanged)
' Every time selected value of combobox changed
' this event handler convert string to "visible" boolean
AddHandler buttonBinding.Format, AddressOf ButtonBinding_Format
return buttonBinding;
End Sub
Private Sub ButtonBinding_Format(object sender, ConvertEventArgs e)
Dim binding = DirectCast(sender, Binding)
Dim button = DirectCast(binding.Control, Button)
e.Value = Equals(button.Tag, e.Value)
End Sub
With data binding you can configure every button separately from each other, while having common logic in one place.

DblClick call in VBA - close on double click without running function

Good Afternoon,
Bear with me, I am new to VBA. I have a Sub that opens a text box when its double clicked, not only do I want it to open the text box (expands it), i also want it to run a sub called EMSpull. This all works perfectly except for the fact I want to then double click to close the textbox without running EMSpull again.
Also, can someone explain "Control", especially for this situation (i didnt write the adjustheight )
Code is below
Private Sub txtEMS_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Call AdjustHeight(txtEMS, lblEMS)
Call EMSpull
End Sub
Public Sub AdjustHeight(cControl As Control, cLabel As Control)
On Error GoTo errhand
If bExpandedMode = False Then
dOldTop = cControl.Top
dOldLeft = cControl.Left
dOldWidth = cControl.Width
dOldHeight = cControl.Height
cControl.Top = lblDescription.Top + 50
cControl.Width = cControl.Width * 2
cControl.Height = 500
cControl.Left = lblResults.Left
bExpandedMode = True
Call HideAllTxt(cControl)
lblDescription.Visible = True
lblDescription.Caption = cLabel.Caption
If Len(cControl.Text) > 2 Then
cControl.CurLine = 0
End If
Else
bExpandedMode = False
Call ShowAllTxt
lblDescription.Visible = False
cControl.Top = dOldTop
cControl.Left = dOldLeft
cControl.Width = dOldWidth
cControl.Height = dOldHeight
End If
Exit Sub
errhand:
Resume Next
End Sub
I'm assuming you have a global boolean named bExpandedMode at the top of the subs? If so this should work fine:
Private Sub txtEMS_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Call AdjustHeight(txtEMS, lblEMS)
if bExpandedMode = true then Call EMSpull 'Calls it only when it expanded in AdjustHeight
End Sub
Public Sub AdjustHeight(cControl As Control, cLabel As Control)
On Error GoTo errhand
If bExpandedMode = False Then
dOldTop = cControl.Top
dOldLeft = cControl.Left
dOldWidth = cControl.Width
dOldHeight = cControl.Height
cControl.Top = lblDescription.Top + 50
cControl.Width = cControl.Width * 2
cControl.Height = 500
cControl.Left = lblResults.Left
bExpandedMode = True
Call HideAllTxt(cControl)
lblDescription.Visible = True
lblDescription.Caption = cLabel.Caption
If Len(cControl.Text) > 2 Then
cControl.CurLine = 0
End If
Else
bExpandedMode = False
Call ShowAllTxt
lblDescription.Visible = False
cControl.Top = dOldTop
cControl.Left = dOldLeft
cControl.Width = dOldWidth
cControl.Height = dOldHeight
End If
Exit Sub
errhand:
Resume Next
End Sub
Basically if that boolean exists and is used like I think it is, it just check whether the box is expanded right now or not. When it's not, the boolean is False, and AdjustHeight expands it, then turns it to true. Conversely when it is set to True, it closes it instead, and sets it to False.
So my fix just checks that same boolean and only runs it 1 way (when it just expanded)

IsInRole - VB Form Load

I've built an application out of Visual Basic with a login screen and a form. The login screen authenticates with Active Directory. After user authentication, the form loads. On form load, I would like to check to see if the authenticated user is in one of four particular Active Directory security groups. Depending on which group the authenticated user is in will depend on which buttons on the form are enabled. I've got the active directory user authentication to work for logging into the program and loading the form, but the specific code used to verifying which group the user is in does not work. Below is my code for form load.
Private Sub form_main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
button_main_pimam.Enabled = False
button_main_pimpm.Enabled = False
button_main_eim.Enabled = False
button_main_achmanager.Enabled = False
button_main_mobiliti.Enabled = False
button_main_checkfree.Enabled = False
button_main_rcm.Enabled = False
button_main_mis.Enabled = False
button_main_colson.Enabled = False
If My.User.IsInRole("domain.local\Fiserv Processing - Electronic Banking") Then
button_main_achmanager.Enabled = True
button_main_pimam.Enabled = True
button_main_pimpm.Enabled = True
button_main_eim.Enabled = True
button_main_colson.Enabled = True
button_main_colson.Enabled = True
ElseIf My.User.IsInRole("domain.local\Fiserv Processing - Operations") Then
button_main_achmanager.Enabled = True
button_main_mobiliti.Enabled = True
button_main_checkfree.Enabled = True
button_main_rcm.Enabled = True
button_main_colson.Enabled = True
ElseIf My.User.IsInRole("domain.local\Fiserv Processing - Loan Operations") Then
button_main_pimam.Enabled = True
button_main_pimpm.Enabled = True
button_main_eim.Enabled = True
button_main_achmanager.Enabled = True
button_main_mobiliti.Enabled = True
button_main_checkfree.Enabled = True
button_main_rcm.Enabled = True
button_main_mis.Enabled = True
ElseIf My.User.IsInRole("domain.local\Fiserv Processing - MIS") Then
button_main_pimam.Enabled = True
button_main_pimpm.Enabled = True
button_main_eim.Enabled = True
button_main_achmanager.Enabled = True
button_main_mobiliti.Enabled = True
button_main_checkfree.Enabled = True
button_main_rcm.Enabled = True
button_main_mis.Enabled = True
button_main_colson.Enabled = True
End If
End Sub
Regardless of which group the authenticated user is in, all the buttons are enabled for use. What am I doing wrong?
Try this approach. In your case, i would cache the array of groups that user belongs to when user authenticates, and then check whenever you need in your app.
Function IsInGroup(UserName As String, groupName As String) As Boolean
Dim vUsuario As New NTAccount(UserName)
Dim sid As SecurityIdentifier = vUsuario.Translate(GetType(SecurityIdentifier))
Using vRootDSE As New DirectoryEntry("LDAP://rootDSE")
Using vSearcher As New DirectorySearcher(New DirectoryEntry("LDAP://" + CStr(vRootDSE.Properties("defaultNamingContext")(0))), "(objectSID=" & sid.ToString() & ")", New String() {"memberOf"}, SearchScope.Subtree)
Dim src As SearchResultCollection = vSearcher.FindAll()
Dim memberOf As ResultPropertyValueCollection = src(0).Properties("memberOf")
For i As Integer = 0 To memberOf.Count - 1
'Debug.Print(memberOf(i).ToString())
' I don't really like this approach, but it's quick to write ;)
If memberOf(i).ToString().Contains("=" & groupName & ",") Then
Return True
End If
Next
End Using
End Using
Return False
End Function

flash will not start automatically playing video and video advancement fails?

I am having trouble getting my current vb.net code to start automatically playing random video trailers retrieved from trailer addicts API service (I only return 6 of them at a time). In addition, I can play the first video but other videos fail to allow me to let me play them manually too with an error about protected memory being accessed. Here's the code:
Private Sub TrailerRotateTimer_Tick(sender As Object, e As EventArgs) Handles TrailerRotateTimer.Tick
If FirstTrailer = False Then
Select Case myoptions.TrailerPlayTime
Case Is = 60
TrailerRotateTimer.Interval = 60000
FirstTrailer = True
Case Is = 90
TrailerRotateTimer.Interval = 90000
FirstTrailer = True
Case Is = 120
TrailerRotateTimer.Interval = 120000
FirstTrailer = True
Case Else
TrailerRotateTimer.Interval = 60000
FirstTrailer = True
End Select
End If
firstvideo = myoptions.TrailerUrlCollection.Item(randnum.Next(1, 5))
' Video.EmbedMovie = True
Video.AllowScriptAccess = "always"
Video.FlashVars = ""
' Video.Playing = True
Video.LoadMovie(0, firstvideo)
If Video.IsPlaying = False Then
' Video.CallFunction("<invoke name=""id"" returntype=""xml""><arguments><string>100788</string></arguments></invoke>")
Video.Play()
'Video.Playing = True
'Video.Loop = False
Else
Video.Stop()
Video.Play()
End If
End Sub
Does anyone know what I am doing wrong?
P.S. here's the direct link I found to the SWF file if needed: http://v.traileraddict.com/js/flash/player.swf?id=100788&e=y&id=100788

Infinite loop when validating input against array

The homework task is to simply allow the user to input a value (string) "FD__" and match it against a list of known inputs and return true/false. The ID's are already defined and it works well, when I type an ID which is defined like Products(2) which is "FD3" it will return true, if the value is not defined it will not only give no results but crash the program, so in conclusion true works but false does not. Any information could be helpful.
Design: http://i.imgur.com/bJnFAMX.png
Public Class Form1
'variables
Dim Products(9) As String
Dim Entered As String
Dim Found As Boolean
Public Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
'Product variable array elements
Products(0) = "FD1"
Products(1) = "FD2"
Products(2) = "FD3"
Products(3) = "FD4"
Products(4) = "FD5"
Products(5) = "FD6"
Products(6) = "FD7"
Products(7) = "FD8"
Products(8) = "FD9"
Products(9) = "FD10"
'process
Entered = txtFind.Text 'define entered value as variable
Found = FindNumber() 'sub function
If Found Then
lblResult.Text = Found 'change the results
End If
End Sub
Public Function FindNumber()
'If true statements
Do While (Found = False)
If Entered = Products(0) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(1) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(2) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(3) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(4) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(5) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(6) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(7) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(8) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
ElseIf Entered = Products(9) Then
Found = True
lblResult.ForeColor = Color.LawnGreen
Else 'keeps crashing
Found = False
lblResult.ForeColor = Color.Red
End If
Loop
Return Found
End Function
End Class
You can actually do this with a lot less code... I'm going to go ahead and do some critique while at it.
'1. Functions should have return types
'2. You should pass the function what it needs to do its job
Public Function FindNumber(numberEntered As String) As Boolean
'Rather than evaluating if a condition is true or false, and then returning
'true or false, you can just directly return the condition.
Return Products.Contains(numberEntered)
End Function
Then in your main program:
'This doesn't need to be a class level variable
Dim entered = txtFind.Text
'Local variables should be lowercase
Dim found = FindNumber(entered)
If found Then
'With Option Strict you can't assign a Boolean to a String...
'but you can do .ToString() instead
lblResult.Text = found.ToString()
'And this was really dependent on the result of the function...
'Also it has nothing to do with finding the number...
lblResult.ForeColor = Color.LawnGreen
Else
lblResult.ForeColor = Color.Red
End If
It looks a lot longer, but take out the comments and I promise you it'll be a much simpler (and shorter) program.
You could optimise this whole code by using the .contains function this simple used like this:
if mylist.contains("sometext") then
'change forecolor to green
else
'change forecolor to red
end if
This would mitigate your problem.