VB.NET reference sender form in a separate module - vb.net

I am trying to loop through each panel and control in each user form I have in my application so I wrote a subroutine to call upon initialization of each user form. The problem I have encountered is referencing the sender user form in a separate module. The code I have:
Sub Configure_UI()
For Each Control_Panel As Panel In sender.Controls.OfType(Of Panel) 'Loop through panels
For Each control In Control_Panel.Controls
Configure_Control(control)
Next
Next
End Sub
The error that it gives says "Sender is not declared. It may be inaccessible due to its protection level.". SO I wonder how do I fix this. What I need is a dynamic solution where sender is the form.name.
Could someone, please, help me out please?

so the solution is as follows
Sub Configure_UI(ByVal sender As Form)
For Each Control_Panel As Panel In sender.Controls.OfType(Of Panel) 'Loop through panels
For Each control In Control_Panel.Controls
Configure_Control(control)
Next
Next
End Sub
and the call to it from user form initialization is:
Configure_UI(me)
Any suggestions on optimizing the solution?

Related

VBA to Open MS Access Form Full Screen

I have problem with VBA Access database I want to open form full screen.
Let me know which property and code I use to do fix the issue.
I tried many script and codes.
Private Sub Form_Load()
Application.Forms = Xlmaximized
End Sub
For a form to maximize it is:
Private Sub Form_Load()
DoCmd.Maximize
End Sub
The application you should never force to be maximize. That would be to violate the user's control over the machine.
If for some reason you would like to violiate the user's control over the machine and maximize the application, you could do this too:
Private Sub Form_Load()
DoCmd.Maximize
Application.RunCommand acCmdAppMaximize
End Sub

Accessing Form controls from a module

I am trying to create a sub in a module that simply hides a lot of panels for a Form which is passed as a parameter to it. Basically, Ill have a button called students and a button called subjects and I want that when I click on the, lets says, students button ALL the panels I define in the function get hidden, and only the students panel remains visible.
When I run this code I get :
System.MissingMemberException: 'Public member 'pnlAlumnos' on type 'Ventana_Principal' not found.'
Code in my module
Module Modulo_Comportamiento_Ventanas
Public Sub Esconder_todos_los_paneles(ventana As Object)
ventana.pnlAlumnos.Hide()
ventana.pnlMaterias.Hide()
End Sub
End Module
Code on the main Form click
Private Sub btnAlumnos_Click(sender As Object, e As EventArgs) Handles btnAlumnos.Click
Esconder_todos_los_paneles(Me)
Me.pnlAlumnos.Show()
End Sub
How can I modify my code in order to being able access the panels and hiding them?
Thanks in advance!
Note : BTW, I know I can do this, but I want to know why it does not work the way I am doing it, I mean the below code looks horrible ( not that my other path looks any more nicer) :
Public Sub Esconder_todos_los_paneles()
WindowsApp1.Ventana_Principal.pnlAlumnos.Hide()
WindowsApp1.Ventana_Principal.pnlMaterias.Hide()
End Sub

