Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Linking and transfer data from different forms (Visual C# 2005)
 

Linking and transfer data from different forms (Visual C# 2005)

i am creating this 2 forms

form A - login

form B - personal info list

firstly user gotta key in its username and password for verification.

i used switch-case to do the comparing of the password and to verify different password range stands for different user.after verification is successful,form B will appear to request user to key in information.

the tricky part is different user will be able to access certain parts of the form B.

how am i suppose to let form B know that form A current user log in is 1 or 2 or 3

so that it is possible to enable different controls?

Form A

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace PasswordTest

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private int logonAttemptCount = 0;

int intAccessCode;

Main frm = new Main();

private void okButton_Click(object sender, EventArgs e)

{

intAccessCode = Int32.Parse(passwordTextBox.Text);

if (usernameTextBox.Text == "level1")

{

switch (intAccessCode)

{

case 0:

case 1:

case 2:

case 3:

case 4:

case 5:

case 6:

case 7:

case 8:

case 9:

statusLabel.Text = "Restricted Access";

MessageBox.Show("Sorry,Please Try Again.Invalid");

logonAttemptCount++;

if (logonAttemptCount == 3)

{

MessageBox.Show("Too many logon attempts. Exiting Application.");

this.Close();

}

break;

case 0101:

case 1111:

statusLabel.Text = "Scientists";

frm.Show();

break;

case 2222:

case 3333:

statusLabel.Text = "Custodians";

frm.Show();

break;

default:

statusLabel.Text = "Access Denied";

MessageBox.Show("Sorry,Please Try Again.Invalid");

usernameTextBox.Text = "";

passwordTextBox.Text = "";

logonAttemptCount++;

if (logonAttemptCount == 3)

{

MessageBox.Show("Too many logon attempts. Exiting Application.");

this.Close();

}

break;

}

}

else

{

if (usernameTextBox.Text == "level2")

{

switch (intAccessCode)

{

case 6666:

case 7777:

statusLabel.Text = "Accountant";

MessageBox.Show("Welcome.Accountant");

usernameTextBox.Text = "";

passwordTextBox.Text = "";

frm.Show();

break;

case 8888:

case 9999:

statusLabel.Text = "Managers";

MessageBox.Show("Welcome.Manager");

usernameTextBox.Text = "";

passwordTextBox.Text = "";

frm.Show();

break;

}

}

else

{

usernameTextBox.Text = "";

passwordTextBox.Text = "";

statusLabel.Text = "Access Invaild";

MessageBox.Show("Sorry,Please Try Again.Invalid");

logonAttemptCount++;

if (logonAttemptCount == 3)

{

MessageBox.Show("Too many logon attempts. Exiting Application.");

this.Close();

}

}

}

}

private void cancelButton_Click(object sender, EventArgs e)

{

this.Close();

}

}

}

Form B

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace PasswordTest

{

public partial class Main : Form

{

public Main()

{

InitializeComponent();

}

private void Main_Load(object sender, EventArgs e)

{

//if (statuslabel.Text == Scientist)

// {

radioButton1.Enabled = false;

radioButton1.Checked = true;

radioButton2.Enabled = false;

radioButton3.Enabled = false;

radioButton6.Enabled = false;

// }

}

private void radioButton4_CheckedChanged(object sender, EventArgs e)

{

if (radioButton4.Checked)

{

BackColorChanged = 33FF33;

}

}

}

}

intoxicated-  Wednesday, October 03, 2007 9:59 AM

You can create a property in the Form B, something like this

public partial class Main : Form
{
public Main()
{
InitializeComponent();
}

private void Main_Load(object sender, EventArgs e)
{
switch (this.username)
{
case ...
......
}
}

private string username;

public string UserName
{
get { return this.username; }
set { this.username = value; }
}
}

before showing the form Main form, set UserName for it..

In Form A:

Main frm = new Main();
.....
frm.UserName = this.textBox1.Text;
frm.Show();

Zhi-Xin Ye  Tuesday, October 09, 2007 8:44 AM

You can create a property in the Form B, something like this

public partial class Main : Form
{
public Main()
{
InitializeComponent();
}

private void Main_Load(object sender, EventArgs e)
{
switch (this.username)
{
case ...
......
}
}

private string username;

public string UserName
{
get { return this.username; }
set { this.username = value; }
}
}

before showing the form Main form, set UserName for it..

In Form A:

Main frm = new Main();
.....
frm.UserName = this.textBox1.Text;
frm.Show();

Zhi-Xin Ye  Tuesday, October 09, 2007 8:44 AM

hi sorry but to check i tried applying the code stated but while building it right the terms i declare under the switch case is not available

is there any error in my switch case?

private void Main_Load(object sender, EventArgs e)

