Maintain parent child relationship between two form - c#

In my windows forms application, I have two forms, mainForm and recordForm. in main form there is some textboxes and buttons , and on a click of particular button I want to show the recordForm.
But I want when the second form is opened, then user can't perform any operation (like filling textboxes etc) until second form is closed. I tried this:
private void tsCustomer_Click(object sender, EventArgs e)
{
recordForm customers = new recordForm();
customers.Show();
}
tsCustomer is button on mainForm. How can i do this?

Change your code from:
customers.Show();
to:
customers.ShowDialog();

You have to use
customers.ShowDialog();
in order for the customer form to be modal.

How about Form.ShowDialog()?
From MSDN Site : Shows the form as a modal dialog box with no owner window.
This does not effectively relates two forms of your application. But showing the second form in modal form is what you want.

Related

Displaying a panel in another windows form on button click c#

I'm working on a c# program and I want a panel to appear on a form when a button is clicked in another. So when the add button is clicked on form2 the panel requesting the details for this to be possible will be displayed on form 1.
I currently have a static method set up in form1 which can be accessed from form2 - however due to panel.Show() being non static it won't allow me to use this in the function.
In Form1 I have:
public static void showPanel()
{
panel.Show()
}
In my second form I have the following:
private void btn_add_Click(object sender, EventArgs e)
{
form1.showPanel();
this.Hide();
}
I have tested with just having the static function show a message box which works. Is it possible to do it the way I want or do I need to take a few steps back and try a different technique?
Are we talking about a new instance of the second form if so you can try to instantiate the new form using:
Form newForm = new YourFormName(potential parameters);
newForm.showPanel();
newForm.Show();
If you want to execute the form on an open form you can give the first form a reference(field with instance) of the second form. Or you can try using: Application.OpenForms. if you give it [1] it'll give you the second open form probably. You can also use .OfType to get the correct form in case your form order isn't always the same.

Detect CloseEvent through different forms

PROBLEM SOLVED
SHORT STORY
I want to detect "FormClosing()" event through different forms, ie, when form1 is closed that is instantiated within form2, can form2 detect when user presses exit in form1?
LONG STORY
My team and I are working on a windows form application. Project has two forms: one is the main form page and the other is accessed via this main form. Main form looks like this:
And the second one looks like this:
If you press "Ekle/Sil" buttons within the main form, you are directed to form 2 where you can edit database entries. When you press "Sayfayı Yenile" button in the main form, the content of the text areas are refreshed by re-fetching entries from the database.
My problem is, I want to automatically refresh the main form when the user closes the second form. My research suggests I should use an "FormClosing()" event to detect a closing form. However, I want to detect this from the main form. Instantiating main form in second form's source code doesn't seem to be a reliable solution. Anyone can tell me how to do this?
EDIT
I solved the problem:
1) Created a public method within the main form that refreshes the page.
2) Send "this" property from the main form when creating the second form.
3) Added an "FormClosed()" handler within the second form that invokes this public method.
Still, I'm looking for a better solution.
EDIT 2
Better solution InBetween's answer
Simply use the Form.Closed event of the new child windows form. Everything is handled from the main form:
void EkleSil_Clicked(object sender, EventArgs e) //or whatever method is called when button is clicked
{
var newChildForm = new ChildForm();
newChildForm.Closed += childFormClosed;
newChildForm.Show();
}
void childFormClosed(object sender, EventArgs e)
{
((Form)sender).Closed -=childFormClosed;
updateShownData();
}
You can create a event in the second form and raise it when the form is closing .
Handle the event in the main form and refresh the main form when the event is raised
Another option would be to pass Form1 as an argument to Form2. Then use the Form.Closing event in Form2 and use the Form1 reference to trigger something.
Form1 form1Ref;
public Form2(Form1 mainform)
{
form1Ref = mainform;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
form1Ref.SomeMethod();
}

How to pop up a message box when user clicks anywhere within the form window in Visual Studio?

I'm fairly new to Visual Studio in C#.
I was wondering how to pop up a message box when the user clicks anywhere within the form window.
Basically, I don't want them accessing and interacting with the program unless they have a password.
Code for the Form1() is really simple right now:
public Form1()
{
InitializeComponent();
}
The actual interface has a bunch of buttons and settings (buttons and settings I don't want the user to be able to interact with unless they have verified themselves).
regarding the 1st questions.
You usually create a second model form , that is transparent and is on top of your form.
Handle the on-click event of the transparent form .
Regarding the second questions- you should rethink your design , maybe do not show the sensitive form at all untill a user has typed the password.
By the way , you have not specified what tech do you use?
is it desktop (winforms / wpf , other)/ web (web forms, asp mvc, other) ?
recommended reading for client side windows programming.
http://msdn.microsoft.com/en-us/library/dd492132.aspx
Edit:
In order to put a form on top of another form , you use a model dialog.
http://msdn.microsoft.com/en-us/library/39wcs2dh(v=vs.110).aspx
In order to create a form transparent
http://msdn.microsoft.com/en-us/library/czke9azk(v=vs.110).aspx
Also , it still depends on your UI technology.
Provided links are form winforms, other technology may require a different approach.
Edit:
As another answer pointed out , you could also bind to the original forms click event , but you will also have to bind to every child control click event recursively.
Click on form and press F4 key that show property window then click on event button there you click on click event that show in .cs file of form .
Make function like
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("Click");
}

