communicating between winForms C# (Log-in process) - c#

Introduction of problem:
I have two forms Home.cs and Login.cs. I have ToolStripMenuItems in Home.cs, Admin will log-in from the Login.cs form. On form_load (Home.cs) event I had made two menu items disabled.
addToolStripMenuItem.Enabled = false;
editToolStripMenuItem.Enabled = false;
After successful login I want to enable those menu items in Home.cs. But is not able to figure out how to do that in C#. I thought I can do something like this:
private Home hm = null;
and then in authentication event I can do....
hm.addToolStripMenuItem.Enabled = true;
Problem:
But this is not working, and this is not the right way to handle this situation. Plz help......

You could simply call the Login.cs from the Load event of the Home.cs form/class , just like that:
public void Home_Load(...params...)
{
Login log = new Login();
if(log.ShowDialog() == DialogResult.Ok)
{
// enable the menu here
}
else
{
// let the menu disabled or exit the application here
}
}

Related

How do i send a value from my first form to my main form in c#

Im making a little log in interface and im trying to send the user id after a succesfull login to my main form for safekeeping. I know how to do it from main to a secondary but i have no idea how to do it back. as im not creating a new Form main = new form();
public void btnLogIn_Click(object sender, EventArgs e)
{
UserClass existingUser = new UserClass();
existingUser.username = tbUsername.Text;
existingUser.password = tbPassword.Text;
result = databaseConnector.LogUSerIn(existingUser.username, existingUser.password);
if (result == 0)
{
MessageBox.Show("There aren't any registerd users with these credentials. Please try again or register below");
}
else
{
MessageBox.Show("Log in succesfull, Happy browsing!");
//Return value to Main form.
LogIn.ActiveForm.Close();
}
I would like the value result be send back to my main form. ive browsed the internet for about half an hour now and i only see from MAIN to SECONDARY but i can never find the reversed.
You can do it in a similar way like the OpenFileDialog does it.
Create a public property UserId in your login form and set the user id when the user logs in.
You can now access this property UserId in your main form after the ShowDialog() method of your login form has been called.
I can look something like this in your main form:
var loginForm = new LoginForm();
loginForm.ShowDialog();
var userId = loginForm.UserId;
// do something with the userId. maybe store it in a property of your main form.

disable browser close button in asp.net application?

I am doing online Exam application using asp.net in this i have to disable the titlebar so that the user has no option to exit with in the time period.So please help with this one
Its not good practice to force user to stay on the page if they don't wish to, but you can have some work around if you want to confirm the close event before they leave the browser tab
function internalHandler(e) {
return "Please don't leave the page you can be fail in exams!";
}
if (window.addEventListener) {
window.addEventListener('beforeunload', internalHandler, true);
} else if (window.attachEvent) {
window.attachEvent('onbeforeunload', internalHandler);
}
If you prevent user to close it any way you don't have control over ALT + F4 or closes it from Task Manager
you can do it using javascript like this
var message = "You have not completed exam. Are you sure you want to leave?";
window.onbeforeunload = function(event) {
var e = e || window.event;
if (e) {
e.returnValue = message;
}
return message;
};
and you can unload it when user finish the exam
window.onbeforeunload = null;
or you can create your own browser application using c# windows forms. where you can set this custom option without having close button. You load your web application form in windows forms application easily.
onbeforeunload & onunload will help you out. You can't disable but you can show user an alert.
var showMsgTimer;
window.onbeforeunload = function(evt) {
var message = 'Don't Discard';
showMsgTimer = window.setTimeout(showMessage, 500);
evt = evt || window.evt;
evt.returnValue = message;
return message;
}
window.onunload = function () {
clearTimeout(showMsgTimer);
}
function showMessage() {
alert("You're Right!");
}
If this is not the one you expect. Then please try https://eureka.ykyuen.info/2011/02/22/jquery-javascript-capture-the-browser-or-tab-closed-event/

C# DialogForm opening when calling for mainForm

I have a problem with my dialogForm. This is the code that opens my dialogForm (this is a login form) when my mainForm starts to run.
private void indexForm_Load(object sender, EventArgs e)
{
startForm loginForm = new startForm();
loginForm.ShowDialog();
indexUsername.Text = klasseGebruikersnaam.gebruikersnaam;
}
So when my indexForm (Main form) starts , it first loads a dialogForm, which is my login form.
Now my problem is that whenever I try to acces the mainForm from another form using this code (for example when I click a button):
this.Hide();
indexForm inf = new indexForm();
inf.Show();
The dialogForm pops up again. So I want to show my mainForm but , when I load my mainForm my dialogForm always pops up.
Any way around this?
Thanks in advance.
The problem is that you are loading your loginForm from your Main Form's Load event. Which is always going to fire after the constructor of the Main Form is called. Typically you will want to launch the loginForm from somewhere before the Main Form is loaded. You could do this in your Program.cs file and make it the main entry point of the program. Or just simply check if the user is already logged in.
Here is an example of both:
Program.cs
static void Main()
{
//Auto-generated code that VS writes for you
using (var loginForm = new LoginForm())
{
if (loginForm.ShowDialog() == DialogResult.Yes) //Presumably it would only return Yes if the login was successful.
{
Application.Run(new MainForm()); //Or however you call your main form
}
}
}
Of you can just put a property on the Main Form that determines if the user is logged in. Then you can call it in the Load event still.
Load Event
if(!this.UserLoggedIn)
{
loginForm.ShowDialog();
//Do something with the dialog result.
}
In my opinion it is better to user the Program.cs approach because if the user fails to login correctly, you can just exit or handle it as needed without loading your Main Form at all. The way you currently have it, the main form must load before the login form is shown, which could be problematic.
Well, you should remove that code from your main form and call it before showing the main form.
Or you could simply set a global variable that keeps the info for the current logged in user and, if that variable is not null, don't call again the login form
So, supppose that you login form prepare an instance variable of type LoggedinUser
public class LoggedinUser
{
public string NickName {get;set;}
public string UserRole {get; set;}
...
}
then in an utility class (or in your index form) you could have a static variable
public static LoggedinUser currentOperator = null;
in your in index_form you could write
if(GlobaClass.currentOperator == null)
{
using(startForm loginForm = new startForm())
{
if(loginForm.ShowDialog() == DialogResult.OK)
GlobalClass.currentOperator = loginForm.LoggedUser;
}
}
looks like you need to add a check to see if the user is logged in around the
loginForm.ShowDialog();
something like
if(!UserLoggedIn())
{
loginForm.ShowDialog();
}

C# ActiveX--how to keep the pops-up dialog(windows.Forms.Form) always on top of browser(IE8)

I have write the ActiveX using C# to communicate with the other browser-based system b, it need pops up an dialog in an other thread because of the existing architecture. the current behavior is that the dialog can be hidden behind if i click the browser title. Is it possible to keep the pops-up dialog always on the top of browser(IE8)? Thanks in advance.
public int operation()
{
....
MyMsgBox myMsgBox = new MyMsgBox(message,title);
evt = System.Threading.AutoResetEvent(false);
Thread showDialogThread = new Thread(ShowMsgDialog);
ShowDislogThread.SetApartmentState(System.Threading.ApartmentState.STA);
showDialogThread.Start(myMsgBox);
System.Threading.WaitHanle.WaitAll(new System.Threading.WaitHandle[] {evt});
....
}
public void ShowMsgDialog(object requestObj)
{
MyMsgBox msgBox = (MyMsgbox)requestObj;
msgBox.showDialog();
evt.Set();
}
Class MyMsgBox:Form
{
public MyMsgBox(string message, string title)
{
//do initialization....
}
}
I have tried to set the TopMost of Form to 'true', then it will be always on the top of all applications. it's not meet the requirement as the pops-up dialog need be only always on the top of browser. Thanks.
I don't think that what you want will be possible.
However, you can make a div stretched across all page and set and event on mouse move to call BringToFront on your ActiveX object. That should do the trick.

How to move wpf application into minimize tray at Window Start-up C#?

I have created setup of my application using Windows Installer.
Now I want to Start application at Windows Start-Up and move it system minimize tray as i don't want to display GUI(View) at Windows Start-Up.
I have searched in Google and i found to use Registry key But that is not enough for me as i also want to move to system minimize tray and application run.
My purpose to do it is, user do not feels annoying when application starts every time when he/she starts system.
Can anyone have answer?
Thanks..
In your application, add an event handler for the FrameworkElement.Loaded event. In that handler, add the following code:
WindowState = WindowState.Minimized;
This will minimise the application when it starts.
To start the application when the computer starts, you'll need to add your program into Windows Scheduler and set it to run at startup. You can find out more on the Schedule a task page at MSDN.
You also have to set this property to remove it from the taskbar
ShowInTaskbar= false;
Maybe this answer is late, but I still want to write it down to help those who haven't found solutions yet.
Firstly you need to add a function to minimize your app to tray when it autostarts as system startup.
In your App.xaml file, change the original StartupUri=... to Startup="App_Startup" as below. App_Startup is your function name and can be changed.
<Application x:Class="Yours.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="App_Startup">
In your App.xaml.cs file. Add the function below:
public partial class App : Application
{
private void App_Startup(object sender, StartupEventArgs e)
{
// Process command line args
var isAutoStart = false;
for (int i = 0; i != e.Args.Length; ++i)
{
if (e.Args[i] == "/AutoStart")
{
isAutoStart = true;
}
}
// Create main application window, starting minimized if specified
MainWindow mainWindow = new MainWindow();
if (isAutoStart)
{
mainWindow.WindowState = WindowState.Minimized;
}
mainWindow.OnAutoStart();
}
}
In your MainWindow.xaml.cs, add a function as below:
public void OnAutoStart()
{
if (WindowState == WindowState.Minimized)
{
//Must have this line to prevent the window start locatioon not being in center.
WindowState = WindowState.Normal;
Hide();
//Show your tray icon code below
}
else
{
Show();
}
}
Then you should set you app utostart as system start.
Now if you have a switch to decide whether you app to autostart as system start, you can just add the function below as your switch status changed event function.
private void SwitchAutoStart_OnToggled(object sender, RoutedEventArgs e)
{
const string path = #"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
var key = Registry.CurrentUser.OpenSubKey(path, true);
if (key == null) return;
if (SwitchAutoStart.IsOn)
{
key.SetValue("Your app name", System.Reflection.Assembly.GetExecutingAssembly().Location + " /AutoStart");
}
else
{
key.DeleteValue("Your app name", false);
}
}
If you want to automatically start the application for all users on Windows startup, just replace the forth line with
RegistryKey key = Registry.LocalMachine.OpenSubKey(path, true);
^_^

Categories

Resources