I have built application in multiple winforms. First form is a login form. When user logs in, it opens him another form, let say form 2, and then from there I have menu strip which takes user further to form 3,4,5 and so on.
What I want is to put a button on a upper right corner and call it LOG OUT.This button will appear on all forms(only not on first one of course), so when user logs in, do what he needs to do, and then to have ability to log out,regardless on what form he is on. When he logs out the first form will pop out again! How can this be done? Is it possible to close form 1 (log in form, parent) and not shut down the whole application (children forms) after log in?
Next thing I need is to put restrictions... What I mean by that is that there will be different type of users, regular ones and admins, which will have more options available. I have done the login part, checked if there is user name and password from database that match e.g. textbox1 and textbox2 but I need some advice to implement what I just described above.
Thanks, Bane
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con1 = getConnection();
con1.Open();
SqlCommand com1 = new SqlCommand();
com1.Connection = con1;
com1.CommandType = CommandType.Text;
com1.CommandText = "select * from radnik WHERE username = '" + textBox2.Text + "' AND password = '" + textBox3.Text + "'";
SqlDataReader reader = com1.ExecuteReader();
if (reader.Read())
{
MessageBox.Show("Uspesno ste se ulogovali!");
Form2 form2 = new Form2();
form2.Show();
}
else { MessageBox.Show("Doslo je do greske!"); }
}
catch (Exception ee)
{
MessageBox.Show(ee.StackTrace);
}
Refresh();
}
this checks info for log in
private void stoloviToolStripMenuItem1_Click(object sender, EventArgs e)
{
var form3 = new Form3();
form3.Show();
}
private void stoloviToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void sifrarnikToolStripMenuItem_Click(object sender, EventArgs e)
{
var form4 = new Form4();
form4.Show();
}
private void rezervacijeToolStripMenuItem_Click(object sender, EventArgs e)
{
var form6 = new Form6();
form6.Show();
}
private void porudzbineToolStripMenuItem_Click(object sender, EventArgs e)
{
var form7 = new Form7();
form7.Show();
}
private void magacinToolStripMenuItem_Click(object sender, EventArgs e)
{
var form9 = new Form9();
form9.Show();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 fr1 = new Form1();
fr1.Show();
}
this just lead user to different winforms
That's a typical MDI application. The Main-Form (the parent of the MDI Child) will have a button in the left upper corner which is activated when the user logs in. The login prompt's form will be a modal form. When the user clicks the logout button you should close all the children forms and then presents again the modal login form. Regarding the user's privileges is not difficult to do, just have a field in the database with that particular piece of information. It could be a byte an integer or whatever you like. Let's say 1=Normal user, 2=Admin user, etc.
I believe you would need 3 classes:
a login form, where the user will authenticate by inserting the credentials and clicking the loginButton
a main form, to hold the menu strip, where the other forms could be shown by pressing the menu buttons
the child form, which will have a logoutButton and other custom controls (so basically you can derive other child forms from it)
In the login form, handle the login button click like this:
private void loginButton_Click(object sender, EventArgs e)
{
if (Authenticated(username.Text, password.Text))
{
var mainForm = new MainForm();
this.Visible = false;
mainForm.Show(this);
}
}
The main form, add a handler for the form closing event and some handlers for showing the children:
public MainForm()
{
InitializeComponent();
FormClosing += MainForm_FormClosing;
}
void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Owner.Visible = true;
}
private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
{
var childForm = new ChildForm();
childForm.Show(this);
}
In the child form, when clicking on the logoutButton, get a reference to the owner of the child form (which will be the main form), close it's owned forms (all it's children) and close the main form itself:
private void logoutButton_Click(object sender, EventArgs e)
{
var owner = ((MainForm)this.Owner);
foreach (var childForm in owner.OwnedForms)
{
childForm.Close();
}
owner.Close();
}
Related
I have form 1 and form 2. In form 1, I have 4 button which button 1 and 2 is use to open form 2 by provide the form 2 with specific name which is form2A and form2B. Button 3 and 4 is use to close the opened form base on the form name. How I can close the form2A when click button 3 but not close the form2B?
public static List<Form> forms = new List<Form>();
Form Open
TagItem.TagItemInfo tagItemInfo = new TagItem.TagItemInfo(symbol_id);
tagItemInfo.Name = symbol_id.ToString();
TagList.forms.Add(tagItemInfo);
tagItemInfo.Show();
Form close
if(TagList.forms.Count > 0)
{
foreach(Form frm in TagList.forms)
{
if(frm.Name == o.SymbolID.ToString())
{
frm.Dispose();
frm.Close();
}
}
}
My system can open as many as form which will specify a name for it.
You have to define Form2A and Form2B as 2 different vars:
private Form2 _form2a, _form2b;
//open Form 2A with Windowtext "Form 2A"
private void Button1_Click(object sender, EventArgs e)
{
_form2a = new Form2 { Text="Form 2A" };
_form2a.Show();
}
//open Form 2B with Windowtext "Form 2B"
private void Button2_Click(object sender, EventArgs e)
{
_form2b = new Form2 { Text="Form 2B" };
_form2b.Show();
}
//close Form 2A
private void Button3_Click(object sender, EventArgs e)
{
_form2a.Close();
}
//close Form 2B
private void Button4_Click(object sender, EventArgs e)
{
_form2b.Close();
}
I have this situation I want to synchronize informations in my dataGridView when I insert it on my add form like you can see on this picture.
In my Insert form on insert button I call Add form to pop up like this
private void button1_Click(object sender, EventArgs e)
{
if (addForm==null)
{
addForm = new AddForm();
}
addForm.MdiParent = this.ParentForm;
addForm.FormClosed += AddForm_FormClosed;
addForm.Show();
}
private void AddForm_FormClosed(object sender, FormClosedEventArgs e)
{
addForm = null;
}
On Add form in Accept click I insert informations and call fillDataGrid() method from Insert form to do data sync but nothing is shown data is shown just when I close Insert form and call it again does someone has susggestion how can I do this this is the first time I work with MdiContainer ?
private void buttonAccept_Click(object sender, EventArgs e)
{
if (validation())
{
Proizvod product = new Proizvod();
product.NazivProizvoda = textBoxName.Text;
product.Opis = textBoxDescription.Text;
product.SerijskiBroj = textBoxNumber.Text;
product.ZemljaPorijekla = textBoxCountry.Text;
if (pDal.insertProduct(product)==0)
{
MessageBox.Show("Informations are successfully inserted","Message");
InsertForm inForm = new InsertForm();
inForm.fillDataGrid();
}
}
}
My fillDataGrid() method and Load event of InsertForm:
public void fillDataGrid()
{
dataGridViewProducts.DataSource = null;
dataGridViewProducts.AutoGenerateColumns = false;
dataGridViewProducts.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridViewProducts.ColumnCount = 3;
dataGridViewProducts.Columns[0].Name = "Product name";
dataGridViewProducts.Columns[0].DataPropertyName = "NazivProizvoda";
dataGridViewProducts.Columns[1].Name = "Country";
dataGridViewProducts.Columns[1].DataPropertyName = "ZemljaPorijekla";
dataGridViewProducts.Columns[2].Name = "Product number";
dataGridViewProducts.Columns[2].DataPropertyName = "SerijskiBroj";
dataGridViewProducts.DataSource = pDal.getAllProducts();
}
private void InsertForm_Load(object sender, EventArgs e)
{
fillDataGrid();
}
private void InsertForm_Shown(object sender, EventArgs e)
{
dataGridViewProducts.CurrentCell = null;
dataGridViewProducts.ClearSelection();
}
Currently in buttonAccept_Click code, you have created a new instance of the list form and called its FillGrid. This way you are manipulating another instance of the list form which is different from the instance which is open and you can see. You are filling a different form which you didn't show it.
Instead of creating a new instance, create a constructor for your second form which accepts a parameter of first form type. Then when you want to create a new instance of seccod form, pass the instance of the first form (this) to second Form. Then in your save button call the FillGrid method of the passed instance.
For more information about how to manipulate another form, read this post. It contains some useful options about:
Pass data to second Form when creating
Manipulate second Form after showing
Manipulate first Form from second Form
Here is some code which belong to the ListForm:
private void ShowAddForm_Click(object sender, EventArgs e)
{
if (addForm == null)
{
addForm = new AddForm(this);
addForm.MdiParent = this.ParentForm;
addForm.FormClosed += AddForm_FormClosed;
}
addForm.Show();
}
private void AddForm_FormClosed(object sender, FormClosedEventArgs e)
{
addForm = null;
}
And here is the code for AddForm
public class AddForm
{
MyListForm listForm;
public AddForm(MyListForm f)
{
InitializeComponent();
listForm = f;
}
private void SaveVutton_Click(object sender, EventArgs e)
{
//perform validation and save data
f.FillGrid();
}
}
I am using Visual studio 2012. I created two forms, form1 with a button to open form2 and the form2 has a button "exit" which will take me back to form1.
this is my code from form 1:
private void btnRecords_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
this.Hide();
}
and from form 2:
private void btnExit_Click(object sender, EventArgs e)
{
this.Hide();
}
I know I can use frm2.Show(); this.Hide(); instead of frm2.showdialog();. But, I need my form 1's state to be unaltered. My form1 contains a login form, which only enables the buttons (such as the new form button) if the login is correct. So if I hide form1 and just show it again, the login resets;
On Form2 class add a property to store the reference to the parent form:
public Form ParentForm { get; set; }
Then on Form1, you can show Form2 this way, hiding Form1 at the same time:
private void btnRecords_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ParentForm = this;
this.Hide();
frm2.ShowDialog();
}
When closing Form2, you can show Form1 again:
private void btnExit_Click(object sender, EventArgs e)
{
this.ParentForm.Show();
this.Close();
}
Or even better, close Form2 this way:
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.ParentForm != null)
this.ParentForm.Show();
}
This will also show Form1 back if we the user closes Form2 using the cross button in the title bar.
Why don't you handle the login in the form_load event of the main form. The form_load does not run every time the form regains focus. Is it appropriate to close the form on them if they do not login on load? In my case, I send an email with the username of windows user to Domain administrators and close the program. They have to reopen the program to give it another go.
private void frmMain_Load(object sender, EventArgs e)
{
//Check login
Form frmLogin = new Form();
frmLogin.ShowDialog();
if (frmLogin.LoginSucessful == true)
{
btnRecords.Enabled = true;
lblWarning.Visible = false;
}
else
{
btnRecords.Enabled = false;
lblWarning.Visible = true;
lblWarning.Text = "You must first Login";
}
//other setup code here
}
I am trying to open a secondary form immediately after the main form displays, but instead, the secondary form displays first and then the main form displays after the secondary one closes (The second form is acting like a splash screen). Here is an example:
private void Form1_Load(object sender, EventArgs e)
{
doSomething1(sender, e);
doSomething2(sender, e);
// The new form I want to open after the main form.
Form2 f2 = new Form2();
doSomething3(sender, e);
}
private void doSomething1(object sender, EventArgs e)
{
// Do something here...
}
private void doSomething2(object sender, EventArgs e)
{
// Do something here...
}
private void doSomething3(object sender, EventArgs e)
{
// Do something here...
}
Use the Form1.Shown event instead of the Load event:
private void Form1_Shown(Object sender, EventArgs e) {
doSomething1(sender, e);
doSomething2(sender, e);
// The new form I want to open after the main form.
Form2 f2 = new Form2();
doSomething3(sender, e);
}
Alternatively, depending on whether doSomething1 and doSomething2 are doing background processing that the user doesn't need to see: you could retain those two in the load handler, and merely move the last two statements into the Shown handler.
In your constructor initialized the Shown event handler first:
public Form1()
{
InitializeComponent();
this.Shown += new EventHandler(Form1_Shown);
}
Then put the code there:
void Form1_Shown(object sender, EventArgs e)
{
doSomething1(sender, e);
doSomething2(sender, e);
// The new form I want to open after the main form.
Form2 f2 = new Form2();
doSomething3(sender, e);
}
public Form1()
{
InitializeComponent();
Form2 f2 = new Form2();
f2.Show();
I'm putting together a simple UI that interacts with a SQL database. My problem is a UI problem, ever time a menustrip item is selected, it opens a new active window. How do I set this up to close the previous active window? I've tried using Form.Close();, but that just closes everything.
private void addCampusToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_campus go = new if_add_campus();
go.Show();
}
private void addDepartmentToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_dept go = new if_add_dept();
go.Show();
}
private void addEmployeToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_employee go = new if_add_employee();
go.Show();
}
Just keep track of the last form you created in a variable:
private Form lastForm;
private void showForm(Form frm) {
frm.FormClosed += (sender, ea) => {
if (object.ReferenceEquals(lastForm, sender)) lastForm = null;
};
frm.Show();
if (lastForm != null) lastForm.Close();
lastForm = frm;
}
And use showForm() to display your forms:
private void addCampusToolStripMenuItem_Click(object sender, EventArgs e)
{
showForm(new if_add_campus());
}
Not tested, should be close.