How to Set the focus on a keyup event in c#? [duplicate] - c#

My app is WinForms .NET 4 (C#) and one of the forms keeps on closing automatically after pressing a button.
The form DOES have default Accept and Cancel buttons but these are not touched.
There is a ButtonTestConnection_Click event which when clicked, does its job but closes the form somehow.
I am using the mouse to click the button so this is NOT a case of cascading keystrokes.
I am NOT setting the DialogResult in this function.
I also tried to check for stray this.Close / this.Dispose calls but found none.
Here is the code:
private void ButtonTestConnection_Click (object sender, System.EventArgs e)
{
this.Enabled = false;
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
this.ProgressBar.Minimum = 0;
this.ProgressBar.Maximum = 500;
this.ProgressBar.Value = 0;
this.ProgressBar.Visible = true;
this.ButtonTestConnection.Visible = false;
try
{
while (this.ProgressBar.Value < this.ProgressBar.Maximum)
{
// Some proxy code.
this.ProgressBar.Value++;
}
}
catch
{
}
this.ProgressBar.Visible = false;
this.ButtonTestConnection.Visible = true;
this.ProgressBar.Invalidate();
System.Windows.Forms.Application.DoEvents();
System.Threading.Thread.Sleep(10);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.Enabled = true;
System.Windows.Forms.MessageBox.Show(result.ToString());
}

Check if the property DialogResult on the button equals to None.
If not, then the form will be closed when you hit that button and the form will return the setting of the Button's DialogResult property.
Usually, this happens a lot when you copy/paste an existing form's button but forget to remove on the pasted button the original DialogResult setting

Related

Panel Control and Check panel property is Empty

I want to Check that whether a panel have some form or not.
kindly tell me the best condition.
if panel have some form inside it than bool variable should be true.
the code that i have write is below.Kindly answer this question as soon as possible. thanks.
this is a button click event.
i want to check that when i click on a button its first check these conditions.
1. check if form have existing this form that i want to pass in it than break
it and make bool variable true.
2. if it does't have than clear all form inside the panel and generate click event form inside it.
enter code here
private void btn_dashboard_Click(object sender, EventArgs e)
{
bool isOpen = false;
dash = new Dashboard();
if (panel_window_data.Controls.Equals(dash))
{
isOpen = true;
}
if (isOpen == false)
{
panel_window_data.Controls.Clear();
dash.TopLevel = false;
panel_window_data.Controls.Add(dash);
dash.Height = panel_window_data.Height;
dash.Width = panel_window_data.Width;
dash.Show();
}
}
I'm using this code in every single btn, and actually works.
/*------------------------------------------------------------------*/
// to show other form inside panel in main form
users objuser = new users();
pnlobjform.Controls.Clear();
objuser.TopLevel = false;
pnlobjform.Controls.Add(objuser);
objuser.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objuser.Dock = DockStyle.Fill;
objuser.Show();
/*---------------------------------------------------------------*/

Enabling Button in Messagebox with timer

I have a message box that pops up when a user click a button. when user click yes it's run an insert function.
what i want is to add or start a count down when a messagebox pop up, the default yes button was disabled. and after 5 second the yes button, become enable and ready to click by user.
if (MessageBox.Show("log", "test", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
insert();
}
As suggested in the comment, you need to have your own implementation for this functionality. Below is partial code that you will need to modify normal form to make it appear like dialogue box:
Add new Form to your project. Open the porperties tab. Set properties as give below in point 2.
Modify form in designer to change following properties to given values:
this.AcceptButton = this.btnYes;//To simulate clicking *ENTER* (Yes)
this.CancelButton = this.button2; //to close form on *ESCAPE* button
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
//FROM CODEPROJECT ARTICLE LINK
this.ShowInTaskBar = false;
this.StartPosition = CenterScreen;
Add a timer to form. Set its interval to 5000 (5 seconds). Write code to start timer on Shown event of form:
private void DialogBox_Shown(object sender, EventArgs e)
{
timer1.Start();
}
Handle ticking of Timer:
public DialogBox()
{
InitializeComponent();
//bind Handler to tick event. You can double click in
//properrties>events tab in designer
timer1.Tick += Timer1_Tick;
}
private void Timer1_Tick(object sender, EventArgs e)
{
btnYes.Enabled = true;
timer1.Stop();
}
Set Yes button handler:
private void btnYes_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Yes;
}
From where you are showing this custom message box, you can check if Yes or No is clicked as follows:
var d=new DialogBox();
var result=d.ShowDialog();
if(result==DialogResult.Yes)
//here you go....

WPF Detecting Window Close Event and coupling with a button event

For two days, I have tried various methods for making it so that on loadButton click, it opens a secondary window and disables the loadButton; Then, when that secondary window has been closed, the loadButton will be re-enabled. Although, obviously all of my attempts have been unsucessful.
I have been reading about using the isClosing event, although, I haven't figured out how to properly implement it. So I decided to go with this route.
private void loadButton_Click(object sender, RoutedEventArgs e)
{
var richWindow = RichTextWindow.GetWindow(new RichTextWindow());
if (richWindow.IsActive != true)
{
loadButton.IsEnabled = false;
richWindow.Show();
}
else
{
loadButton.IsEnabled = true;
}
}
Issue here is, the first half is executed. Once I click the loadButton, it does disable. However, on closing the new Window, the loadButton is still disabled.
Could anyone point me in the right direction on where I need to go with this?
I think what you want is something like this:
private void loadButton_Click(object sender, RoutedEventArgs e)
{
var richWindow = RichTextWindow.GetWindow(new RichTextWindow());
richWindow.Closed += (s, e) => loadButton.IsEnabled = true;
loadButton.IsEnabled = false;
richWindow.Show();
}
Basically, disable the button before opening the window. Then, listen for the window to close and enable the button again.
richWindow.Loaded +=
{
loadedButton.IsEnabled = false;
};
richWindow.Closing +=
{
loadedButton.IsEnabled = true;
}

C# groupbox visibility in windows form

I Googled around but seems my problem happens when two gropboxes are overlapping, in my case they are not overlapping!
Problem is that the Visible property of groupbox doesn't work. what am I trying to do is that groupbox1 is visible when program starts and groupbox2 is not, by clicking on a button it should goes invisible and groupbox2 should appear, clicking the same button this action should be done vice versa.
here is my code:
private void button2_Click(object sender, EventArgs e)
{
if (groupBox2.Visible == false)
{
groupBox1.Visible = false;
groupBox2.Visible = true;
}
if (groupBox1.Visible == false)
{
groupBox1.Visible = true;
groupBox2.Visible = false;
}
}
Your problem is that after the first if-statement, it immediately checks if groupBox1.Visible is false, which it always will be. It then proceeds to flip it back.
Change the if to an else, or at least and else if and your code will work.

Preventing a dialog from closing in the button's click event handler

I have a dialog that I show with <class>.ShowDialog(). It has an OK button and a Cancel button; the OK button also has an event handler.
I want to do some input validation in the event handler and, if it fails, notify the user with a message box and prevent the dialog from closing. I don't know how to do the last part (preventing the close).
You can cancel closing by setting the Form's DialogResult to DialogResult.None.
An example where button1 is the AcceptButton:
private void button1_Click(object sender, EventArgs e) {
if (!validate())
this.DialogResult = DialogResult.None;
}
When the user clicks button1 and the validate method returns false, the form will not be closed.
Given that you've specified you want a pop error dialog, one way of doing this is to move your validation into a OnClosing event handler. In this example the form close is a aborted if the user answers yes to the question in the dialog.
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Determine if text has changed in the textbox by comparing to original text.
if (textBox1.Text != strMyOriginalText)
{
// Display a MsgBox asking the user to save changes or abort.
if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Cancel the Closing event from closing the form.
e.Cancel = true;
// Call method to save file...
}
}
}
By setting e.Cancel = true you will prevent the form from closing.
However, it would be a better design/user experience to display the validation errors inline (via highlighting the offending fields in some way, displaying tooltips, etc.) and prevent the user from selecting the OK button in the first place.
Don't use the FormClosing event for this, you'll want to allow the user to dismiss the dialog with either Cancel or clicking the X. Simply implement the OK button's Click event handler and don't close until you are happy:
private void btnOk_Click(object sender, EventArgs e) {
if (ValidateControls())
this.DialogResult = DialogResult.OK;
}
Where "ValidateControls" is your validation logic. Return false if there's something wrong.
You can catch FormClosing an there force the form to remain opened.
use the Cancel property of the event argument object for that.
e.Cancel = true;
and it should stop your form from closing.
This doesn't directly answer your question (other already have), but from a usability point of view, I would prefer the offending button be disabled while the input is not valid.
Use this code:
private void btnOk_Click(object sender, EventArgs e) {
if (ValidateControls())
this.DialogResult = DialogResult.OK;
}
The problem of it is that the user has to clic two times the buttons for closing the forms;
Just add one line in the event function
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
this->DialogResult = System::Windows::Forms::DialogResult::None;
}
I wish I had time to find a better example, but you would be much better off using the existing windows forms validation techniques to do this.
http://msdn.microsoft.com/en-us/library/ms229603.aspx
void SaveInfo()
{
blnCanCloseForm = false;
Vosol[] vs = getAdd2DBVosol();
if (DGError.RowCount > 0)
return;
Thread myThread = new Thread(() =>
{
this.Invoke((MethodInvoker)delegate {
picLoad.Visible = true;
lblProcces.Text = "Saving ...";
});
int intError = setAdd2DBVsosol(vs);
Action action = (() =>
{
if (intError > 0)
{
objVosolError = objVosolError.Where(c => c != null).ToArray();
DGError.DataSource = objVosolError;// dtErrorDup.DefaultView;
DGError.Refresh();
DGError.Show();
lblMSG.Text = "Check Errors...";
}
else
{
MessageBox.Show("Saved All Records...");
blnCanCloseForm = true;
this.DialogResult = DialogResult.OK;
this.Close();
}
});
this.Invoke((MethodInvoker)delegate {
picLoad.Visible = false;
lblProcces.Text = "";
});
this.BeginInvoke(action);
});
myThread.Start();
}
void frmExcellImportInfo_FormClosing(object s, FormClosingEventArgs e)
{
if (!blnCanCloseForm)
e.Cancel = true;
}
You can probably check the form before the users hits the OK button. If that's not an option, then open a message box saying something is wrong and re-open the form with the previous state.

Categories

Resources