Keep form on top and open and close other forms - c#

I'm new to C# and could use some help.
What I have so far is a set of 8 windows forms I have created in C#, these forms have basic things like text boxes, labels, radio buttons, etc. Now that I have completed making all of these forms I want to have one additional form (called the Selector form) I can use to select one of the other 8 forms. At any given time, I want the Selector form to be on top of other windows and it will have 8 radio buttons (or regular buttons, doesn't matter). When one of the buttons is clicked, the current form (not the Selector form) should disappear and a new form should appear. The name of the button will be the name of the new form that appears.
I have seen a few examples and here is the code I have so far:
void Button1Click(object sender, EventArgs e)
{
//this.Hide();
var form1 = new CASII();
form1.Closed += (sender1, args) => this.Close();
form1.Show();
}
void Button2Click(object sender, EventArgs e)
{
// this.Hide();
var form2 = new CCARAdmin();
form2.Closed += (sender1, args) => this.Close();
form2.Show();
//Application.Run(new CCARAdmin());
}
Problem I am having is I don't want to hide the Selector form, which this does, and I don't know how to identify the other form that is open to close it and then open a different form.
From starting the program the logic would be like this:
Show Selector form
When a button is clicked on the Selector form, keep the Selector form on top and show the other form with the name of the button.
When a different button is clicked on the Selector form, close the previous form that was open (not the Selector form) and open the new form corresponding to the name of the button. Keep the Selector form on top.
When the Selector form is close, application stops.

Problem I am having is I don't want to hide the Selector form, which
this does, and I don't know how to identify the other form that is
open to close it and then open a different form.
Set Selector form TopMost to True to make it always on top. Or
you can use BringToFront after opening a new form
to know other forms that are open check this answer. Or you can define each From as a field in the Selector form, and check that.

selectorForm.TopMost = true ( this will help to keep the selector form always on top).
Create a form variable in your selector form to keep the reference of your currently opened form.
Sample code for 1 button click :
Form frm = null;
void Button1Click(object sender, EventArgs e)
{
//this.Hide();
var form1 = new CASII();
if (frm == null)
{
frm = form1;
}
else
{
frm.Close();
}
form1.Show();
this.TopMost = true;
frm = form1;
}

I resolved this by setting TopMost to true and then using the following code under each one of the buttons:
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
if (Application.OpenForms[i].Name != "FormSelector")
Application.OpenForms[i].Close();
}
var form = new TRAC();
if (radioButton9.Checked == true)
{
form.Show();
}

Related

C# Win Forms not opening

I have a WinForm listbox selection of a list of forms that the user can go into.
I have a function that should open the new form
private void sendToDB_Button_Click(object sender, EventArgs e)
{
string selected_item = listBox1.SelectedItem.ToString();
Form _secondForm = new Form();
if (selected_item == "389")
_secondForm = new Forms._389_Form();
else if ( selected_item == "120" )
_secondForm = new Forms._120_Form();
//... Repeat for 30 more forms ...
this.Hide();
_secondForm.Show();
}
When running the application and I select "389" the current form closes as it should but nothing is opened in a new form. Curious if having the forms in a folder called Forms is the issue here. The line in question is _secondForm = new Forms._389_Form(); and is this breaking the application?
Instead of _secondForm.Show(); I changed it to _secondForm.ShowDialog(); and I got the expected results.
According to this link: https://stackoverflow.com/a/20859112/8149159 , The author of the answer states that:
The Show function shows the form in a non modal form. This means that you can click on the parent form.
ShowDialog shows the form modally, meaning you cannot go to the parent form
try
this.Hide();
_secondForm.ShowDialog();
//if you want close precedent form
this.Close();
Hide is for "hide" all action at the user
ShowDialog is for open the form and
Close is for close the precedent form

Creating new pages in Visual Studio using C# Windows Forms

I am trying to write a Windows Forms application using C#, however I have never done this before. I have successfully created a login page which takes the user to a home page where 4 buttons are displayed. I am trying to find code to put inside each button that will take the user to a different page.
In windows forms application, you have to call Show method on the Form which you want to show.
Let's say on Button1 click you want to show Form2, then below code will bring Form 2 up on the screen.
private void button1_Click_1(object sender, EventArgs e)
{
Form2 obj = new Form2();
if (obj == null)
{
obj.Parent = this;
}
obj.Show();
this.Hide();
}
You need to create, initialize and show the other forms.
frmSecond frm = new frmSecond();//You should call any other constructor, may be with some parameters
frm.Text ="I wanted to change the title";//Optional: You can change any property value if you need
frm.Show();
If you want to show the form, collect some information from user and return some values then you can use ShowDialog method instead of Show method
if(frm.ShowDialog(this) == DialogResult.OK){
var myVar = frm.ReturnObject;
...
}
You can use below approach for one parent window and new child window.
When you press the button new form opens:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
{
frm2.ShowDialog();
}
}

C#, WinForm in another winform

