I implement a form for handle excel file when click button "Start".
Event click Start button:
private void btnImport_Click(object sender, EventArgs e)
{
showFormSelectLanguage();
if (CheckSheetFile() == true) {
using (WaitingForm frm = new WaitingForm(handleExcel))
{
frm.ShowDialog(this);
}
var dialogMessage = new DialogMessage();
dialogMessage.ShowDialog(this);
} else
{
ShowDialogNotFoundSheet();
}
}
showFormSelectLanguage method display dialog for select language:
private void showFormSelectLanguage()
{
var formSelectLanguage = new FormSelectLanguage();
formSelectLanguage.ShowDialog(this);
}
ShowDialogNotFoundSheet function for check sheet excel exist:
private void ShowDialogNotFoundSheet()
{
var dialogNotFoundSheet = new DialogNotFoundSheet();
dialogNotFoundSheet.setTextContent("Not found sheet");
dialogNotFoundSheet.ShowDialog(this);
}
Event click confirm select language button at Select language form:
private void btnConfirmLanguage_Click(object sender, EventArgs e)
{
//close dialog
this.Close();
}
Event click Close button for close DialogNotFoundSheet form:
private void btnCloseDialogNotFoundSheet_Click(object sender, EventArgs e)
{
this.Close();
}
CheckSheetFile method:
private bool CheckSheetFile()
{
var isCorrectFile = false;
try
{
xlWorkBook = xlApp.Workbooks.Open(txtFilePath.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);
var xlWorkBook1 = xlWorkBook.Sheets["SheetName"];
isCorrectFile = true;
}
catch (Exception e)
{
return false;
}
return isCorrectFile;
}
Issue:
When I click Close button at DialogNotFoundSheet form. Then FormSelectLanguage from still display. It repeats. How can resolve it?
Expected 2 forms can close
Thanks!
Update:
All References btnImport_Click:
UI:
I don't exactly know what you did with btnImport_Click, but if your purpose is to disable the function of a button at a time and to enable it at another time, actually you don't have to register or unregister the click event, you can simply set button's Enabled propety.
//btnImport.Click += btnImport_Click;
btnImport.Enabled = true;
//btnImport.Click -= btnImport_Click;
btnImport.Enabled = false;
My guess of the reason of this loop is that you have called += btnImport_Click many times, but -= btnImport_Click is never (or less) run.
For instance if you do:
btnImport.Click += btnImport_Click;
btnImport.Click += btnImport_Click;
Each time btnImport is clicked, btnImport_Click will get invoked twice.
Related
I have a WinForm popping up within a WinForm. If I close the inner Winform and reopen it, the text in the textbox doesn't highlight. However if I close both WinForms and reopen the first and then the inner WinForm, the text in the textbox will highlight.
How can I get the inner WinForm text to highlight every time?
I have tried the 2 ways I know of / most common ways of highlighting the text but they seem to only work the first time around.
Any and all help/direction is appreciated.
The inner winforms is being called from a .ShowDialog(); in the outer Winforms.
portalTitleEntryForm.ShowDialog();
Here is my code of the inner Winform and what I have tried:
public partial class PortalTitleEntryForm : Form
{
public string portalEntryTitle;
public string dialogResult;
public PortalTitleEntryForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
titleTextBox.TabIndex = 0;
continueButton.TabIndex = 1;
cancelButton.TabIndex = 2;
this.CancelButton = cancelButton;
this.AcceptButton = continueButton;
titleTextBox.Focus();
if (portalEntryTitle != null || !portalEntryTitle.Equals(""))
{
titleTextBox.Focus();
titleTextBox.Text = portalEntryTitle;
/*titleTextBox.SelectionStart = 0; // doesn't work the second time
titleTextBox.SelectionLength = titleTextBox.Text.Length;*/ // doesn't work the second time
titleTextBox.SelectAll(); // doesn't work the second time
bool focusStatus = titleTextBox.Focused; // bool equals false the second
//titleTextBox.Focus(); // doesn't work the second time around
}
private void continueButton_Click(object sender, EventArgs e)
{
if (!titleTextBox.Text.Equals(""))
{
portalEntryTitle = titleTextBox.Text;
dialogResult = DialogResult.OK.ToString();
continueButton.DialogResult = DialogResult.OK;
Close();
return;
}
else if (titleTextBox.Text.Equals(""))
{
MessageBox.Show("Please give your entry a title.", "M3dida", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
dialogResult = DialogResult.Cancel.ToString();
Debug.WriteLine("Cancel button was clicked");
Close();
return;
}
}
}
Form loads only once, unless it is destroyed.
You might want to handle Form.Activated event and invoke titleTextBox.Focus() in the handler.
Within my application i am trying to use forms in tabpages. These forms are all data managing forms.
I edited the tabcontrol (created a custom tabcontrol) so i can remove tabpages by double clicking on the tabheader.
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
//* Default method of closing a tab.
if (Selectedtab != null)
TabPages.Remove(Selectedtab);
}
Although it works. Its actually not doing what I need.
Because any changes on the form in the tabpage, are lost, regardless of the below
public partial class SomeForm : Form
{
private void SomeForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (HasChanges() && CustomMessage.WarningBox("There is unsaved data. Are you sure you want to close"))
return;
((TabControl)((TabPage)this.Parent).Parent).TabPages.Remove((TabPage)this.Parent);
}
}
When setting a breakpoint at this function, it never enters.
Now my question: Is it possible to call the Close method of the form from Tabcontrol. Preferably something like below.
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
if (Selectedtab != null)
{
if (Selectedtab.EmbeddedForm != null)
TabPages.ASelectedtab.EmbeddedForm.Close();
}
}
The main problem I am facing is I don't know how to access a function on a form from only knowing the selected tab. And I cant find it either.
Solution after using KyleWangs answer as base:
Custom Control:
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
base.OnMouseDoubleClick(e);
string frmSearchName = "Frm" + SelectedTab.Name.Substring(3);
Form f = (Form)Application.OpenForms[frmSearchName];
if (f != null)
f.Close();
else
TabPages.Remove(SelectedTab);
}
Form:
private void SomeForm_FormClosing(object sender, FormClosingEventArgs e)
{
SetChanges();
if (HasChanges() && !CustomMessage.WarningBox("There is unsaved data. Are you sure you want to close?"))
e.Cancel = true;
else
((TabControl)((TabPage)this.Parent).Parent).TabPages.Remove((TabPage)this.Parent);
}
You can use property Application.OpenForms to get the open form instance.
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.TabPages.Clear();
PageForm1 f1 = new PageForm1();
AddNewTab(f1);
}
private void AddNewTab(Form frm)
{
TabPage tab = new TabPage(frm.Text);
frm.TopLevel = false;
frm.Parent = tab;
frm.Visible = true;
tabControl1.TabPages.Add(tab);
frm.Location = new Point((tab.Width - frm.Width) / 2, (tab.Height - frm.Height) / 2);
tabControl1.SelectedTab = tab;
}
private void tabControl1_DoubleClick(object sender, EventArgs e)
{
Form f = (Form)Application.OpenForms[tabControl1.SelectedTab.Text];
f.Close();
tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
}
Can I determine if the click call was successfully? I'm debuggin an application that click on button (see below code) sometimes it seems fail to do so, I'd liek to determine whatever the click was successfully.
HtmlElement button = ...;
IHTMLElement nativeElement = button.DomElement as IHTMLElement;
nativeElement.click();
Have you considered adding an event handler for the click event for that particular HTMLElement? You could do something along these lines :
bool shouldfire = false;
bool didFire = false;
private void yourMethod()
{
HtmlElement button = ...; //however you are getting this element
button.Click +=button_Click;
IHTMLElement nativeElement = button.DomElement as IHTMLElement;
nativeElement.click();
shouldfire = true;
}
private void button_Click(object sender, HtmlElementEventArgs e)
{
didFire = true;
}
private void yourOtherMethod()
{
if (shouldfire != didFire)
{
//do something
}
}
On button click, I open a new form (lets say Form2), but I don't want that Form2 to open more than 1time. And I don't want to use .ShowDialog(), because it wont allow me to go to the Previous Form. How can I do that ?
You can use Application.OpenForms property to check if form already opened:
if (!Application.OpenForms.OfType<Form2>().Any())
{
Form2 form2 = new Form2();
form2.Show();
}
You can show existing form instead of creating new:
Form2 _form2 = null;
void Button1_Click(object sender, EventArgs e)
{
if (_form2 == null)
{
_form2 = new Form2();
_form2.Closed += Form2_Closed;
}
_form2.Show();
_form2.BringToFront();
}
private void Form2_Closed(object sender, System.EventArgs e)
{
_form2 = null;
}
You can issue the Show method, which will show the form and allow the users to get back to the form, but then you can also override the OnClosing event of the form:
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
}
and that will keep the users from being able to literally close the form. Finally, if you wanted, you could Hide the form when the user closes it:
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}
and then you'll need to hold on to that instance in the first form so that you can re-show it when the button is clicked. So in the first form you might have a class field like this:
private Form2 _form2 = new Form2();
and then in the button click it would look like this:
_form2.Show();
and since they can't actually close the form this will always work.
You can try something like
bool windowIsNotOpen;
Mutex mutext = new Mutex(true, "Form2", out windowIsNotOpen);
if (!windowIsNotOpen)
{
// Form2 is already open
return;
}
You can do a static property that tells you whether it's open or not. You'd set it when the form is opened, and turn it off when the form is closed.
class Form2 {
public static bool IsOpen { get;set; }
public Form2() {
Load += (sender, e) => { Form2.IsOpen = true; };
Closed += (sender, e) => { Form2.IsOpen = false; };
}
}
Whenever you want to open it, check the flag first.
Form2 f;
bool isOpen = false;
private void button1_Click(object sender, EventArgs e)
{
if (f == null)
{
f = new Form2(); ;
f.FormClosed += new FormClosedEventHandler(f_FormClosed);
}
if (!isOpen)
{
isOpen = true;
f.Show();
}
}
void f_FormClosed(object sender, FormClosedEventArgs e)
{
isOpen = false;
}
Try this or use Application.OpenForms and check which one opened
I have a form and I want to get an instance of the same form as stated in the code below. And I have a button: every time I press this button, if a new form is created, I want it to focus to that window, if not, I want to create a new form.
I managed to create a new form but if I want to focus on it, the code did not work, any ideas?
private void btn_Click(object sender, EventArgs e)
{
if (opened == false)
{
Text = "form1";
var form = new myformapp();
form.Show();
opened = true;
form.Text = "form2";
}
else
{
if (Application.OpenForms[1].Focused)
{
Application.OpenForms[0].BringToFront();
Application.OpenForms[0].Focus();
}
if (Application.OpenForms[0].Focused)
{
Application.OpenForms[1].BringToFront();
Application.OpenForms[1].Focus();
}
}
}
You can try shortening your code without the need to introduce more variables with this example:
void button1_Click(object sender, EventArgs e) {
bool found = false;
for (int i = 0; i < Application.OpenForms.Count; ++i) {
if (Application.OpenForms[i].GetType() == typeof(myformapp) &&
Application.OpenForms[i] != this) {
Application.OpenForms[i].Select();
found = true;
}
}
if (!found) {
myformapp form = new myformapp();
form.Show();
}
}
Updated code from Francesco Baruchelli's comment.
If I understand correctly what you are trying to do, you can keep a static List with the opened forms. Everytime an instance of your Form is opened you add it to the List, and everytime it is closed you remove it. The when you press the button you can check the size of the List. If it is 1 you create a new Form, open it and set the focus on it. If the size is already 2, you look in the List for the instance which is different from the one executing the click event. The code could be something like this:
private static List<Form1> openForms = new List<Form1>();
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = null;
if (openForms.Count == 2)
{
foreach (Form1 aForm in openForms)
if (aForm != this)
{
frm = aForm;
break;
}
}
else
{
frm = new Form1();
frm.Show();
}
frm.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
openForms.Add(this);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
openForms.Remove(this);
}