Accessing Form controls from a module - vb.net

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

Related

VB.NET reference sender form in a separate module

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?

Visual Basic Detect Mouse Position

So i want to make a maze game in Visual Basic and if the cursor reaches a certain panel, it will show a message box ONCE and then the Form closes.
The question is How?
I've tried
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
If Cursor.Position = Panel1 Then
MsgBox("Completed")
Application.Exit()
End If
End Sub
And didn't work. I got
Error 1 Overload resolution failed because no accessible '=' can be
called with these arguments:
'Public Shared Operator =(left As System.Drawing.Point, right As System.Drawing.Point) As Boolean': Value of type
'System.Windows.Forms.Panel' cannot be converted to
'System.Drawing.Point'. C:\Documents and Settings\Admin\Local
Settings\Application Data\Temporary
Projects\WindowsApplication1\Form1.vb 4 12 WindowsApplication1
Remember that i want the message box to only appear once, because sometimes when the cursor is on the panel, it shows multiple msgbox until the cursor is outta there.
I want the mouse inside the panel and run a code.
I believe there is an event called 'mouse enter' event you can use so if you type the code for the messagebox in that even for the control you want them to mouseover it will pop up everytime they do that.
For it to to nly pop up once make a counter that adds 1 and dont execute that code if the counter is already on 1.
I had a little search and found: Determine whether the mouse is over a control? (over a Control pixel range)
I just knocked up a test with a button and seemed to work fine. Please adapt to your own needs.
Private Sub Button1_Paint(sender As Object, e As PaintEventArgs) Handles Button1.Paint
Debug.WriteLine(MouseIsOverControl(Button1))
End Sub
Public Function MouseIsOverControl(ByVal c As Control) As Boolean
Return c.ClientRectangle.Contains(c.PointToClient(Control.MousePosition))
End Function
In this example I've just output "true or false" to determine detection. You can test and change it to your own needs to determine what you want to do depending on 'true/false'. Hope this helps.

Passing form with reference to itself

I have the following problem:
I have 2 windows forms ("Main" and "Form2") and separated class "PI".
Using panel, I display "Form2" inside "Main".
Code from "Main":
Dim frm As new Form2 = Form2()
panel1.Controls.Add(frm)
frm.Show()
That's working fine. After clicking on the button from "Form2" the program went to class PI to do some calculations, this works fine also, but when all calculations from class PI are finished, I need to pass results back to the "Form2" using the following code (test is just string public variable). Code from "PI":
Public Shared Sub Test
Form2.test = "It works!"
end sub
Code from "Form2":
Public test As String
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PI.Test()
MsgBox(test)
End Sub
So, the problem is that without panel, if I open Form2 independently (not inside "Main", passing variables in this way works good (msgbox shows the statement), otherwise - it doesn't (empy box). Could you please tell me what is wrong and how can I fix it.
Thanks in advance!
Why it works on standalone Form2:
In
Public Shared Sub Test()
Form2.test = "It works!"
End Sub
you are always addressing default instance of Form2. It is that one you simply call as standalone in case you are describing as "works as expected".
But in your code
Dim frm As new Form2 = Form2()
panel1.Controls.Add(frm)
frm.Show()
you are creating new (another) instance of your Form2.
Use the original default instance there (if you need only one Form2), i.e. do not use new, and it will start working.
panel1.Controls.Add(Form2)
Structuring your code like this couples PI and Form2 together - they know too much about each other. Generally we prefer classes like PI to be independent from the UI code that uses them - it makes them easier to work with, reuse and debug.
I don't know what the result of the Test method is (in your example it's just a string), but you should make it so that the Test method returns it, then Form2 can use it.
If the result is more complicated (maybe it's several values) you could create a class to contain the result values and return an instance of that class from Test. Or you could just set multiple attributes on the PI instance and change the Form2 code to get the values from those attributes after calling Test. (This isn't as nice as returning the value, but it might be simpler, and it's much better than having PI set results on Form2 directly.)

Passing References through forms vb.net

Is there anyone with very good knowledge in vb.net. I am working on my final semester project. I need to pass objects between forms. I have my codes of two forms here.
http://pastebin.com/xP1LdL3t
http://pastebin.com/fpuY98NT
To connect to the irc server i am using irc.Connect() function. It is perfectly working on my channel form and it is to be called only when users want to connect or on form load. When i double click the online user list a private message form opens. And i am unable to send irc.sendraw command and that form has not called irc.Connect(). It is not supposed to be called in every form. What i want is to use the channel's connection status on other forms so that irc.sendraw command will work.
If i have failed to explain properly please let me know.
It's not clear what you mean by "pass references". There are several ways to communicate between forms. Here are a few:
Declare public variables or properties in one form to be accessed by other forms. If you do this, be sure you are using the proper instance of the form containing the public variables. If you refer to one of these variables before that form is loaded, you'll end up with two instances of the form and things will get confusing.
Use a public method, similar to (1)
Declare global variables in a separate module, to be accessed by any form in the project. (Some people consider this bad manners.)
Pass parameters to and from the forms.
Raise an event to be handled in another form.
basically,
If you want to pass through function in Form 1 to function in Form 2 you can do somthing like this:
Public Class Form1
Dim x As Integer = 2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.fun(x)
Form2.Show() ' it's optional
End Sub
End Class
and in Form 2 you just get the value as ref:
Public Class Form2
Dim y As Integer 'the variable to get the value of x
Public Sub fun(ByRef x As Integer)
y = x
End Sub
End Class
Hope it can help you, and it's what you wanted.

Intellisense in private modules

Assume I have a module named Module1 with the following code:
Private Sub MyPrivateSub()
'do something
End Sub
Public Sub MyPublicSub()
'do something
End Sub
Public Sub test()
End Sub
If I place the cursor inside Test() and start typing "MyPrivateSub" or "MyPublicSub", I don't get any intellisense. I can type "Module1." (or "Me." if Module1 were a class module) to get an intellisense menu, but this only contains the public method MyPublicSub, as shown here:
Is there some way to get an intellisense menu for all members, public and private? I'm working on a project with modules that have many methods, and going up and down continually to copy/paste member names is time consuming.
Short answer
hit a combination of
CTRL+SPACE
and start typing the sub name like shown here
Long answer
Everything you do in programming matters. When you decide to make your sub/function/variable private you do that for a reason and you need to understand how this will affect the access level and "scope".
The scope of a member is dictated by its access level and in VBE anything that is private and qualified with it's parent member does not get intelli-sense. Simply, because you can't (are not supposed to be able to access it) from the outside.
Because your MyPrivateSub access level is Private you can't access it with intelli-sense through qualifying the module with Module1..
If you're starting from fresh, you can do the following:
for all your private sub function, name them into a standard format but as public function at the moment, for e.g.:
Public sub iamprivate_calDate ()
Public sub iamprivate_getsetfunction()
...
after you're completed with your macros, simply find and replace all your "Public sub iamprivate" into "Private sub " in your editor.