I just face a new problem
I have one form which there is a button in it and when user click on this button another form will be appear and open.
I set an events that when user double click on the second form the form will be disabled and user can't do anything when form become disabled.
But i want to set a way that user can make the second form enable again.
I tried some events that when user press enter on the second form the second form become enable again and this is my code :
f3.Enabled = true;
But in fact when i press enter after disable the form nothing happen at all.
I tried some another way something like when user pressDown a key the second form become enable and this is my code :
f3.Enabled = true;
But something is make me angry that when the second form is open i cant do anything with the first form and i have to close the second form first.
But cause of disabled i can't close the second form.
what is your advice ?
what events can i add or what code should i put in my program to make this way to enable and disable the second form so easily ?
thanks in advance for your advice .
Update
Would you please tell me how can i put some controls in one grid and disable them ?
For example i don't want that user make any changes except close the form by clicking on 'x' button.
Update
This is my Form number 3
As you see i have 2 buttons and 3 labels which i didn't put code in labels.
but i wanna when user click on start all controls in form become disabled (especially click on form : it means that user can't click on the form like when its disabled) except Exit button. And above the form 'x' Button and minimized and maximized button become enable.
Update
I did this code in Form 2:
public partial class Form2 : Form{
public Form2()
{
InitializeComponent();
}
private void Form2_DoubleClick(object sender, EventArgs e)
{
this.Enabled = false;
}
private void Form2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.Enabled = true;
}
}
}
And already a code that you wrote there.
But i have that problem yet.
I put a picture that maybe can help ya.
maybe can help ya this image
Update
I tried KeyPreview in my form and changed it into true but i have that problem yet.
Any advice ?
It means that there is no code for my idea ?!?
.Update
I tried so many code for this question and couldn't find my answer yet...
let me describe better that what is my problem
I have 2 forms and i wanna when user click on the second form the form become non clickable and user can't do anything when user click on that button except minimize and maximize and exit from the form.
I found some codes but they didn't help me...
Maybe you can :
this is my code in the button :
private void btnStart_Click(object sender, EventArgs e)
{
f3.Visible = false;
}
and i also tried this code :
private void btnStart_Click(object sender, EventArgs e)
{
this.Enabled = false;
}
And also set keypreview for this form but when i click on the button the form become Disable and i cant do anything except close the program from taskbar.
any advice ?
You can do it something like this for forms:
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog();
}
}
Form2:
// on form, set form property KeyPreview to true
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_DoubleClick(object sender, EventArgs e)
{
this.Enabled = false;
}
private void Form2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.Enabled = true;
}
}
}
If you want to do with grid view or something, you can enable/disable grid in the same way.
Please let me know if you have further questions.
I hope it will help you.. !
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
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'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...
This question is related to this my question. Now I have form in my class and when user click on button I show (or hide) form. That´s ok. But I want to hide form when I move with origin form or when I click somewhere in origin form. The new form is behind that origin form. I was trying events like lostfocus and others but It didn´t help. So I think I need some trick that check from my control if there was click in parrent form (origin form) or some other hack. I know the best would be that I put code but I have many lines so I think that best way will be if you help me in general way and then I try to applicate to my app.
You can do it with a global mouse and keyboard hook. In fact, its been wrapped up into well documented, well structured .NET API over at CodePlex
Go over there and download it. Then, set up a global mouse hook:
_mouseListener = new MouseHookListener(new GlobalHooker());
_mouseListener.MouseMove += HandleGlobalHookMouseMove;
_mouseListener.Start();
The key here is that you will receive the MouseMove event ANY time the mouse moves ANYWHERE on the desktop, not just within the bounds of your window.
private void HandleAppHookMouseMove(object sender, MouseEventArgs e)
{
if (this.Bounds.Contains(e.Location))
{
HandleEnter();
}
else
{
HandleLeave();
}
}
You can also setup one for MouseClick. The combination of the two will enable you to determine any time the mouse moves over your origin form, or the mouse is clicked when its over it. Unlike the LostFocus and other events you tried, focus is irrelevant.
Does below help?
public partial class Form1 : Form
{
Form f2 = new Form2();
public Form1()
{
InitializeComponent();
f2.Show();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (this.ClientRectangle.Contains(e.Location) && f2.Visible) { f2.Hide(); }
}
private void button1_Click(object sender, EventArgs e)
{
f2.Visible = !f2.Visible ? true : false;
}
}