I am trying to design an application which creates a new employee data for an organization. My requirement is when a new employee record is being created by any existing user there is a main form of the application on which I need to show that an existing user has created a new user. For example, if the existing user is A and new user is B then the message on the main form should be as mentioned below:
A has created a new User B.
I am stuck in the place where I am not able to understand how to display this message on my main form.
For this do I need to keep listening to the database and in case any new row is created, then display the message accordingly or there is some other way as well as I do not want the application to keep hitting the database unnecessarily.
I am new to C# and so stuck here and need help.
Regards
Vineet More
If you want only pass data from one form to another, you can either:
1) Use dependency injection to pass MainForm to CreateForm and then populate MainForm from that CreateForm (not that good idea).
2) Show CreateForm as Dialog form and return DialogResult when closing CreateForm.
Related
I have been looking for some time on ways to open a second form from another already shown form.
This is some piece of code that works:
frmSecond second = new frmSecond();
this.Hide();
second.ShowDialog();
this.Close();
What it does basically is to Hide() the currently opened form, then it opens another form (the ShowDialog() method). It will only Close() the currently hidden form when the form you have just created is closed.
The problem here is: this way of doing it creates an immense thread of forms. If I need to go from frmSecond to frmThird, it will maintain the first form and the frmSecond being executed in the background, while only showing the frmThird.
Then, as the frmThird is open, if I need to get back to the first form, I would use some code like:
frmFirst first = new frmFirst();
this.Hide();
first.ShowDialog();
this.Close();
And it would create another frmFirst! Then we would have three forms being executed in the background (the first frmFirst, frmSecond, and frmThird).
This method works, but uses an increasingly amount of processing memory, which may be prejudicial for any kind of project.
Is there any alternative or add up to correct this problem?
If anything is unclear, please don't bother in letting me know.
Thank you.
If you want to get access to already created forms try using the static Application.OpenForms property. It contains a list of all of the forms currently open in your application. Documentation is here.
As an example, if you always want to keep frmFirst open and then navigate back to it when you close one of your other forms you can do this:
frmFirst existing = Application.OpenForms.OfType<frmFirst>().FirstOrDefault();
if (existing != null)
existing.Show();
You would need to remove your this.Close() calls for this to work.
To free the memory you need to dispose the form using the Dispose method when it is no longer needed.
Seems like you have a desing error here
Then, as the frmThird is open, if I need to get back to the first
form, I would use some code like:
frmFirst first = new frmFirst();
this.Hide();
first.ShowDialog();
this.Close();
This will leave the original created frmFirst in memory, not visible, doing nothing but eating memory.
If you know you want to get back to frmFirst that was created before, why not just do this :
frmFirst.Show();
and save you lots of memory.
You have 2 choices actually
Hide forms and reactivate them when needed
Close and dispose forms, and recreate them when needed
What you are doing is creating each form over and over again, without getting rid of the prior created forms. Hence you need lots of memory for hidden forms...
I'm working on a simple Outlook 2016 VSTO with a custom ribbon that contains a button and one custom form. I am currently trying to figure out how to display the custom form I created in button click action. I tried instantiating a new form of the custom form (FormAddGroups) type and .Show(); it, but there is something in the constructor I don't understand.
formAddGroups = new FormAddGroups(xxx);
formAddGroups.Show();
It asks me for a formRegion from the current project in the xxx arguement, but I'm not quite sure how to access it, or if it's even the right way of showing it.
I'm also considering creating a custom message class on that button click, but I'm not sure if that's even possible.
Is that the right approach or should I go back?
Call MAPIFOlder.Items.Add("IPM.Note.MyCustomClass") to create the new item and display it (MailItem.Display).
My main form is this
Now my problem occurs when I click Add -> Student, which is meant to bring up a new window that, instead it does this
So as you can see it opens the new form inside the same window. It didn't use to do this, then I made the main form an MdiContainer and set it as the MdiParent of the second form.
Is there a property I can change to make this new form pop up in a new window? Becuase I need to leave the MdiContainer property as True so that I can take data from Form2 and use it in Form1
Please let me know any other information you may need to help me fix this issue, as I'm not sure what caused it so I don't know what to change in order to fix it or even where I'm meant to be looking
you need to hide form enroll
when you open add window add.show()
you need to hide enroll form enroll.hide()
I am making one application in which there is one main Form FormMain and one helper Form FormHelper.
For understanding, take an example that in FormMain the user is typing in the orders and frequently the user wants to open the FormHelper and fill some values in it.
When the order is saved, the values in FormMain is saved as well as FormHelper.
What I did was make the FormHelper a field in the definition of FormMain :
public partial class FormMain : Form
{
FormHelper BillsForm;
}
And, in the constructor :
public FormReceiptNew(string ReceiptNo)
{
InitializeComponent();
BillsForm = new BillsForm();//just once
}
Now suppose that the user, while filling the FormMain, wants to enter some values in FormHelper, he/she can press ALT+H and the Form will be shown using ShowDialog() and when it's done, the user will close the FormHelper, and the same process will happen for as many times the FormHelper is required.
The reason why I want the same Form to open multiple times, is that the user is filling certain values in it and I want to persist the values the next time the FormHelper is shown again and when the user is done completely with the FormMain, the values in the FormMain will be saved along with the values in FormHelper.So when the user presses ALT+H each time, the following code will not work:
BillsForm= new FormHelper();
BillsForm.ShowDialog();
as it is creating a new form and all the old values will be deleted.
In your main form, store a reference to the helper form.
You instantiate the helper form once (onLoad for example) and keep calling showDialog() on that same object. All the fields should be retained between calls.
If you were to store those values in the main form after closing, you could send those values back to the FormHelper upon creation either through the constructor or setters. From your question it already sounds like you're going to be storing those values that you want to persist inside the main form, so setting them upon creation shouldn't be an issue.
I want to make an application that have one main form that have one functionality = Add new form.
I dont know how forms will be created, they are created dynamically by the user (AddForm methos from the main form).
All the subs forms are the same but some of thier prop receive differnce parameters (it can be in the ctor or later).
I want to be able to close all the forms when I close the main form.
Before new form will displayed I want to display setting form (to take the form parameters) maybe with ShowDialog method and do validating check on the form and just if the form validate the new form will displayed, if not (or if the user prees cancel) the form will disposed.
I know aboout MDI but I really perfer other way
Any ideas?
Thanks!
Closing all forms when main form is closed is somewhat easy, you just pass the main form reference in Show() method of the "child" forms; e.g. if you show child from main, you do:
child.Show(this);
This needs to be done if the your main form is not the real "main form of the program", but you want to close all "child" forms.
However, wiring all this together would preferably be done in some special class for this purpose, maybe called ScreenRepository. In this class, you would have a collection of open forms at any moment, you would deffer form creation to this class (so that this class automatically injects form parent) etc... Having this class would be easy to re-activate (give focus) the form if it is behind other forms, create new form if needed etc...
Idea is simple create a application that open a main form a the beginning and then open open other forms if needed if you close main form rest of forms are also closed. Like in GIMP.