Catch a value from form1 to form2 in vb [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
In VB, I create 2 forms in 1 project. In form1, I put 3 buttons with different value. In form2, I only put 1 textbox with no value.
My question is, how if I press one of the button in form1, the form2 is automatically opened and the value from the button that I press automatically added to the form2 textbox?
Add following code into your button handler. You can double click button and add the code into event handler which is automatically created:
'Here we are creating actual object and passing string into it constructor method
Dim instanceOfForm2 = new Form2("String value from Form1!")
instanceOfForm2.Show() ' Showing form
In Form2 we need to tweak our constructor to accept one parameter:
Public Sub New(someValue as String)
InitializeComponents() 'This is always first row in form constructor
TextBox1.Text = someValue 'And put that text into textbox...
End Sub
In VB6 you can do all the stuff that lardymonkey has in place, but you don't have to. The most concise way to do what you want is this. First, make your three command buttons in Form1 into a control array. To do this, give them all the same name (I'll use "cmdMyButtons" in my example), and set their index properties to 0, 1, and 2. Then do this in Form1's code window:
Option Explicit
Dim ButtonText(2) As String
Public Sub Form_Load()
ButtonText(0) = "First Button Text"
ButtonText(1) = "Second Button Text"
ButtonText(2) = "Third Button Text"
End Sub
Public Sub cmdMyButtons_Click(Index As Integer)
With Form2
.txtMyTextBox.Text = ButtonText(Index)
.Show vbModal
End With
End Sub
Now, I like lardymonkey's idea of showing modally, so I put it in here as well. However, several things in his code sample aren't intrinsically necessary to do what you want, and create overhead, so I pulled them out:
You don't need to make a property; you can just set the text
directly as I have here.
You don't need to create a form variable; you can just reference the form directly as I have here.
You don't have to load the form explicitly; the form gets
automatically loaded as soon as you set the variable (by the way,
the Show method also automatically loads the form, too--you only use
Load when you want to have the form loaded into memory before you do
anything to it).
If you close the modal form it will be
automatically unloaded. However, unloading a form doesn't set any
object variables referencing it to nothing. Therefore, frmDetail
will not be Nothing when you check it, you will unload a form that
isn't loaded. While this doesn't throw an error (the statement is ignored), I wouldn't do it anyway. So, you don't
need any of the "make sure the form is destroyed" code.
And now, for a short lecture on the whole business of always explicitly destroying object variables:
There is a longstanding argument about whether you need to explicitly set all your local object variables to Nothing before exiting a subroutine in VB6. I don't agree with this at all; VB takes care of this automatically when the variables go out of scope. As far as I can see, the reason that people believe that they have to do this is that the scope finalizer doesn't collect garbage in any particular order, and sometimes two interacting COM objects need to be destroyed in a particular order due to poor coupling architecture. In such a case, you do indeed need to clear the objects in the correct order to work around intermittent bugs, so the myth developed that VB's garbage collection is buggy and needs to be circumvented by always manually destroying object variables.
Frankly, the idea that a programmer is going to always do this and never forget is naive. So I persist in disagreeing with it; the developers of VB6 put a lot more thought and effort into developing the scope finalizer than any programmer is going to put into circumventing it.
Without knowing the specific version of the software you are using we cant answer, we can answer it if you provide the correct version
In .net it's a simple as creating the form then passing the value over.
Friend objForm2 as New Form2
Private Sub button1_Click(ByVal sender As System.Object, ByVal e as System.EventArgs) Handles button1.Click
objForm2 = new Form2()
TextBox1.Text = value
End Sub
This would be a way of doing it in VB6. I've left the error handling up to you.
I've made the assumption that the name of the text box is txtOutput on form2.
In Form2 add the following:
Option Explicit
Public Property Let OutputText(ByVal strOut As String)
txtOutput.Text = strOut
End Property
In Form1 add the following:
Option Explicit
Private Sub Command1_Click()
DisplayForm "1"
End Sub
Private Sub Command2_Click()
DisplayForm "2"
End Sub
Private Sub Command3_Click()
DisplayForm "3"
End Sub
Private Sub DisplayForm(ByVal strValue As String)
'Declare the variables
Dim frmDetail As Form2
'Create the form
Set frmDetail = New Form2
'Load the form and display it modal
Load frmDetail
frmDetail.OutputText = strValue
frmDetail.Show vbModal, Me
'Make sure the form is destoryed
If Not frmDetail Is Nothing Then
Unload frmDetail
Set frmDetail = Nothing
End If
End Sub
Make sure you comment the code and if you need some error handling then add it. If you need help with the VB6 functions you can find it here MSDN VB6 Reference

VBA Listbox becomes unresponsive after first use