I need help. I have winform, like one which is shown on picture, so I want to improve next things, when I click on first button, on the right side will be showed content from another winform, is this possible? I don't want to use panels.
Why don't you just embed a single UserControl dynamically to represent the current page or if you want to do everything at design-time - a custom TabControl?
showed content from another winform
You generally don't embed a popup window into another, rather controls. Otherwise you have to deal with hiding Minimise, Maximise, Close etc.
MDI Form is the best solution for you.
Use: this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;`
to make your child Forms more beautiful in your project!
If you want the MDI parent to auto-size the child form you can code like this.
Code Example:
private void Form1_Load(object sender, EventArgs e)
{
IsMdiContainer = true;
}
private void btnHotel_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
frm2.MdiParent = this;
}
private void btnCaffe_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show();
frm3.MdiParent = this;
}
You can use TabControl control -> for each specific content you want to display you just have to add a TabPage to TabControl and when a specific menu option is selected, just switch to required TabControl page.
myTabControl.SelectedIndex = 1; // for selecting and displaying page with index 1
To hide navigation header of myTabControl, set those props (in constructor or in Form_Load event:
myTabControl.ItemSize = new Size(0, 1);
myTabControl.SizeMode = TabSizeMode.Fixed;
Then, you have only content of the TabControl page displayed, without navigation header.
First make it's this.IsMdiContainer = true; either from design time or run time.
then at the button's click event place the following code.
childForm frm = new childForm();
frm.MdiParent = this; //the current mdi parent form
frm.FormBorderStyle = FormBorderStyle.None;
frm.Dock = DockStyle.Fill;
frm.Show();
output
You can use mdi (multiple-document interface):
when you want a form start inside mainform, use this following code:
Dim hotelForm As New HotelForm()
HotelForm.MdiParent = Me // Me is your parent form want to open hotelForm inside
HotelForm.Show()
All forms have a property TopLevel, by setting this property to false, then you can deal with that form as a control, and add it to a panel control.
See below psuedo code:
Form2 newForm = new Form2();
newForm.TopLevel = false;
myPanel.Controls.Add(newForm);
newForm.Show();
After that, your main form design should look like as a navigation control and a panel beside it docked "Fill", then on clicking any navigation button, just create the desired form and set TopLevel to false, then show it on the panel
You have to put a Container control like a panel "pnlHost" in your form and use this for showing any form you want in it
private Form _currentForme;
private void ShowForm(Form frm)
{
_currentForme?.Close();
_currentForme = null;
_currentForme = frm;
_currentForme.TopLevel = false;
_currentForme.TopMost = false;
pnlHost.Controls.Clear();
_currentForme.FormBorderStyle = FormBorderStyle.None;
pnlHost.Controls.Add(_currentForme);
_currentForme.Dock = DockStyle.Fill;
_currentForme.Show();
}
I would use MDI parent and child for this.
But I guess you could also finish the child application, build the exe and initiate it as a new process from your Parent application.
Check the solution given here.
I tried a similar approach long ago and it worked perfectly.
Set current form as a MdiParent then use Datagridview to display data from other forms

Enable/Disable another form WITHOUT INSTANTIATING NEW ONE

Here's my question.. From form 1, i want when the user clicked on update items, Form 2 will open but form 1 will still remain open. At this point, I want the Form 1 to be enabled false and I've done it. But I want when the form 2 closes, the form 1 enable should be true again. I cannot do this... Here's my code:
In Form 1:
private void btnEditItem_Click(object sender, EventArgs e){
Form3 form3 = new Form3();
form3.Show();
this.Enabled = false;
}
In Form 2(which is for update items, after updating):
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
Form1 form1 = new Form1();
form1Here.Enabled = true;
}
In this situation, it will open another Form1. The question is, how am i suppose to "ENABLE BACK AGAIN THE FORM 1 WITHOUT OPENING IT?" HELP ME PLEASE PROVIDE SOME SAMPLE IF POSSIBLE. THANKS
Attach a handler to your form.
Form3 form3 = new Form3();
form3.FormClosed += new FormClosedEventHandler(frm3_FormClosed);
form3.Show();
private void frm3_FormClosed(object sender, FormClosedEventArgs e)
{
this.Enabled = true;
}
Or you can use ShowDialog
Opens a window and returns only when the newly opened window is closed.
From your description it seems you want a modal Dialog Box.
From MSDN Modal and Modeless Dialog Boxes:
A modal dialog box must be closed (hidden or unloaded) before you can
continue working with the rest of the application
This means that if Form 1 opens Form 2 as a modal dialog box, form 1 will remain visible, but it is disabled, you can't do anything with it until you've closed form 2. In your words: "form 1 is disabled and when form 2 closes form 1 is enabled again".
The code:
private void btnEditItem_Click(object sender, EventArgs e)
{
using (Form3 form3 = new Form3()}
{
var dlgResult = form3.ShowDialog(); // show form 3 as a modal dialog box
// halt this procedure until form3 is closed
// handle the result of form3:
if (dlgResult == DialogResult.OK)
{
var x = form3.SomePropery;
ProcessDialogOutput(x);
}
}
}
The using statement is not necessary, but it is neat programming, because Form3 implements IDisposable

Windows Forms C#

I am learning windows forms and can create a one form with textboxes and stuff, but I was wondering how can I change the form upon let's say clicking a button?, so for instance my initial form has a textbox and a button, if the button is clicked I want to show a form with a dropdown and a button. SO the question should be:
1) How do I change the form upon clicking a button but without creating a new instance of the form.
2) If I wanted, how can I add a form when the button is clicked showing the same drop down and button as a pop up form?
In reality I would like to know both cases, changing the form via using the same form and via popping a new form on top.
Should the questions not be clear, I am willing to further explain
Thank you
I'm assuming you already know how to add controls in the form designer and how to implement event handlers.
Question 1
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Visible)
{
comboBox1.Visible = false;
textBox1.Visible = true;
}
else
{
comboBox1.Visible = true;
textBox1.Visible = false;
}
}
The button click handler simply toggles the visibility of the two controls.
Question 2
private void button2_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.ShowDialog();
}
This time the button handler instantiates a new form an then shows it as a modal dialog. Call Show() if you don't want to show modally.

Categories

Resources