Program ignoring code - vb.net

Private Sub Play_Click(sender As Object, e As EventArgs) Handles Play.Click
WordGeneration()
userInput()
Once the user has clicked a button which has been implemented into my form it will then run each sub specified . However the problem is , i do not want the sub WordGeneration() being run more than once . Is there a way for me to program into my code to stop WordGeneration() from being run more than once so it doesnt keep randomly generating a word , as i only want it to generate one word.

Create a Boolean variable which you can set to True once you have called the code:
Public Class Form1
Private _codeCalled As Boolean = False
Private Sub Play_Click(sender As Object, e As EventArgs) Handles Play.Click
If Not _codeCalled Then
WordGeneration()
End If
userInput()
End Sub
End Class
So in your WordGeneration method set the variable _codeCalled = True.
This should then only call the method once. Anytime you want to call the method again look at setting _codeCalled = False. You may also want to give it a different name which better fits your structure.

Related

Why does a form move trigger ResizeEnd?

I use the following code in my form:
Public Class Form1
Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles MyBase.ResizeEnd
MsgBox("Resized")
End Sub
End Class
When I move my form, it also seems to trigger MyBase.ResizeEnd. Why is that? A move of the panel doesn't change the size, so I don't understand why.
Why does a form move trigger ResizeEnd?
Because this is the documented behavior. From the documentation:
The ResizeEnd event is also generated after the user moves a form, typically by clicking and dragging on the caption bar.
If you want an event that doesn't get triggered when the form is moved, you should use either Resize or SizeChanged. The problem with those two events is that they will be triggered while the form is being resized by the user. To work around that, you may use it with both ResizeBegin and ResizeEnd with a couple of flags to signal when the user actually finishes resizing the form.
Here's a complete example:
Private _resizeBegin As Boolean
Private _sizeChanged As Boolean
Private Sub Form1_ResizeBegin(sender As Object, e As EventArgs) Handles MyBase.ResizeBegin
_resizeBegin = True
End Sub
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
' This is to avoid registering this as a resize event if it was triggered
' by another action (e.g., when the form is first initialized).
If Not _resizeBegin Then Exit Sub
_sizeChanged = True
End Sub
Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles MyBase.ResizeEnd
_resizeBegin = False
If _sizeChanged Then
_sizeChanged = False
MessageBox.Show("The form has been resized.")
End If
End Sub
One thing to note is that both ResizeBegin and ResizeEnd are only triggered when the user manually resizes* the form. It does not, however, handle other situations like when the form is resized via code, when the form is maximized, or restored.
* or moves the form, which is the part that we're trying to avoid here.

How to update a form based on a checkbox status

I'm currently trying to code some macros for a software (Revit), and I ran into an issue I do not know how to solve.
So I have a windows form with two checkboxes and a list of elements, and I would like that list of elements to be updated depending on what the status of the checkboxes is.
Here's my checkbox status' code:
If StructcheckBox.Checked = True Then
Select Case tmpView.ViewType
Case ViewType.EngineeringPlan
vpList.Add(tmpVP)
End Select
End If
If LegcheckBox.Checked = True Then
Select Case tmpView.ViewType
Case ViewType.Legend
vpList.Add(tmpVP)
End Select
End If
Now the problem with that code is that it only checks the initial status of the checkboxes, but do not update the list when the checkboxes are checked/unchecked.
How to make it so that the list VpList is updated everytime a checkbox status is changed?
Thanks!
The key here is to add a sub that does the checkboxes checking. You will need that sub because it will called multiple times depending on the user's actions.
Because you are not providing any insights about tmpView and vpList I will stick with your code, however you should note that depending on what exactly are you trying to do your code can be simplified or rewritten to be a bit more efficient. For example you don't specify if you want the tmpVP value to be unique in the list or to be more than one times, I assume that you want to be unique, so here is the sub's code (please read the comments in the code):
Private Sub CheckBoxesStatus(StructcheckBoxChecked As Boolean, LegcheckBoxChecked As Boolean)
If StructcheckBoxChecked Then
If tmpView.ViewType = ViewType.EngineeringPlan Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'StructcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
If LegcheckBoxChecked Then
If tmpView.ViewType = ViewType.Legend Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'LegcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
End Sub
Now that you have the sub you can call it whenever you want. "Whenever you want" means on various user's actions, like checking or un-checking a checkbox and in various other places like form's initialization (loading) these are events. According to your question you need to call it from 3 different events.
1.When the form is loading in order to get the initial status:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
2.When the user changes the StructcheckBox status:
Private Sub StructcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles StructcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
3.When the user changes the LegcheckBox status:
Private Sub LegcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles LegcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
Here is the full form's code:
Public Class Form1
Private Sub CheckBoxesStatus(StructcheckBoxChecked As Boolean, LegcheckBoxChecked As Boolean)
If StructcheckBoxChecked Then
If tmpView.ViewType = ViewType.EngineeringPlan Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'StructcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
If LegcheckBoxChecked Then
If tmpView.ViewType = ViewType.Legend Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'LegcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
End Sub
Private Sub StructcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles StructcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
Private Sub LegcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles LegcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
End Class
Hope this helps.

