Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to open form from another, while passing variables from the first?

Status
Not open for further replies.

belcom125

Programmer
Nov 17, 2004
45
CA
I have a little authentication window come up as soon as you start the program but then when a user enters their username and password (correctly; it is checked against database) I need it to set a few variables (variables must be public and kept until application closes) and pass them to the next form that I want to open (what's the method, property or function to do that). Please help me out.
 
If the variables are declared within a module as Public then they will be available to the next form that opens anyway.

Why would you need to pass them in to it?
 
You could create a sub (in this case SetUser) in the new form and call it after you instantiate it

Dim objMainForm as new myForm()
objMainForm.SetUser(txtUsername.Text, txtPassword.Text)
objMainForm.show()

Or, as Neil said, create public properties in a module.
 
Or if you really need to pass the variables in simply overload the Sub New in the form you are creating to accept the necessary variables.
 
ok. thanks. I'll try. I need it because once the user(sales rep) enters their username and password I can retrieve info from database based on that particular rep's Id number. Like the customers, sales orders and everything else related to that particular person. I am new to VB.NET and I haven't done much at all in VB6 either (I am a network engineer), but now my work requires me to create and maitain databases, database driven websites, VB apps, crystal reporting and so on. And that's why I need some help.
 
I have a problem...

when I do this after successfuly authenticating.

Dim MainFrm = New Main
MainFrm.Show()

it just blinks for a milisecond and that's it.
I just want to open the main application form and work with it. Why does it not stay ?
 
I am supposing that you named the Login form as the StartUp object. As soon as that form, is Closed, the application will be ended. See Application.Run

From MSDN:
[Visual Basic]
Overloads Public Shared Sub Run( _
ByVal mainForm As Form _
)

Remarks
Typically, the main function of an application calls this method and passes to it the main window of the application.

This method adds an event handler to the mainForm parameter for the Closed event. The event handler calls ExitThread to clean up the application.

Note The Dispose method of the Form class will be called prior to the return of this method.


Compare Code
 
the quick answer, use showdialog.

the not so quick answer, I would recommend reorganizing your code a little.

Use the logon screen as a dialog (see this FAQ: )

In the constructor of your primary form, have it call the logon form's dialog function. Have the logon form return true or false depending on wether or not they were validated. stick that in a 3 try loop w/ a message box for failed logons. if they fail or quit, set the primary form to nothing and let the program end.

-Rick

----------------------

[monkey]I believe in killer coding ninja monkeys.[monkey]
 
showdialog() works great, well... for now at least :eek:)
 
I'm going to throw some code together (because I'm working on testing stuff and it's god awefull boring)

in the logon form:
Code:
private variable m_Validated as object 

public function Logon as object
  me.showdialog
  return m_Validated
end function

private sub ProcessCancel
  'set the return value to nothing, 
  'so we know the user hit cancel
  m_Validated = nothing
  me.close
end sub

private sub ProcessOK
  'code to determine if the login was valid
  if valid
    m_Validated = True
  else
    m_Validated = false
  end if
  me.close
end sub

Then in your primary form:
Code:
public sub New()
  dim frmLogon as new clsLogonDialog 
  dim iAttempts as integer
  dim Validated as object

  for iAttempts = 1 to 3
    validated = frmLogon.Logon
    if validated is nothing orelse _
       validated = true then exit for
    messagebox.show "Invalid logon, try again"
  next

  if validated = false then
    messagebox.show "Maximum logon attempts reached. Please restart and try again."
  end if
  if validated <> true then 
    end
  end if
end sub

-Rick

----------------------

[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top