Why am I getting an 'import statement unnecessary' - vb.net

I am working in Visual Studio creating a Windows Form Application in Visual Studio. I am very new to the language, and am doing a very simple app. I currently have 3 different forms created on one project. I created a button that switches from the first form to the second form. I am not trying to create a button that switches from the second form to the third form, but cannot import the third form into the code of the second project without getting an unnecessary import error.
This is the code for the first form to the second form and this button works
Imports random__Fun_Page_2
Public Class frmMainPage
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Dim newForm = New frmChoiceOne()
newForm.Show()
Me.Visible = False
End Sub
This is the code from the second form to the third form which gives me the error 'unnecessary import error'
Imports random__Fun_Page_3
Public Class frmChoiceOne
Private Sub radioOneChoiceOne_CheckedChanged(sender As Object, e As EventArgs) Handles radioOneChoiceOne.CheckedChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim newForm2 = New frmChoiceTwo
newForm2.show()
Me.Visible = False
End Sub
End Class
So my question is why can I import the second form on the first project, but cannot import the third form on the second project. Sorry if this does not give clear detail, but if there is any other information I can provide please let me know

As noted, it may well be just a suggestion.
But then again, as a normal course of action one will VERY rare if at all need to use imports for the ability to reference say some other form? That "practice" of using imports for forms is really never required here. It not clear what example or why imports for form names are being used here?
So a general rule, imports are to allow with greater ease to reference or use some .net assembly or library code (by what we call a namespace). As an typical development approach, one does not need nor as a regular development approach do a imports of forms in applications.
So if we have say Form AA and we want to launch form B?
Your code can do this on the click event:
BB.Show()
Me.Hide()
Note that you can (and should) use .show() and .Hide() in palce of .visible.
Now in our form BB, to close the form and return? We could/would use this:
AA.Show()
Me.Close()
Notice how I CLOSED form BB. Note how I did NOT close form AA, since my whole application and startup form was form AA. (if I close form AA, then my whole application shuts down if that is the first "base" form you specify in the project to load on startup. So I did want to close form BB (not just hide it), and then return to form AA.
And in say form BB, there is nothing that prevents you from getting/grabbing controls or values from form AA (or say form CC or whatever).
So no need for "importing" the other
In form BB, you can get/grab/use controls from from AA like this:
Me.TextBox1.Text = AA.TextBox1.Text
So you have full use of any of the loaded forms as per above, and no imports of the forms is or should be required.
So, you would never "import say form AA or CC into that form BB. You have full use of any loaded form and its values etc. in code.
Perhaps you goal is some type of sub-form here? (we don't have that option in vb.net).
But the "general" idea and concept is to not use imports for use of other forms.
If you need some "general" code module for some subs to call (and code not really needed to be part of a form? Then under project, you can add a code module, and place the general subs and functions that are not part of say a form. But once again, no imports should be required to do this.
I will also point out in above, I did not create a "instance" of the form before I used it. Over time one can adopt creating a form instance, but in your general code you can just use the "one line" BB.Show() and the form will load for you - not really a need to "create" the form instance before you use (show) it.

Related

Put A textbox value in a Label from another form

I have Two Forms
In The First Form I Have A Textbox Called TB1 An A String Called S1
In The Second Form I Have A Label Called L1
I Declared them publicly in a module
Module M1
Public L1 As New Label
Public TB1 As new Textbox
Public S1 As new String
End Module
/../
'in the First form
S1=TextBox1.text
'in the second form
L1.text=S1
But I Get this error "System.NullReferenceException" on the first Label L1
Any ideas why i'm getting this error
Try something like this:
On Form1 in the event calling Form2, Add:
Form2.L1.Text = TextBox1.text
Form2.Show()
You can't put them in a module like that; nothing will see them.
In addition, there's no way to see how this code is flowing.
To do a simple exercise:
Draw first form - form1 with Textbox1 on it.
Make the first form startup form (it will be by default in new winforms project)
Draw 2nd form - form2 with Label1 on it.
Put a button on the first form
Double click the button
In the handler put this code:
Sub Button1_Click (etc...
dim Form2Instance as new Form2
Form2Instance.Label1.Caption = Textbox1.text
Form2Instance.ShowDialog
End Sub
You can put a textbox on form2 and read the results in form1 the same way.
You can tell if the user OK'd (not canceled) form2 like this:
if Form2Instance.ShowDialog() = DialogResults.OK then
' only do this code if they HIT OK
I suggest maybe hitting youtube or pluralsite and watching a 'winforms for beginners' video, if such a thing exists. There are a lot of traps and pitfalls that you will run into hacking away on your own, that will be explained to before hand if you learn the basics. This is especially true if you've only done web programming or are just learning programming.

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.

BringToFront isn't bringing form to the front

I'm trying to set up a button that does the following:
Checks to see if a form is open (and has lost focus). If so, it brings that form to the front.
If not, it opens a new instance of the form.
However, I've tried a few different methods and it will always either create a new form (if I use frm_About.visible as the check) or simply not do anything (with the following code).
Private Sub counter_aboutClick(sender As Object, e As EventArgs) Handles counter_About.Click
If Application.OpenForms().OfType(Of frm_About).Any Then
frm_About.BringToFront()
Else
Dim oAbout As frm_About
oAbout = New frm_About()
oAbout.Show()
oAbout = Nothing
End If
End Sub
I've heard that there's a bug with BringToFront in certain scenarios, am I hitting that bug?
VB.Net does a terrible thing and creates a default instance of a form (which can be referred to by its class name). This creates endless confusion and headaches - I suggest you read up on default instances (google can find a lot to read about, surely)
In this case, you have a class called frm_About as well as a default instance of that form which is also called frm_About. If you've created a new form of type frm_About then the following code
If Application.OpenForms().OfType(Of frm_About).Any Then
frm_About.BringToFront()
will search your open forms to look for a form of type frm_About and, if it finds one, will attempt to bring the default instance of frm_About to the front - note that the open form can be (an in your case is most likely) not the default instance but any instance created with New frm_About().
To find the actual instance of the form you would have to do something like :
For Each openForm In Application.OpenForms()
If TypeOf (openForm) Is frm_About Then _
CType(openForm, frm_About).BringToFront()
Next

Best practice for sharing variables across forms in VB.NET

I need to share variables across two forms in VB.NET. One of them is the main form and the other is a child form.
I have been searching, and I have found a few methods. I want to use the best method to do this. I have listed a few options below. Please comment on which one would be the best option:
Create a static/shared variable in one of the forms and access it in the other forms via:
Form1 frm = new Form1(); // Creating object of parent to access shared variable
frm.a = "abc"; // Passing value
Send an instance of the main form to the child form when creating the child form. The variables can then be accessed via a property function.
Create global variables in a module. This seems like the easiest option, but I doubt it is the best option.
I also read something about delegates and events, but I don't know how to implement this.
If there is another method I haven't mentioned, please share it with me.
There is no one answer to the question. All the methods you listed should 'work.' Which you should use depends why you want to share the variable. For example:
Say you have a form with a list of records, and the user double-clicks a record, so you want to open a new form to edit the record, and you want to pass the record ID. In this case I would add a constructor method to the second form:
Sub New(RecordID as String)
'Add code to load the record here
End Sub
Say some of the forms in your application may want to know the database path or something else global like that. For that, I would make the appropriate variable on the parent form into a Public variable (called a Field) and access it as MainForm.FieldName. (Disclaimer: Purists will say you shouldn't rely on the somewhat messy fact that VB.NET automatically instantiates an instance of the form class and lets you refer to it by the form name, and that you should instead get a pointer to the actual instance of the form and store it in your child form and access the parent form like that. Actually, this is like number '2' in your post. But it's not actually necessary if you don't mind programmatical incorrectness.)
Say there is something global in your app, like the time the app was started, so you can tell the user "You've been using the app for 5 hours, go get a life!" These things could be stored in a module. (Or in the application class but that's quite hidden)
Create two forms. Add 3 radio buttons and 1 button to form1. Add a label to form2. In the code for form1 type
Public rdb As Integer = 1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
If RadioButton1.Checked Then
rdb = 1
ElseIf RadioButton2.Checked Then
rdb = 2
ElseIf RadioButton3.Checked Then
rdb = 3
End If
End Sub
Then in form2's code
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = Form1.rdb
End Sub
Store your global variables in a Module1.vb file, they must be publicly declared to be accessible from all forms:
Public X as String
Public Y as Integer
Then just use them as you would any other variable on any page:
X = "Hello"
Y = 10
Textbox1.Text = X
Textbox2.Text = Y
It's not the safest practice so it shouldn't be used for all your variables. But it is very neat and simple.
The child form can have public get and set functions for a private variable, and the parent form can set it when it changes and its end, or get it before it uses it to see if it has changed in the child form.
You can add public properties to either form. They can access those properties from each other. (That is not called shared though, and is not static in most cases.)
Not sure if this answers the question but one thing I found useful was to refer to the variables in form1 while programming in form2 as Form1.variablename and when in Form1 and referring to variables in Form2 to use in Form1 as Form2.variablename Basically, refer to variables in other forms by putting the name of the form they are in followed by a . and then the variable name