BC30455 Argument not specified for parameter 'sender' of

i need help with my code
Public Class Forma
Private Sub ProjectToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ProjectToolStripMenuItem.Click
If ProjectToolStripMenuItem_Click() Then
Print(Form_load)
End If
End Sub
Private Sub Form_load(FormB As Object)
End Sub
End Class
but i keep getting BC30455 Argument not specified for parameter 'sender' of. What am i doing wrong
Your condition want to check the result of the event ProjectToolStripMenuItem_Click (click event of ProjectToolStripMenuItem). On this function you don't define the sender and e parameter.
You should review your code and fix the issues:
Public Class Forma
Private Sub ProjectToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ProjectToolStripMenuItem.Click
If exampleFunction() Then '<-- choose the correct function or variable
Print(Form_load) '<-- Form_load?? maybe FormB??
End If
End Sub
Private Sub Form_load(FormB As Object)
End Sub
End Class
Some basics you should know:
A Sub doesn't return a value, so you can't use a Sub on a condition (only functions, variables and values can be used on a condition).
It looks like you check if the button / menu item was clicked by the user. You don't need to check because the event ProjectToolStripMenuItem_Click is called automatically if the user click on ProjectToolStripMenuItem. At the point you try to check the button / menu item was clicked it was actually clicked. You can find more information about Event Handlers on the official .NET docs.
The error message should be clear enough to solve the issue:
You have not supplied an argument for a required parameter.
To correct this error:
Supply an argument for the specified parameter.
source: https://learn.microsoft.com/en-us/dotnet/visual-basic/misc/bc30455
You try to print the form on button click?
Public Class Forma
Private printForm as Object
Private Sub ProjectToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ProjectToolStripMenuItem.Click
If ConditionIsTrue() Then '<-- choose the correct Sub or Function
Me.printForm.Print()
End If
End Sub
Private Sub Form_load(FormB As Object)
'... maybe some other stuff
Me.printForm = FormB
'... maybe some other stuff
End Sub
End Class
You cannot pass a Sub as a Parameter Print(Form_load). FormB.Print() might be what you are looking for.
This looks like a problem : If ProjectToolStripMenuItem_Click() Then
It's a call to the same sub and it has no parameters specified.

One button to control another button on many user controls