{

switch (this.username)

{

case Scientists:

radioButton1.Enabled = false;

radioButton1.Checked = true;

radioButton2.Enabled = false;

radioButton3.Enabled = false;

radioButton6.Enabled = false;

break;

case Custodians:

radioButton1.Enabled = false;

radioButton1.Checked = false;

radioButton2.Enabled = true;

radioButton3.Enabled = false;

radioButton6.Enabled = false;

break;

case Accountant:

radioButton1.Enabled = true;

radioButton1.Checked = false;

radioButton2.Enabled = false;

radioButton3.Enabled = false;

radioButton6.Enabled = false;

break;

case Managers:

radioButton1.Enabled = false;

radioButton1.Checked = g;

radioButton2.Enabled = false;

radioButton3.Enabled = false;

radioButton6.Enabled = false;

break;

}

intoxicated-  Tuesday, October 09, 2007 9:56 AM

Set a breakpoint at switch (this.username) to see what the username is, does it matches any of your usernames?

Zhi-Xin Ye  Tuesday, October 09, 2007 9:59 AM

the program cant run as they state that "Scientist" doesnt exist in the context for the for B and this eror applies to the rest like managers and all.

its either they are able to locate or did i lack out of any declaring?

Form A

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Globalization;

using System.Threading;

namespace PasswordTest

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private int logonAttemptCount = 0;

int intAccessCode;

Main frm = new Main();

private void okButton_Click(object sender, EventArgs e)

{

//retrieve the access code ,convert the string of no to 32-bit signed integer

intAccessCode = Int32.Parse(passwordTextBox.Text);

if (usernameTextBox.Text == "level1")

{

switch (intAccessCode)//check access code input

{

case 0:

case 1:

case 2:

case 3:

case 4:

case 5:

case 6:

case 7:

case 8:

case 9:

statusLabel.Text = "Restricted Access";

MessageBox.Show("Sorry,Please Try Again.Invalid");//Pop out message box

logonAttemptCount++;//counter +1

if (logonAttemptCount == 3)

{

MessageBox.Show("Too many logon attempts. Exiting Application.");

this.Close();

}

break;

case 0101:

case 1111:

statusLabel.Text = "Scientists";

frm.UserName = this.statusLabel.Text;

frm.Show();

break;

case 2222:

case 3333:

statusLabel.Text = "Custodians";

frm.UserName = this.statusLabel.Text;

frm.Show();

break;

default:

statusLabel.Text = "Access Denied";

MessageBox.Show("Sorry,Please Try Again.Invalid");

usernameTextBox.Text = "";

passwordTextBox.Text = "";

logonAttemptCount++;

if (logonAttemptCount == 3)

{

MessageBox.Show("Too many logon attempts. Exiting Application.");

this.Close();

}

break;

}

}

else

{

if (usernameTextBox.Text == "level2")

{

switch (intAccessCode)

{

case 6666:

case 7777:

statusLabel.Text = "Accountant";

MessageBox.Show("Welcome.Accountant");

frm.UserName = this.statusLabel.Text;

frm.Show();

break;

case 8888:

case 9999:

statusLabel.Text = "Managers";

MessageBox.Show("Welcome.Manager");

frm.UserName = this.statusLabel.Text;

frm.Show();

break;

}

}

else

{

usernameTextBox.Text = "";

passwordTextBox.Text = "";

statusLabel.Text = "Access Invaild";

MessageBox.Show("Sorry,Please Try Again.Invalid");

logonAttemptCount++;

if (logonAttemptCount == 3)

{

MessageBox.Show("Too many logon attempts. Exiting Application.");

this.Close();

}

}

}

}

private void cancelButton_Click(object sender, EventArgs e)

{

this.Close();

}

private void englishToolStripMenuItem_Click(object sender, EventArgs e)

{

Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-NZ");

Form1 newForm = new Form1();

newForm.Show();

}

private void chineseToolStripMenuItem_Click(object sender, EventArgs e)

{

Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-SG");

Form1 newForm =new Form1();

newForm.Show();

}

private void germanToolStripMenuItem_Click(object sender, EventArgs e)

{

Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-AT");

Form1 newForm = new Form1();

newForm.Show();

}

private void frenchToolStripMenuItem_Click(object sender, EventArgs e)

{

Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");

Form1 newForm = new Form1();

newForm.Show();

}

}

}

Form B(error is from this form)

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace PasswordTest

{

public partial class Main : Form

{

public Main()

{

InitializeComponent();

}

private void Main_Load(object sender, EventArgs e)

{

switch (this.username)

{

case Scientists:

radioButton1.Enabled = false;

radioButton1.Checked = true;

radioButton2.Enabled = false;

radioButton3.Enabled = false;

radioButton6.Enabled = false;

break;

case Custodians:

radioButton1.Enabled = false;

radioButton1.Checked = false;

radioButton2.Enabled = true;

radioButton3.Enabled = false;

radioButton6.Enabled = false;

break;

case Accountant:

radioButton1.Enabled = true;

radioButton1.Checked = false;

radioButton2.Enabled = false;

radioButton3.Enabled = false;

radioButton6.Enabled = false;

break;

case Managers:

radioButton1.Enabled = false;

radioButton1.Checked = false;

radioButton2.Enabled = false;

radioButton3.Enabled = false;

radioButton6.Enabled = true;

break;

}

}

private string username;

public string UserName

{

get

{

return this.username;

}

set

{

this.username = value;

}

}

}

}

### those highlighted in orange are actually theunderlined errors

thanks

intoxicated-  Wednesday, October 10, 2007 1:42 AM

obviously you do not define the Scientists,Custodians....

Zhi-Xin Ye  Wednesday, October 10, 2007 2:47 AM

meaning define it in form B?

#define scientist?

or string scientist

intoxicated-  Wednesday, October 10, 2007 3:10 AM

Hi

Your using Scientist as if it were a declared object e.g string Scientist = "Scientist" or Scientist MyScientist = new Scientist()

But seen as though your comparing a string, you just need to wrap it in quotes at the case statement.

e.g case "Scientists":

Cheers

Si

sdix  Wednesday, November 14, 2007 11:00 PM

thanks(: solved cheers

intoxicated-  Thursday, November 15, 2007 1:12 AM

You can use google to search for other answers

Custom Search

More Threads

• Outlook Calendar Look and Feel - Automatic refresh
• Remote server unreachable error message
• taskvision web edition
• e-mailing
• Terrarium 1.2 strange error
• datagridview column cel value multiply to textbox value
• mscoree.dll not found.
• Lots of Views, but no reply
• Need TaskVision Source file
• Can't add new animals ..