{"id":173,"date":"2016-03-01T12:32:03","date_gmt":"2016-03-01T12:32:03","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=173"},"modified":"2022-02-17T06:24:14","modified_gmt":"2022-02-17T06:24:14","slug":"general-form-usage-2","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/forms\/general-form-usage-2","title":{"rendered":".General Form Usage"},"content":{"rendered":"<h4>\n\tOpening a form<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tfrmConfiguration ConfigForm = new frmConfiguration();\r\n\tConfigForm.PassedValue1 = &quot;This is a string I&#39;m passing&quot;;\r\n\r\n\t\/\/TO OPEN AS A DIALOG FORM:\r\n\tConfigForm.ShowDialog();\r\n\t\/\/or\r\n\tif (ConfigForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)\r\n\telse if (ConfigForm.DialogResult == System.Windows.Forms.DialogResult.Cancel)\r\n\r\n\t\/\/TO OPEN NOT AS A DIALOG FORM:\r\n\tConfigForm.Show();\r\n<\/code><\/pre>\n<h4>\n\tClosing a form<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tClose();\r\n\t\/\/or\r\n\tthis.Close();\r\n<\/code><\/pre>\n<h4>\n\tClosing a form from the Form Load Event<br \/>\n<\/h4>\n<p>\n\tYou can&#39;t use this.Close as the form has not yet finished loading so if you do you&#39;ll cause fatal error. Instead use this:\n<\/p>\n<pre>\r\n<code>\r\n\tthis.BeginInvoke(new MethodInvoker(this, System.Windows.Forms.Form.Close));\r\n<\/code><\/pre>\n<h4>\n\tDynamically updating Form Properties<br \/>\n<\/h4>\n<p>\n\tUse this instead of the forms name:\n<\/p>\n<pre>\r\n<code>\tthis.Text = &quot;A New Title For This Form&quot;;<\/code><\/pre>\n<h4>\n\tTo Disable A Form<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/This form\r\n\tthis.Enabled = false;\r\n\t\/\/Another form\r\n\tfrmMyForm.Enabled = false;\r\n<\/code><\/pre>\n<h4>\n\tWhy Is A Form Closing?<br \/>\n<\/h4>\n<p>\n\tYou can use things like this (see the CloseReason members):\n<\/p>\n<pre>\r\n<code>if (e.CloseReason == System.Windows.Forms.CloseReason.UserClosing)<\/code><\/pre>\n<h4>\n\tSupressing Display Update Of A Form<br \/>\n<\/h4>\n<p>\n\tYou can&#39;t stop a form being painted, but you can do this:\n<\/p>\n<pre>\r\n<code>\r\n\tthis.SuspendLayout();\r\n\tMyControlName1.SuspendLayout();\r\n\tMyControlName2.SuspendLayout();\r\n\t...\r\n\tMyControlName1.ResumeLayout();\r\n\tMyControlName2.ResumeLayout();\r\n\tthis.ResumeLayout();\r\n<\/code><\/pre>\n<p>\n\tYou can also make specific controls invisible\n<\/p>\n<h4>\n\tDefault Accept and Cancel Buttons<br \/>\n<\/h4>\n<p>\n\tSet in the forms AcceptButton and CancelButton properties.<br \/>\n\tSet Dialog result for each button\n<\/p>\n<h4>\n\tReturning a Dialog Result<br \/>\n<\/h4>\n<p>\n\tYou can assign a dialog result value to buttons on the form in their properties.\n<\/p>\n<h4>\n\tStopping A Form \/ Application From Closing<br \/>\n<\/h4>\n<p>\n\tHandle the form&#39;s &#39;Closing&#39; event and set &#39;e.Cancel&#39; to &#39;True&#39; if you want to prevent the form from closing.<br \/>\n\te.Cancel = true;<br \/>\n\tIf your going to present a message box, such as a &#39;do you want to save&#39; then you need to do the cancel first to stop the ap closing while waiting for the user input. Then afterwards do the close.\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Opening a form frmConfiguration ConfigForm = new frmConfiguration(); ConfigForm.PassedValue1 = &quot;This is a string I&#39;m passing&quot;; \/\/TO OPEN AS A DIALOG FORM: ConfigForm.ShowDialog(); \/\/or if (ConfigForm.ShowDialog() == System.Windows.Forms.DialogResult.OK) else if (ConfigForm.DialogResult == System.Windows.Forms.DialogResult.Cancel) \/\/TO OPEN NOT AS A DIALOG FORM: ConfigForm.Show(); Closing a form Close(); \/\/or this.Close(); Closing a form from the Form Load Event [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-173","post","type-post","status-publish","format-standard","hentry","category-forms"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":174,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/173\/revisions\/174"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}