Hey and sorry for another strange question...
I have 25 UserControls with a Start_Button on each of them - this Start_Button can either be Visible or not depending on whether the UserControl is active. On my form1 I have a Start_All button.
I would like to simulate a click of all the UserControl's Start_Buttons which are visible only.
Instead of simulating click-events expose a method for the start-functionality and call this method from the Start_Button.Click-event. Then you can use this method from wherever you want. On this way your code remains readable and reusable.
You should also provide an Active property in your UserControl which you can simply link to your start-button's Visible-property.
Presuming that the user-controls are in a container-control like Panel:
Public Sub StartAll()
Dim allActiveUserControls =
From uc In controlPanel.Controls.OfType(Of MyUserControlType)()
Where uc.Active
For Each uc In allActiveUserControls
uc.Start()
Next
End Sub
Here is an example for the Active property:
Public Property Active As Boolean
Get
Return StartButton.Visible
End Get
Set(value As Boolean)
StartButton.Visible = value
End Set
End Property
and here are the Start method and the event-handlers:
Public Sub Start()
' Do Something ... '
End Sub
Private Sub StartButton_Click(sender As System.Object, e As System.EventArgs) Handles StartButton.Click
Start()
End Sub
Private Sub Start_All_Click(sender As System.Object, e As System.EventArgs) Handles Start_All.Click
StartAll()
End Sub

Initialize not properly make with classes vb.net

So, I need to do an program for a client and he wants a search bar in it. So I made it and everything worked perfectly but I put it in my main form. Now, I want to put it in a class but when I initialize the program, it gives me the following error
An error occurred while creating the form. For more information,
see Exception.InnerException. The error is: The form is self-reference during
construction from a default instance, which led to infinite recursion. In the
constructor of the form, refer to the form using 'Me'.
I tried to put Me.Rbtn_X... but it doesn't recognize it.
Initialization
' Main form
Public Sub New()
InitializeComponent()
Initialize_search()
End Sub
Initialize_search()
' Main form
' search is initialize like this :
' Dim search as New Research
Private Sub Initialize_search()
search.generate_autocomplete()
End Sub
generate_autocomplete()
' Research class
Sub generate_autocomplete()
' Main_form = Main form
Dim field = ""
' This is the place where the program fail
If Main_form.RbtnR_avancee_contact.Checked Then
field = "personneressource"
Else
field = "beneficiaire"
End if
' ....
End Sub
Is there something I didn't understand or It's not possible to do it that way?
Edit: added Form_shown event
Public Sub New()
InitializeComponent()
' Initialize_search()
End Sub
Private Sub Form_personne_Shown(sender As Object, e As EventArgs) Handles Me.Shown
MessageBox.Show("You are in the Form.Shown event.")
End Sub
The form is not created (fully) until New completes. By adding your Initialize_search to it, it eventually leads to the statement `Main_form.RbtnR_avancee_contact.Checked'. This is wrong on two counts:
1) the form doesnt exist yet, so you cant refer to it. (this is what the error meant with 'form is self-reference during construction')
2) the ref should be Me.RbtnR (which is what it meant by 'refer to the form using 'Me'')
Move your Initialize_search to the Form_shown event. Your code should look like this (including Lar's suggestion)
' Main form
Public Sub New()
' REQUIRED
InitializeComponent()
End Sub
If there is really something that needs to be setup for this, add it to the form_shown event:
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Shown
' NOTE: even .NET refers to ME not MainForm etc
InitializePanel
InitializeSeach
End Sub
Then:
Private Sub Initialize_search()
search.generate_autocomplete(Me.RbtnR_avancee_contact.Checked)
End Sub
Then:
Sub generate_autocomplete(AdvContact as Boolean)
Dim field AS STRING = ""
If AdvContact Then
field = "personneressource"
Else
field = "beneficiaire"
End if
' ....
End Sub
Your search class doesn't have a reference to the instance of the form's controls.
Try passing the value instead:
Sub generate_autocomplete(advancedChecked As Boolean)
Dim field As String = ""
If advancedChecked Then
field = "personneressource"
Else
field = "beneficiaire"
End if
End Sub
Then when you call it:
search.generate_autocomplete(Me.RbtnR_avancee_contact.Checked)
Even if did work like you want it to, according to your code, it would always result in field containing the same value (whichever was set in designer).
Instead, try putting this code inside RbtnR_avancee_contact.Checked event. Or even TextChanged for the autocomplete box (and initialize it for the first time user enters anything), it would examine the checked state and populate autocomplete items.
With this approach, if your user never uses the search box, you don't need to initialize it.