I have 3 forms. Names: MainScreen, LoadingForm, MoviesInfo.
When I press the button on the MainScreen, it is doing some works and the LoadingForm is loading inside a panel on the MainScreen.I want to do when works done, show the MoviesInfo Form on the same panel on the MainScreen or panel on the LoadingForm. How can I do that?
I add the forms on the panel like that.
public static void AddFormToPanel(Form frm, Panel panel)
{
frm.TopLevel = false;
panel.Controls.Add(frm);
frm.Show();
frm.Dock = DockStyle.Fill;
frm.BringToFront();
}
//Loading Form
public partial class LoadingForm : Form
{
public Action Worker { get; set; }
public LoadingForm(Action worker)
{
InitializeComponent();
if (worker == null)
{
throw new ArgumentNullException();
}
Worker = worker;
}
private void btnCancel_Click_1(object sender, EventArgs e)
{
this.Close();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Task.Factory.StartNew(Worker)
.ContinueWith(t => { this.Close(); }, TaskScheduler.FromCurrentSynchronizationContext());
}
}
MainScreen Form
private void MovieOnButtonClick(object sender, EventArgs eventArgs)
{
using (loading = new LoadingForm(getMovieData))
{
loading.ShowDialog(this);
}
AddFormToPanel(moviesInfo, panelMain);
}
I don't want this line loading.ShowDialog(this); I want to add loading form inside the panel.
Move the action and the control to the MainForm:
private async void MovieOnButtonClick(object sender, EventArgs eventArgs)
{
var loadingForm = new LoadingForm(); // create a dummy loadingForm
AddFormToPanel(loadingForm, panelMain);
var work = Task.Factory.StartNew(Worker); // Worker = GetMovies or so
await work;
AddFormToPanel(moviesInfo, panelMain);
loadingForm.Dispose();
}
Remove all logic and events from the LoadingForm.
And it is better to use UserControl than Forms to place in that Panel.
Related
I have one Windows Form application and there are several user controls in it.
I show them by pressing their buttons via this method;
private void UC_Bring(Control uc)
{
uc.Dock = DockStyle.Fill;
panelControls.Controls.Clear();
panelControls.Controls.Add(uc);
}
For example;
-First button is "Home" button. (this is the startup page)
-Second button is "Operation" button.
I click the "Operation" button and i fill some text boxes in this user control. But if i click the "Operation" button again, the user control resets itself and text boxes are being clear.
I want not to bring the user control again if mentioned user control is in use / already brought to screen. How can i do that?
Screenshot
Thanks in advance.
I tried that before
private void UC_Bring(Control uc)
{
uc.Dock = DockStyle.Fill;
if (uc.Visible!=true)
{
panelControls.Controls.Clear();
panelControls.Controls.Add(uc);
}
}
I edit this with button click event together with bringing method;
private void UC_getir(Control uc) //User control çağırma metodu
{
uc.Dock = DockStyle.Fill;
panelControls.Controls.Clear();
panelControls.Controls.Add(uc);
}
private void BtnOp_Click(object sender, EventArgs e)
{
UC_Op ucOp = new UC_Op();
UC_getir(ucOp);
}
You are very likely creating a new control every single time. You just need to Clear() the Panel and Add() the new UserControl each different clicks. If the UserControl is not in a container it is not visible. That mean you also don't need to fool around with the visibility. Here's a totally working solution of just that behavior.
public partial class Form1 : Form
{
private ucHome HomeUserControl = new ucHome();
private ucOperations OperationsUserControl = new ucOperations();
private ucMaterials MaterialsUserControl = new ucMaterials();
private ucSettings SettingsUserControl = new ucSettings();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
HomeUserControl.Dock = DockStyle.Fill;
OperationsUserControl.Dock = DockStyle.Fill;
MaterialsUserControl.Dock = DockStyle.Fill;
SettingsUserControl.Dock = DockStyle.Fill;
}
private void UC_Bring(Control uc)
{
panelControls.Controls.Clear();
panelControls.Controls.Add(uc);
}
private void btnHome_Click(object sender, EventArgs e)
{
UC_Bring(HomeUserControl);
}
private void btnOperations_Click(object sender, EventArgs e)
{
UC_Bring(OperationsUserControl);
}
private void btnMaterials_Click(object sender, EventArgs e)
{
UC_Bring(MaterialsUserControl);
}
private void btnSettings_Click(object sender, EventArgs e)
{
UC_Bring(SettingsUserControl);
}
}
public class ucHome : UserControl { }
public class ucOperations : UserControl { }
public class ucMaterials : UserControl { }
public class ucSettings : UserControl { }
I have a form that is being shown using ShowDialog(), thus it is a modal window.
private void OpenForm(object sender, ItemClickEventArgs e)
{
MyForm testForm = new MyForm();
...
testForm.Enabled = true;
testForm.ShowDialog(this);
var dialogOk = testForm.DialogOK;
if(dialogOk)
{
//do some stuff 1
}
}
There is an "OK" button on the form. When OK is clicked, DialogOk is set to true. Inside MyForm class:
private void OkClick(object sender, EventArgs e)
{
// do some stuff 2
...
DialogOK = true;
Hide();
}
I need to convert this to a non-modal window. The solution seems to be to use Show() instead of ShowDialog(), but when I use Show(), the code does not stop and wait for the OK button to be clicked, so "do some stuff 1" is never called.
Using Show(), how can I keep the behavior to have "do some stuff 1" run after the OK button is clicked?
Update: Here is what I am trying now:
public partial class MyForm: XtraForm
{
public bool DialogOk;
private void OkClick(object sender, EventArgs e)
{
// do some stuff 2
...
DialogOk = true;
Close();
}
}
Method 1:
public partial class MyMainForm : XtraForm
{
private MyForm testForm;
private void OpenForm(object sender, ItemClickEventArgs e)
{
if(testForm == null)
{
testForm = new MyForm();
}
...
testForm.Enabled = true;
testForm.FormClosed += (s, a) => {
var dialogOk = testForm.DialogOk;
if (dialogOk)
{
// do some stuff 1
}
};
testForm.Show(this);
}
}
Method 2:
public partial class MyMainForm : XtraForm
{
private MyForm testForm;
private void OpenForm(object sender, ItemClickEventArgs e)
{
if(testForm == null)
{
testForm = new MyForm();
}
...
testForm.FormClosed += testForm_Closed;
testForm.Show(this);
}
private void testForm_Closed(object sender, EventArgs args)
{
var testForm = (Form)sender;
testForm.Closed -= testForm_Closed;
if (testForm.DialogResult == DialogResult.OK)
{
// do some stuff 1
}
}
}
You can handle Form.Closed event:
MyForm testForm = new MyForm();
testForm.Closed += testForm_Closed;
testForm.Show();
private void testForm_Closed(object sender, EventArgs args)
{
var testForm = (Form)sender;
testForm.Closed -= testForm_Closed;
if (testForm.DialogResult == OK)
// do some stuff 1
}
The easiest way is to move the code from OpenForm to the event handler OkClick. However, if this is not a good spot to put the code because you might want to use the same form for different tasks, you could add a handler for the FormClosed event, that is called after the form is closed and runs the code, e.g.:
private void OpenForm(object sender, ItemClickEventArgs e)
{
MyForm testForm = new MyForm();
...
testForm.Enabled = true;
testForm.FormClosed += (s, a) => {
var dialogOk = testForm.DialogOK;
if(dialogOk)
{
//do some stuff 1
}
};
testForm.Show(this);
}
You can use an async event handler tied to an TaskCompletionSource which listens and awaits the close of the form
private asyc void OpenForm(object sender, ItemClickEventArgs e) {
var source = new TaskCompletionSource<DialogResult>();
EventHandler handler = null;
handler = (s, args) => {
var form = (MyForm)s;
form.FormClosed -= handler;
source.SetResult(form.DialogResult);
}
var testForm = new MyForm();
testForm.FormClosed += handler; //subscribe
//...
testForm.Enabled = true;
testForm.Show();
var dialogOk = await source.Task;
if(dialogOk == DialogResult.Ok) {
//do some stuff 1
}
}
With that you can keep the logic in the OpenForm and allow the code to wait without blocking.
In the form when the button is clicked then all you need to do is set the dialog result and close the form.
public partial class MyForm: XtraForm {
//...
private void OkClick(object sender, EventArgs e) {
// do some stuff 2
// ...
DialogResult = DialogResult.Ok;
Cose();
}
}
This works for me, so not sure why it isn't for you (scratching head)... This form has two buttons, one which opens the same form again and another button that closes the form. The 'parent' form adds an event to the Closed event.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 test = new Form1();
test.FormClosed += Test_FormClosed;
test.Show();
}
private void Test_FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show("closed -- do something else here!");
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}
I have a countdown and it outputs to a label on the main form, I want to popout the countdown from the main form to a child form because the child form will be able to stay on top of all other windows (for game) I can't get it to updated the countdown on the child's label. I only get the second when i hit the popout button.
This is on the main form
private void tmrMain_Tick(object sender, EventArgs e)
{
TimerSeconds = TimerSeconds - 1;
if (TimerSeconds == 0)
{
tmrMain.Stop();
if (chbxPlaySound.Checked)
{
System.Media.SystemSounds.Exclamation.Play();
}
if (chbxRepeat.Checked)
{
btnStartTimer.PerformClick();
}
}
string dis_seconds = Convert.ToString(TimerSeconds);
//
// lblTimer is the label that shows the countdown seconds
//
lblTimer.Text = dis_seconds;
}
This is my pop out button on the main form
private void btnPopOut_Click(object sender, EventArgs e)
{
PopTimer ShowTimer = new PopTimer(TimerSeconds);
ShowTimer.Show(this);
}
This is my child form
public partial class PopTimer : Form
{
public PopTimer(int countTimer)
{
InitializeComponent();
//string dis_seconds = Convert.ToString(TimerSeconds);
lblPopTimer.Text = Convert.ToString(countTimer); ;
}
}
Your PopTimer form needs an accessible method, something like this:
public void SetCountdown(int value) {
lblPopTimer.Text = value.ToString();
}
Then your PopTimer needs to be accessible, so move the declaration:
private PopTimer showTimer = null;
private void btnPopOut_Click(object sender, EventArgs e)
{
showTimer = new PopTimer(timerSeconds);
showTimer.Show(this);
}
Then in your tick event:
if (showTimer != null) {
showTimer.SetCountdown(timerSeconds);
}
In one of our apps we want to want to limit the user from opening other menu items when an existing menu item is already open. We are currently doing this:
private void menuItem1_Click(object sender, EventArgs e)
{
Myform f = new MyForm();
f.ShowDialog(this);
}
However in doing this, we lose the ability to interact at all with the parent window because internally, the parent.enabled property was set to false. Using the code above, if the user has menu item open and wants to move the parent window to see something on their desktop, they first must close the menu item, move the parent, and reopen the menu item.
I have come up with the follow method of doing the UI in a backgroundworker
public class BaseForm : Form
{
private bool _HasChildOpen;
protected BackgroundWorker bgThead;
public BaseForm()
{
_HasChildOpen = false;
bgThead = new BackgroundWorker();
bgThead.DoWork += new DoWorkEventHandler(OpenChildWindow);
bgThead.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.ClearChildWindows);
}
protected void ClearChildWindows(object sender, RunWorkerCompletedEventArgs e)
{
_HasChildOpen = false;
}
public void OpenChildWindow(object sender, DoWorkEventArgs e)
{
if (!_HasChildOpen)
{
Form f = (Form)e.Argument;
f.StartPosition = FormStartPosition.CenterScreen;
f.ShowDialog();
}
}
}
and then each menu item has the following code
private void menuItem1_Click(object sender, EventArgs e)
{
if (!bgThead.IsBusy)
{
bgThead.RunWorkerAsync(new Myform());
}
}
but this approach is a big no no. However, using invoke seems to get me back where I started:
private void doUIWork(MethodInvoker d)
{
if (this.InvokeRequired)
{
this.Invoke(d);
}
else
{
d();
}
}
public void OpenChildWindow(object sender, DoWorkEventArgs e)
{
if (!_HasChildOpen)
{
doUIWork(delegate() {
Form f = (Form)e.Argument;
f.StartPosition = FormStartPosition.CenterScreen;
f.ShowDialog();
});
//Form f = (Form)e.Argument;
//f.StartPosition = FormStartPosition.CenterScreen;
//f.ShowDialog();
}
}
How do I properly limit the user to just one menu item open, but at the same time leave the parent enabled such that it can be moved resized etc?
You will need to programmatically disable menu strip behavior once one of the forms is open. So if you have Form1 and Form2, (with a menuStrip on Form1 and toolStripMenuItem1, toolStripMenuItem2 on the menuStrip):
private void menuItem1_Click(object sender, EventArgs e)
{
var f2 = new Form2();
f2.FormClosing += f2_FormClosing;
f2.Show();
this.menuStrip1.Enabled = false;
}
private void menuItem2_Click(object sender, EventArgs e)
{
var f2 = new Form2();
f2.FormClosing += f2_FormClosing;
f2.Show();
this.menuStrip1.Enabled = false;
}
void f2_FormClosing(object sender, FormClosingEventArgs e)
{
this.menuStrip1.Enabled = true;
}
using the Show() method instead of ShowDialog() enables interaction with the parent control, though you will need to manually disable/enable behavior depending on when the child control is shown or not.
I've got two forms, with subForm being called/created by a buttonClick in Form1. Right now I can initiate subForm, hide Form1, and then unhide Form1 when subForm is closed. What I'd like to be able to do is:
If user clicks changeform button, check to see if subForm is active but hidden
If no, then initiate subForm, else hide Form1, unhide subForm and pass control to it
If user clicks subForm's changeform button, hide subForm, unhide Form1 and pass control to it
If user clicks the "X" in the upper right corner of the form, then close the application, regardless of which form is active. (Right now, selecting the "X" closes the subForm and opens/unhides Form1.)
I can find solutions that do part of the requirements (and maybe all, I'm just too noob to know). To repeat from my previous question here, the code I have so far is:
Form1
private void countClick(object sender, EventArgs e)
{
this.Hide();
subForm myNewForm = new subForm();
myNewForm.ShowDialog();
this.Show();
countSelect.Checked = false;
}
and subForm
private void totalClick(object sender, EventArgs e)
{
this.Close();
}
This works, but it's not really elegant.
I think the best way to do this is to roll your own ApplicationContext. This allows you full control over the application lifetime without having it being tied to a specific Window. See http://msdn.microsoft.com/en-us/library/ms157901.aspx for more information.
Here's an example:
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyApplicationContext());
}
}
public class MyApplicationContext : ApplicationContext
{
public MyApplicationContext()
{
ShowForm1();
}
public void ShowForm1()
{
if (_form2 != null)
_form2.Hide();
if (_form1 == null)
{
_form1 = new Form1(this);
_form1.FormClosed += OnFormClosed;
}
_form1.Show();
MainForm = _form1;
}
public void ShowForm2()
{
if (_form1 != null)
_form1.Hide();
if (_form2 == null)
{
_form2 = new Form2(this);
_form2.FormClosed += OnFormClosed;
}
_form2.Show();
MainForm = _form2;
}
private void OnFormClosed(object sender, FormClosedEventArgs e)
{
if (_form1 != null)
{
_form1.Dispose();
_form1 = null;
}
if (_form2 != null)
{
_form2.Dispose();
_form2 = null;
}
ExitThread();
}
private Form1 _form1;
private Form2 _form2;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Form1(MyApplicationContext context)
: this()
{
_context = context;
}
private void button1_Click(object sender, EventArgs e)
{
if (_context != null)
_context.ShowForm2();
}
private readonly MyApplicationContext _context;
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(MyApplicationContext context)
: this()
{
_context = context;
}
private void button1_Click(object sender, EventArgs e)
{
if (_context != null)
_context.ShowForm1();
}
private readonly MyApplicationContext _context;
}
So we'll start out by going to the child form and creating a new event that can be used to notify the parent when it wants to change forms:
public event Action ChangeForm;
Then we fire the event and hide the child form when it wants to change forms:
private void ChangeForm_Click(object sender, EventArgs e)
{
Hide();
if (ChangeForm != null)
ChangeForm();
}
The parent form needs an instance of the child form as an instance field:
private subForm child = new subForm();
And it needs to initialize it in it's constructor, both adding handlers to the ChangeForm event to show the parent, and to the closed event to close itself:
public Form1()
{
InitializeComponent();
child.ChangeForm += () => Show();
child.FormClosed += (s, args) => Close();
}
Then all that's left is for the parent form to hide itself and show the child when it wants to change forms:
private void ChangeForm_Click(object sender, EventArgs e)
{
Hide();
child.Show();
}
Why not simply setting them to foreground, topmost, and so on ?
And setting them back vice versa ?
---added as comment as proposed
To access MainForm fromsubForm:
Create a constructor in your subForm and a field:
MainForm MainFormRef_Field;
subForm(MainForm MainFormRef)
{
this.MainFormRef_Field = MainFormRef;
}
Now you can access your MainForm using this reference. Like this:
MainFormRef_Field.Show();
MainFormRef_Field.Hide(); //or how ever you want to handle it
To access subForm fromMainForm:
To handle your subForm use the object you created for it. Here:
subForm myNewForm = new subForm();
To close whole application if any of the form closes:
Set a Form_Closing event for both forms:
private void MainForm_Closing(object sender, EventArgs e)
{
Application.Exit();
}
private void subForm_Closing(object sender, EventArgs e)
{
Application.Exit();
}
Note:
I am not writing the whole code for all of your cases. Set the variables, check the conditions. Its all up to you that how code it. All the main points you needed I've provided you the solution of them.