How to hide usercontrol and main form c# winforms - c#

how to hide user control and main form. The case is like this,
I have two form and one UserControl. Main form and form registers, and UserControl inside the main form. Inside the UserControl there are two buttons, create and delete. When I press the create button, then UserControl and main form hidden and form register show. I've try, but only hidden UserControl.

Here is MSDN reference to hiding controls Reference
private void btnCreate_Click(object sender, EventArgs e)
{
var registerForm = new Register();//Create instance of register form
this.Hide();//To hide main form
registerForm.Show();//To show register form
userControl.Hide();//To hide user control
//but do not need to hide usercontrol because when form will hide its automatically hide
}

Related

How to hide the form that holds the user control in C#?

I am making a system using C# and I am just new to using the User Control with the forms. So what I wanted to do is to hide the current form that holds the user control using the button placed in the user control. Is there any way to do this? I need the code for the button in user control.
Additional Details:
I have the Form1 in which a user control is docked to it, the user control is named as UserControl1 and it has a button, I want that button to open another form which is Form2 and in the process is to hide the Form1 that holds the UserControl1.
Inside your UserControl:
this.ParentForm.Close();
About ParentForm: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.containercontrol.parentform?view=windowsdesktop-6.0#System_Windows_Forms_ContainerControl_ParentForm
About Close: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.close?view=windowsdesktop-6.0

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();
}

Usercontrol removal

My problem is the following. I have a form which when it loads up invokes a user control onto it. On the user control I have buttons. With the buttons I would like to make the user control disappear with all the buttons and picture boxes on it and make a new user control appear. The appear part is all right but I can't make the user control disappear with all the objects on it.
Can you help me with this one?
So the dispose method closes the usercontrol well, but then I can't make the new user control appear. Code is like this:
akusztikusUserControl auc = new akusztikusUserControl();
public menuUserControl()
{
InitializeComponent();
}
private void akusztikus_btn_Click(object sender, EventArgs e)
{
this.Dispose();
this.Controls.Add(auc);
}
}
Have you considered using Panel or a TableLayoutPanel control ? In both cases you can remove controls through the .controls.remove option
You can then just add the user control to the panel / tablelayoutpanel whenever you need to use it again.

Maintain parent child relationship between two form

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.

Updating textbox on mainform with a variable on a child form that is launched from main form

I have a windows form app. The main form has a textbox, and a button to launch another form. When the other form is launched, both forms are on screen (but the launched form is modal). The child form has a textbox and button, and when the button is pressed, I want the textbox on the main form (the parent) to be updated with the value in the textbox on the child form.
How is this functionality achieved?
Ideally you want to keep both forms from being dependent on each other, this could be achieved with interfaces:
public interface IMainView
{
public void UpdateValue(string val);
}
public interface IChildView
{
public void Show(IMainView parent);
}
have your main form implement IMainView and the child implement IChildView, the parent calls child.show(this) and the child calls parent.UpdateValue(blah);
Hope this helps.
If the child form is closed when the button is clicked, you could put a public property which wraps the value of the textbox on the child form. Then the main form can read this property after calling ShowDialog.
If you want this to happen without closing the child form, you can create a function on the main form to change the textbox. Then the child form would call that function.
The best ways to achive this situation are clockWize's and Hans Passants's advices.
But what about that?
Write a property for your textbox at parent form, like this.
public string TextBoxText
{
get { return txtTextBox.Text;}
set { txtTextBox.Text = value;}
}
When you are opening the child form set the owner.
ChildForm f = new ChildForm();
f.Owner = this;
f.Show();
Create an event handler to child forms button click event.
public Button1_Click(object sender; EventArgs e)
{
ParentForm f = (ParentForm)this.Owner;
f.TextBoxText = txtChildTextBox.Text;
}
i didn't compile code; so may have errors :)
}
When a button is pressed to close the launched form, returning you to the main form- the launched form's text box is still in scope.
Closing a form is merely changing the object's state, not disposing of it. So in the button eventhandler that launches the form from the main form, the next line after launching your modal window, it can access the text from the object it launched as the textbox is a child of that form's object. Unless you're launching your modal window in another thread, which I wouldn't figure you are since it's modal, when it is closed, it should go to the next line in the buttons eventhandler that launched it.
your main form may have code something like this right now (haven't done winforms in a while so bear with me if I miss something):
public void Button1_Click(object sender, ClickEventArgs e)
{
SomeFormIWantToLaunch launchForm = new SomeFormIWantToLaunch();
launchForm.ShowDialog(this);
}
You need to just add after launchForm.ShowDialog(this); something like:
this.SomeTextBox.Text = launchForm.ATextBox.Text;

Categories

Resources