Inactive form and waits for user input and then activate again

I'm doing my semester project and I need little help
Is there any way that a form can be inactivate and some popup box will open and ask user for input, when user types his input, this input will automatically assign to some variable (in inactive form) and then the form will activated.
Example:
How many processes do you want to create ?? (form in background is inactive, and pop up box is waiting for user to type his input), user types 4 and this 4 is assigned to our main form and then form will activated and perform his tasks.
P.S: I know about storing value, I'm just confuse in inactivation and re-activation of form. And Without creating a new form, Is it possible to have such popup box.
All suggestions are welcome.
The behavior you describe is that of a model form. A model form is opened by a parent form and, while shown, prevents the parent form from being interacted with.
You can create a new form and show it as model like this:
ParentForm:
...
Form childForm = new ChildForm();
childForm.ShowDialog();
int result = childForm.Result;
ChildForm:
public int Result;
void OnSubmitButtonPressed(object sender, EventArgs e)
{
Result = inputBox.Value;
Close();
}
Plus error checking, prevent user from closing the child window, etc. You can show the child window in the parent's Loaded event if you need the value as soon as the form is ready.

WinForms programming - Modal and Non-Modal forms problem

I have a problem with modality of the forms under C#.NET. Let's say I have main form #0 (see the image below). This form represents main application form, where user can perform various operations. However, from time to time, there is a need to open additional non-modal form to perform additional main application functionality supporting tasks. Let's say this is form #1 in the image. On this #1 form there might be opened few additional modal forms on top of each other (#2 form in the image), and at the end, there is a progress dialog showing a long operation progress and status, which might take from few minutes up to few hours. The problem is that the main form #0 is not responsive until you close all modal forms (#2 in the image). I need that the main form #0 would be operational in this situation. However, if you open a non-modal form in form #2, you can operate with both modal #2 form and newly created non modal form. I need the same behavior between the main form #0 and form #1 with all its child forms. Is it possible? Or am I doing something wrong? Maybe there is some kind of workaround, I really would not like to change all ShowDialog calls to Show...
Image http://img225.imageshack.us/img225/1075/modalnonmodalproblem.png
Modal forms do exactly what "modal" means, they disable all other windows in the app. That's rather important, your program is in a somewhat perilous state. You've got a chunk of code that is waiting for the dialog to close. Really Bad Things could happen if those other windows were not disabled. Like the user could start the modal dialog again, now your code is nested twice. Or she could close the owner window of the dialog, now it suddenly disappears.
These are the exact kind of problems you'd run into if you call Application.DoEvents() inside a loop. Which is one way to get a form to behave modal without disabling other windows. For example:
Form2 mDialog;
private void button1_Click(object sender, EventArgs e) {
mDialog = new Form2();
mDialog.FormClosed += (o, ea) => mDialog = null;
mDialog.Show(this);
while (mDialog != null) Application.DoEvents();
}
This is dangerous.
It is certainly best to use modal forms the way they were designed to stay out of trouble. If you don't want a modal form then simply don't make it modal, use the Show() method. Subscribe to its FormClosing event to know that it is about to close:
private void button1_Click(object sender, EventArgs e) {
var frm = new Form2();
frm.FormClosing += new FormClosingEventHandler(frm_FormClosing);
frm.Show();
}
void frm_FormClosing(object sender, FormClosingEventArgs e) {
var frm = sender as Form2;
// Do something with <frm>
//...
}
The first thing that comes to mind would be something like this. You could disable form 1 when you launch form 2 and then have form 1 handle the closed event of the second form to re-enable itself. You would NOT open modal 2 using show dialog.
Now keep in mind, from a user perspective this is going to be quite cumbersome, you might look at doing a MDI application to get all windows inside of a single container.
Your main form will not be responsive until any modal dialogs that are in the same process space are closed. There is not work around for that.
It looks to me like you could use an MDI application setting the Form #0 IsMdiContainer property to true.
Then, you could do something alike:
public partial class Form0 {
public Form0 {
InitializeComponent();
this.IsMdiContainer = true; // This will allow the Form #0 to be responsive while other forms are opened.
}
private void button1_Click(object sender, EventArgs e) {
Form1 newForm1 = new Form1();
newForm1.Parent = this;
newForm1.Show();
}
}
Using the ShowDialog() as you stated in your question will make all of the forms Modal = true.
By definition, a modal form is:
When a form is displayed modally, no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (usually in response to some user action) before input to another form can occur. Forms that are displayed modally are typically used as dialog boxes in an application.
You can use this property [(Modal)] to determine whether a form that you have obtained from a method or property has been displayed modally.
So, a modal form shall be used only when you require immediate assistance/interaction from the user. Using modal forms otherwise makes believe that you're perhaps running into a wrong direction.
If you do not want your main form to be an MDI container, then perhaps using multithreading is one solution through a simple BackgroundWorker class is the key to what you want to achieve. Thus, it looks to me like a design smell...
What is it you want to do, apart of making your main form responsive, etc.
What is it you have to do?
Explaining what you have to do, we might be able to guide you altogether into the right, or at least perhaps better, direction.
Actually the answer is very simple. Try
newForm.showDialog();
This will open a new form, while the parent one is inaccessible.

Categories

Resources