I have a VBA (Excel 2010) system which involves selecting an item from a listbox and then displaying it in another form. Here is a very simplified version of what happens.
' Part of frmForm1 code module
sub lstListbox_Click
dim MyEvent as string
dim i as integer
i=me.lstListbox.listindex
MyEvent=me.lstlistbox.list(i)
' Now show the item in the second form
Load frmForm2
me.hide
ThisWorkbook.LoadDataIntoForm2 (frmForm2, MyEvent)
frmForm2.show
unload frmForm2
me.show
end sub
The listbox accepts the click, and first the event (the event handler is giver above). Key parts of the event handler are:
Load the second form (to display the detail data)
Pass the second form as a UserForm parameter to a procedure (LoadDataIntoForm2)
Hide the host form (frmForm1) and show the second form (frmForm2)
When the second form processes an Exit click, the code looks like this:
' Part of frmForm2 code module
sub cmdExit_Click
me.hide
end sub
The first time round it works fine - but when I return to frmForm1 (in the tail end of the lstListBox_Click procedure), even though the rest of the form is operative, the listbox remains stubbornly unresponsive.
I've managed to abstract this down to a little demo system if that would help - the same behavior is seen there. (It's regular .xls file, but that seems not to be easily acceptable as an upload)
Has anyone seen this before? And does anyone have any ideas how I might get this to work the way I want it to?
Thanks,
Tony
The default for the .Show method is to make the form modal. Explicitly set it to modeless:
Sub lstListbox_Click
...
Me.Show vbModeless
End Sub

Visual Basic - how to force event to fire

I'm new to VB (using VBA, actually) and I would like to force an event to fire. Specifically, I am updating the value in a textbox and I would like to call that textbox's AfterUpdate() event.
Private Sub cmdCreateInvoice_Click()
Me.txtInvDate = '11/01/10'
Me.txtInvDate.AfterUpdate
End Sub
During run time, I get an error that says "Compile Error: Invalid Use of Property". If I try something similar, but with the click event (ex: cmdCreateInvoice.Click), which does NOT have a property that shares the name, I get an error that says "Compile Error: Method or Data member not found".
I know there must be a way to fire one event from another. But what is the proper syntax?
Thanks!
AFAIK, there is no way to "fire an event manually" in VB(A). What you can do is call the event handler manually, and for this, rdkleine has given you the answer already:
Call txtInvDate_AfterUpdate()
This will have exactly the same effect as if the event had fired (though it does not give you the whole chain of events that may also fire along with it--unless you Call their handlers as well).
IgorM has another valid point, in comments on his answer--it's "cleaner" to write a different Sub to do the work you want done, then call it from both the event handler & wherever you're trying to do it now (button click?). So:
Private Sub txtInvDate_AfterUpdate()
DoWhatever
End Sub
Private Sub button_Click()
DoWhatever
End Sub
Private Sub DoWhatever
'your desired effect
End Sub
You could even make DoWhatever a Public Sub in a Module.
Edit
And no, in VB(A) it doesn't matter what order you define your Sub (or Function) routines.
Call
txtInvDate_AfterUpdate()
Try to use something like this:
Private Sub TextBox1_LostFocus()
TextBox1.Value = "5"
End Sub
Use LostFocus event if you change value manually.
Or you can use another way:
Private Sub CommandButton1_Click()
TextBox1.Value = "new value"
End Sub
Private Sub TextBox1_Change()
MsgBox TextBox1.Value
End Sub
Try to use Change event.
Some code apparently will (regretfully) force events to fire:
Me.Dirty = False
forces BeforeUpdate and AfterUpdate to fire, as I understand it. There are probably more cases too. Any secret hooks which attach to your code in that manner reflect bad design in my view and will inevitably lead to inadvertant & frustrating loops being created.
Is there a way to keep a textboxes event firing until I click on another text box? I'm needing to stay "in" the text box which has Do While loop for a selection. Once the selection is made, I want the user to be able to replace the selection without having to click on the text box again. If the user is satisfied with the select, they can click on a different text box.