I've researched thoroughly but still i cant find the best solution for this..
I have a 3 buttons, BACK - HOME - FORWARD ..
This is just like the buttons on the upper left of browser .. and im trying to do this on a form..
what i have is this ..
the back button code is
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
}
// simply hiding the form .. so that the previous form will be shown..
the home button code is this..
private void button2_Click(object sender, EventArgs e)
{
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
if (Application.OpenForms[i].Name != "HomePage")
Application.OpenForms[i].Close();
}
}
// this will show the HomePage form and close other forms whos name is not "HomePage"
the Problem is when i press the Back Button , im hiding it .. how can a button will re open a previously closed or hided form ?
I hope you can Help me! Thanks!
private void button3_Click(object sender, EventArgs e)
{
???????
}
You would need to store a reference to the form that you wish to open again.
There are a couple of options to do this, but if you simply wanted to allow the user to go "Forward" once, you could just store a reference to the form like so:
internal class MyHistory {
internal static Form LastForm;
}
// ........
private void button1_Click(object sender, EventArgs e)
{
MyHistory.LastForm = this;
this.Hide();
}
// ........
private void button3_Click(object sender, EventArgs e)
{
MyHistory.LastForm.Show();
}
Of course, you could maintain a full stack of history items and traverse back/forwards through them if you wanted to be more comprehensive than this.
Note that, if you .Close() your form, you won't be able to reopen it as the reference will be disposed of once it is closed. This method would only work if you were to .Hide() it, which keeps the form instance valid, just hides the form from the user's view.
you could use a form list which holds all initialized forms. that way you can hide, show, add and remove forms dynamically.
List<Form> lstForms = new List<Form>();
then when you add a form:
Form newForm = new Form();
lstForms.Add(newForm);
Hiding a Form:
lstForms(x).Hide(); //x = index of Form you want to hide
Showing a Form
lstForms(x).Show(); //x = index of Form you want to hide
Removing a Form (when closing it for example)
lstForms.RemoveAt(x);
that way you can dynamically work with forms and it is much easier to keep an overview if you have many forms...
Related
So, I'm making a payroll management system as a hobby project to help my resume and general knowledge of c#. So, I'm making a UI and I can open a new window just fine with this code:
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct();
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.Show();
this.Hide();
}
however, I don't know the event to check when the little red "x" button is clicked, because when that button is clicked, I want to go back to the main screen because I hide the main screen when that button is clicked, and when i click the red "x" on the screen that just opened, it closes, but the application continues to run in the background.
If there is some better way to manage multiple menus, I'm open to suggestions, however, this is what I've found easiest.
Thanks in advance
I second Robert Harvey's suggestion; this gives the user the reassurance tha tht emain window is still open/ nothing got lost, but it's unreachably "behind" the CreateAdminAcct form while the CreateAdminAcct form is open
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct();
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.ShowDialog();
//do any code here that needs to access createAcct before it's lost
MessageBox.Show(createAcct.NewAdmin.Name);
}
If you really do want to hide your main form, pass the main form itself to createAcct, and make it createAcct's job to re-open the main form when it is closing
private void button1_Click(object sender, EventArgs e)
{
CreateAdminAcct createAcct = new CreateAdminAcct(this); //note passing this form to constructor
createAcct.StartPosition = FormStartPosition.CenterScreen;
createAcct.Show();
}
class CreateAcctForm : Form{
private Form _showWhenClosing;
CreateAcctForm(Form revertTo){
InitializeComponent();
_showWhenClosing = revertTo;
}
}
void Form_Closing(object sender, ...){ //event
_showWhenClosing.Show();
}
Side note: please rename your controls after you drop them ona form. code that's stuffed with label57, textbox25 is effectively obfuscated and really wearisome to follow
Hi I'm relatively new to C# and completely new to windows form and basically trying to create a subliminal messaging program that at timed intervals will quickly display a message for it to then be hidden again.
I've managed to by looking through various other posts created another form that will pop up and then hide very quickly using
msgObject.Activate();
that brings the form to the front. However it is stopping me from being able to type when I'm working and I basically wanting to know if it is possible to make some kind of message or form appear at the front of all my other programs without it interrupting my current work or opening or closing of other windows and tasks if that makes sense. As currently it brings the form to the front of everything but will also stop me from being able to type etc.
I'm not sure if this is possible with my current method of using a form but if there is a way of achieving the result I'm after I'd be very grateful to find out
Thanks.
Here is more of my code to clarify
public partial class FormHomePage : Form
{
private bool startSubliminal = false;
msg msgObject = new msg();
List<string> subliminalMessages = new List<string>();
public FormHomePage()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (startSubliminal)
{
msgObject.Show();
msgObject.BringToFront();
msgObject.Activate();
}
}
private void button1_Click(object sender, EventArgs e)
{
subliminalMessages.Add(txtBox1.Text);
msgObject.LabelText = txtBox1.Text;
txtBox1.Text = string.Empty;
startSubliminal = true;
msgObject.Show();
msgObject.BringToFront();
}
private void timer2_Tick(object sender, EventArgs e)
{
msgObject.Hide();
}
}
How are you showing the second form (the message form) in the first place? You're probably using .Show() (right?), which will make the form "steal" the focus anyway. The same applies to .Activate() and .BringToFront().
Instead, what you can do is to show the message form and make sure it stays on top the current form, and then activate the current/main form once again.
Something like this should work:
var frm = new YourMsgForm();
frm.Show(this);
this.Activate();
Here's a demonstration:
Note that I used .Show(this) instead of .Show(), that's in order to set the current form as the Owner of the new one, that way we guarantee that the new form will stay on top of the current one. This is equivalent to calling frm.Owner = this; then frm.Show();.
Another way to make sure the form stays on top is by setting the TopMost property instead of the Owner property. However, doing so will make the new form on top of the other windows as well (not just your application).
well I have a funny problem with closing dialog forms.
here is the problem:
I run application and open second form (through menu strip) as showdialog(); and then from second form open third form. When I open third form by button 1 and then close it everything is alright, but when I open the third form by button 2 and then close it, third form will be closed and then it closes the second form also. !!! In second form when I show a messageBox and close it also the second form will be closed.
here is my codes:
open second form from first form codes:
private void settingsToolMenu_Click(object sender, EventArgs e)
{
settingsForm s1 = new settingsForm(this);
s1.ShowDialog();
}
open third form from second by button 1 form codes:
private void addReportButton_Click(object sender, EventArgs e)
{
addReport a1 = new addReport(this);
a1.ShowDialog();
}
open third form from second by button 2 form codes:
private void editReportButton_Click(object sender, EventArgs e)
{
addReport a2 = new addReport(this);
a2.ShowDialog();
}
as you see there is no differences between button 1 and button 2
here is a video from application running.
Not sure what's happening out there, but there should be .Show() method, which runs a window in a different way including closing strategy. Try it out.
Try This
Instead of
addReport a2 = new addReport(this);
a2.ShowDialog();
Use
addReport a2 = new addReport();
a2.ShowDialog(this);
Then on click of Exit / Close button of dialog window
private void BtnExit_Click(object sender, EventArgs e)
{
this.Dispose();
}
Hope this will solve your issue.
I used this code and it worked. I have 3 forms, the first form is opened when running the app, the second form is opened with a button (can be menustrip, doesn't matter), then the third is opened like that too, after closing the third form the second form remains open.
FormN fm = new FormN();
fm.ShowDialog();
Use that piece of code in every method that is called from clicking on a button and it should work fine. Just change the "FormN" for whatever your forms are named. Also, if you need to pass any form's attributes into the next form you can do this:
Code at first form:
public string mytext; //Variable I want to use later, in Form2.
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
mytext = tb1.Text;
Form2 fm = new Form2(this);
fm.ShowDialog();
}
Notice how I save "tb1"'s (TextBox1) value in a variable before calling "fm.ShowDialog();", so I can use the TextBox1 value later inside the Form2.
Code at second form, having main form's variables (such as "mytext" value).
Form1 mfm;
public Form2(Form1 mainfm)
{
InitializeComponent();
mfm = mainfm;
}
public void button2_Click(object sender, EventArgs e)
{
//In this method I use the variable "mytext" wich is a Form1 attribute.
//You can see how I declare it in the first form's code (see above).
textBox1.Text = mfm.mytext;
}
With this you have created an object of your main form ("Form1 mfm;") with all the variables it contained before calling the second form, which can be used for the third form too.
in second form formClosing() event i wrote these codes:
private void settingsForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(e.CloseReason != CloseReason.UserClosing)
{
e.Cancel = true;
}
}
and nothing can close the second form except user!
I have a combo box in my C# which is place in form named frmMain which is automatically fill when I add (using button button1_Click) a product in my settings form named frmSettings. When I click the button button1_Click I want to reload the frmMain for the new added product will be visible.
I tried using
frmMain main = new frmMain();
main.Close();
main.Show();
I know this code is so funny but it didn't work. :D
This is windows form!
EDIT
Please see this image of my program for better understanding.
This is my frmMain
Here is what my settings frmSettings form look like. So, as you can see when I click the submit button I want to make the frmMain to reload so that the updated value which I added to the settings will be visible to frmMain comboBox.
Update: Since you changed your question here is the updated version to update your products
This is your products form:
private frmMain main;
public frmSettings(frmMain mainForm)
{
main = mainForm;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
main.AddProduct(textBox1.Text);
}
It will need the mainform in the constructor to pass the data to it.
And the main form:
private frmSettings settings;
private List<string> products = new List<string>();
public frmMain()
{
InitializeComponent();
//load products from somewhere
}
private void button1_Click(object sender, EventArgs e)
{
if (settings == null)
{
settings = new frmSettings(this);
}
settings.Show();
}
private void UpdateForm()
{
comboBoxProducts.Items.Clear();
comboBoxProducts.Items.AddRange(products.ToArray());
//Other updates
}
public void AddProduct(string product)
{
products.Add(product);
UpdateForm();
}
You then can call UpdateForm() from everywhere on you form, another button for example.
This example uses just a local variable to store your products. There are also missing certain checks for adding a product, but I guess you get the idea...
this.Close();
frmMain main = new frmMain();
main.Show();
There is no such built in method to set all your values as you desire. As i mentioned in the comment that you should create a method with your required settings of all controls, here is the sample code:
private void ReloadForm()
{
comboBox.ResetText();
dataGridView.Update();
//and how many controls or settings you want, just add them here
}
private void button1_Click(object sender, EventArgs e)
{
ReloadForm(); //and call that method on your button click
}
Try out this code.
this.Refresh();
Application.Doevents();
this.Refresh();
Refresh();
this.Hide();
frmScholars ss = new frmScholars();
ss.Show();
you want to Invalidate the form
http://msdn.microsoft.com/en-us/library/598t492a.aspx
IF you are looking to refresh page from usercontrol .Here is expample where i amrefreshing form from usercontrol
Find the form in which this reload button is.
Then Call invalidiate tab control and refresh it.
Dim myForm As Form = btnAuthorise.FindForm()
For Each c As Control In myForm.Controls
If c.Name = "tabControlName" Then
DirectCast(c, System.Windows.Forms.TabControl).Invalidate()
DirectCast(c, System.Windows.Forms.TabControl).Refresh() 'force the call to the drawitem event
End If
Next
Not required reload for entire form. Just create a function for form initialise.
you can call this function any time. This will refresh the form.
private void acc_Load(object sender, EventArgs e)
{
form_Load();
}
public void form_Load()
{
// write form initialise codes example listView1.Clear();...
}
private void button1_Click(object sender, EventArgs e) //edit account
{
//Do something then refresh form
form_Load();
}
If you want to automatically update the value of the other form when you click the button from another one you can use timer control. Just set the timer to 0.5s in order to update the form fast
I think that, by calling the frmMain_load(sender,e) when you are clicking the button should reload the form.
You may also try to Invalidate() the form just like #Nahum said.
I'im writing a program that works with 2 forms, the main form and the form where the configuration is made, so when the user clicks toolstripmenu->Preferences the Preferences form is showned and I want it to make the user only capable of having one Preferences form at a time.
When I use:
Prefs preferencias = new Prefs;
private void preferenciasToolStripMenuItem_Click(object sender, EventArgs e)
{
preferencias.Show();
}
It works, but when I close the Preferences form and try to open a new one the program crashes.
And When I use:
private void preferenciasToolStripMenuItem_Click(object sender, EventArgs e)
{
Prefs preferencias = new Prefs;
preferencias.Show();
}
The user can hav multiple Preferences form.
What can I do?
Thanks in advance.
It sounds like you want a modal dialog, so you need to use the ShowDialog( ) method instead of Show( ):
private void preferenciasToolStripMenuItem_Click(object sender, EventArgs e)
{
preferencias.ShowDialog();
}
The ShowDialog() that others have suggested is a good answer. If you're interested in an alternative, here's something I sometimes do:
private void FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
}
}
What this does is simply hides the form so that if you show it again, its already loaded into memory. Additionally, if you have a timer or some other thread in that form running, it can still run and do its thing.
preferencias.ShowDialog()
will only allow one preference window to be open .
You can use the Application.OpenForms property in your menu item's click event to check if a form of that type is already open. If there is no form of that type open, then you can open your instance. If there is, it simply won't show.
foreach (Form form in Application.OpenForms) {
if (form.GetType() != typeof(PreferencesForm)) {
new PreferencesForm().Show();
}
}
Or as already stated you can call PreferencesForm.ShowDialog() to make the form modal, in which case the user has to close the form before they can even interact with the main form again.
The method you use depends on if you want the user to be able to use the main form even if the preferences form is open.
If you're looking for 1 and ONLY 1, You probably want to implement the Singleton pattern for the Prefs class.