C# Form displayed once then won't display again

2 days ago 9
ARTICLE AD BOX

I am using Microsoft Visual Studio Professional 2022, the application is being developed using C#.

I have a form that is created and displays once, but will not display again, if I use breakpoints when debugging then the form will be displayed but if I remove the breakpoints it isn't, can anyone help?

The code that displays the form:

private void ShowSetupForm(dicParams_t dicParams, dicRegEx_t dicRegEx) { if (mfrmSetup != null && !mfrmSetup.IsDisposed) { MethodInvoker miDispose = new MethodInvoker(delegate { if (mfrmSetup != null) { mfrmSetup.Dispose(); mfrmSetup = null; } }); if (miDispose != null) { if (mfrmSetup.InvokeRequired) { mfrmSetup.Invoke(miDispose); } else { miDispose.Invoke(); } } } if (mdicControls != null) { mdicControls.Clear(); } if (mdicControls == null) { mdicControls = new dicControls_t(); } if (mdicClearIds != null) { mdicClearIds.Clear(); } mdicClearIds = new dicClear_t(); if (mdicTypes != null) { mdicTypes.Clear(); } if (mdicTypes == null) { mdicTypes = new dicCtrlTypes_t(); } if (mdicMacros != null) { mdicMacros.Clear(); } mdicMacros = new dicMacro_t(); if (mdicMaximum != null) { mdicMaximum.Clear(); } if (mdicMaximum == null) { mdicMaximum = new dicMaximum_t(); } if (mdicMinimum != null) { mdicMinimum.Clear(); } if (mdicMinimum == null) { mdicMinimum = new dicMinimum_t(); } //Ensure the table is clear MethodInvoker miSetupForm = new MethodInvoker(delegate { mfrmSetup.table.Controls.Clear(); mfrmSetup.table.ColumnStyles.Clear(); mfrmSetup.table.RowStyles.Clear(); }); mfrmSetup = new clsSetupForm(); if (mfrmSetup != null && miSetupForm != null) { if (mfrmSetup.InvokeRequired) { mfrmSetup.Invoke(miSetupForm); } else { miSetupForm.Invoke(); } } if (!(dicParams.Count > 0 && mfrmSetup != null && mfrmSetup.Visible != true && dicParams.ContainsKey(mcstrTitle))) { return; } mfrmSetup.AcceptButton = mfrmSetup.OK; mfrmSetup.CancelButton = mfrmSetup.Cancel; //Height and Width adjustments int intAdjHeight, intAdjWidth; intAdjHeight = intAdjWidth = 0; foreach (kvpParams_t kvPair in dicParams) { if (kvPair.Key .CompareTo(mcstrTitle) == 0) { mfrmSetup.Text = kvPair.Value; continue; } if (kvPair.Key .CompareTo(mcstrHeight) == 0) { int intHeight; if (int.TryParse(kvPair.Value, out intHeight)) { mfrmSetup.Height = intHeight; } continue; } if (kvPair.Key .CompareTo(mcstrWidth) == 0) { int intWidth; if (int.TryParse(kvPair.Value, out intWidth)) { mfrmSetup.Width = intWidth; } continue; } if (kvPair.Key .CompareTo(mcstrControls) != 0) { continue; } //Translate JSON string into controls array JsonArray aryControls = JsonSerializer .Deserialize<JsonArray> (kvPair.Value); //Process each record of the controls array int intLastRow = -1, intRow = 0; foreach (JsonObject objCtrl in aryControls) { string strLabel, strRegEx, strType; strLabel = strRegEx = strType = null; if (objCtrl[mcstrLabel] != null) { strLabel = objCtrl[mcstrLabel] .ToString(); } if (!string.IsNullOrEmpty(strLabel)) { if (objCtrl[mcstrRegEx] != null) { strRegEx = objCtrl[mcstrRegEx] .ToString(); dicRegEx.Add(strLabel, strRegEx); } } if (objCtrl[mcstrType] != null) { strType = objCtrl[mcstrType].ToString(); } CheckBox cbCtrl = null; Control ctrl = null; ListBox lbCtrl = null; Label lblCtrl = null; TextBox tbCtrl = null; Type tCtrl = null; if (!string.IsNullOrEmpty(strLabel)) { //Create and add label to table lblCtrl = new Label(); lblCtrl.Text = strLabel + ":"; lblCtrl.TextAlign = ContentAlignment.MiddleRight; } //Create and add control to table if (strType != null) { if (strType.CompareTo(mcstrCtrlCheckBox) == 0) { tCtrl = typeof(CheckBox); cbCtrl = new CheckBox(); if (cbCtrl != null) { ctrl = cbCtrl; } } else if (strType.CompareTo(mcstrCtrlListBox) == 0) { tCtrl = typeof(ListBox); lbCtrl = new ListBox(); if (lbCtrl != null) { lbCtrl.IntegralHeight = false; if (objCtrl[mcstrMultipleSection] != null) { bool blnMultipleSelection; if (bool.TryParse( objCtrl[mcstrMultipleSection].ToString() , out blnMultipleSelection)) { if (blnMultipleSelection == true) { lbCtrl.SelectionMode = SelectionMode .MultiExtended; } } } ctrl = lbCtrl; } } else if (strType.CompareTo(mcstrCtrlTextBox) == 0) { tCtrl = typeof(TextBox); tbCtrl = new TextBox(); if (tbCtrl != null) { if (!string.IsNullOrEmpty(strRegEx)) { tbCtrl.Tag = strRegEx; } ctrl = tbCtrl; } } } //Apply parameters string strName = string.Empty; //Look for name first, we use this to key other parameters if (objCtrl[mcstrName] != null) { strName = objCtrl[mcstrName].ToString(); } if (string.IsNullOrEmpty(strName)) { //Shouldn't get here! continue; } int intColumns = mfrmSetup.table.ColumnCount; int intRows = mfrmSetup.table.RowCount; float fltRowHeight = 0; for (int intPass = 1; intPass <= 2; intPass++) { Control ctrlCurrent = null; float fltWidth = mfrmSetup.table.Width; if (intPass == 1) { ctrlCurrent = lblCtrl; if (ctrl == null || lblCtrl == null) { //No control on the same line use full table width fltWidth = mfrmSetup.table.Width; } else { fltWidth = lblCtrl.Width; } } else { if (lblCtrl != null && ctrl != null) { //There is a label so position the control in the correct column fltWidth = ctrl.Width; } ctrlCurrent = ctrl; if (ctrl != null && lblCtrl != null && !string.IsNullOrEmpty(lblCtrl.Name)) { ctrl.Name = lblCtrl.Name; } } if (ctrlCurrent == null) { continue; } if (objCtrl[mcstrClear] != null) { JsonArray aryClearIds = (JsonArray)objCtrl[mcstrClear]; if (!mdicClearIds.ContainsKey(strName) && aryClearIds != null && aryClearIds.Count > 0) { mdicClearIds.Add(strName, aryClearIds); } } if (objCtrl[mcstrReadonly] != null) { bool blnReadonly = false; if (Boolean.TryParse(objCtrl[mcstrReadonly] .ToString() , out blnReadonly) && blnReadonly == true && tbCtrl != null) { tbCtrl.ReadOnly = true; } } //Adjust settings that could prevent dynamic resizing ctrlCurrent.AutoSize = false; ctrlCurrent.Dock = DockStyle.None; ctrlCurrent.Anchor = AnchorStyles.Top | AnchorStyles.Left; //Add OnSizeChanged handler ctrlCurrent.SizeChanged += OnSizeChanged; if (intLastRow != intRow) { intLastRow = intRow; float fltCtrlWidth = 0, fltTmpWidth; if (intPass == 1 && objCtrl[mcstrLabelWidth] != null) { if (float.TryParse(objCtrl[mcstrLabelWidth].ToString() , out fltTmpWidth)) { fltCtrlWidth = fltTmpWidth; } } else if (intPass == 2 && objCtrl[mcstrCtrlWidth] != null) { if (float.TryParse(objCtrl[mcstrCtrlWidth].ToString() , out fltTmpWidth)) { fltCtrlWidth = fltTmpWidth; } } if (fltCtrlWidth > 0) { ctrlCurrent.Width = (int)fltCtrlWidth; } if (ctrlCurrent.Height > fltRowHeight) { fltRowHeight = ctrlCurrent.Height; } if (objCtrl[mcstrCtrlHeight] != null) { float fltTmpHeight; if (float.TryParse(objCtrl[mcstrCtrlHeight].ToString() , out fltTmpHeight)) { fltRowHeight = fltTmpHeight; } } ctrlCurrent.Height = (int)fltRowHeight; mfrmSetup.table.Controls.Add(ctrlCurrent); } intRow++; //Set name first, it helps to identify control when debugging MethodInvoker miSetName = new MethodInvoker(delegate { ctrlCurrent.Name = strName; }); if (miSetName != null) { if (ctrlCurrent.InvokeRequired) { ctrlCurrent.Invoke(miSetName); } else { miSetName.Invoke(); } } //Is this name already in the dictionary? if (!mdicControls.ContainsKey(strName)) { //Add control to dictionary mdicControls.Add(strName, ctrl); if (tCtrl != null) { mdicTypes.Add(strName, tCtrl); } } if (objCtrl[mcstrMaximum] != null && !mdicMaximum.ContainsKey(strName)) { string strMaximum = objCtrl[mcstrMaximum].ToString(); float fltMaximum; if (float.TryParse(strMaximum, out fltMaximum)) { mdicMaximum.Add(strName, fltMaximum); } } if (objCtrl[mcstrMinimum] != null && !mdicMinimum.ContainsKey(strName)) { string strMinimum = objCtrl[mcstrMinimum].ToString(); float fltMinimum; if (float.TryParse(strMinimum, out fltMinimum)) { mdicMinimum.Add(strName, fltMinimum); } } if (objCtrl[mcstrOnChange] != null) { string strOnChange = objCtrl[mcstrOnChange] .ToString(); Type thisType = this.GetType(); if (thisType != null) { MethodInfo miMethod = thisType.GetMethod(strOnChange); if (miMethod != null) { ctrlCurrent.TextChanged += (EventHandler) Delegate.CreateDelegate (typeof(EventHandler), this, miMethod); } } } if (objCtrl[mcstrOnClick] != null) { string strOnClick = objCtrl[mcstrOnClick] .ToString(); Type thisType = this.GetType(); if (thisType != null) { MethodInfo miMethod = thisType.GetMethod(strOnClick); if (miMethod != null) { ctrlCurrent.Click += (EventHandler) Delegate.CreateDelegate (typeof(EventHandler), this, miMethod); } } } if (objCtrl[mcstrCheckAlign] != null && cbCtrl != null) { string strCheckAlign = objCtrl[mcstrCheckAlign] .ToString(); ContentAlignment eCntAlign = ContentAlignment .MiddleLeft; if (strCheckAlign .CompareTo(mcstrCenter) == 0) { eCntAlign = ContentAlignment .MiddleCenter; } else if (strCheckAlign .CompareTo(mcstrLeft) == 0) { eCntAlign = ContentAlignment .MiddleLeft; } else if (strCheckAlign .CompareTo(mcstrRight) == 0) { eCntAlign = ContentAlignment .MiddleRight; } MethodInvoker miCheckAlign = new MethodInvoker(delegate { cbCtrl.CheckAlign = eCntAlign; }); if (miCheckAlign != null) { if (cbCtrl.InvokeRequired) { cbCtrl.Invoke(miCheckAlign); } else { miCheckAlign.Invoke(); } } } if (objCtrl[mcstrTextAlign] != null && tbCtrl != null) { string strTextAlign = objCtrl[mcstrTextAlign] .ToString(); HorizontalAlignment eHorzAlign = HorizontalAlignment.Left; if (strTextAlign .CompareTo(mcstrCenter) == 0) { eHorzAlign = HorizontalAlignment .Center; } else if (strTextAlign .CompareTo(mcstrLeft) == 0) { eHorzAlign = HorizontalAlignment .Left; } else if (strTextAlign .CompareTo(mcstrRight) == 0) { eHorzAlign = HorizontalAlignment .Right; } MethodInvoker miSetTextAlign = new MethodInvoker(delegate { tbCtrl.TextAlign = eHorzAlign; }); if (miSetTextAlign != null) { if (tbCtrl.InvokeRequired) { tbCtrl.Invoke(miSetTextAlign); } else { miSetTextAlign.Invoke(); } } } if (intPass == 2 && objCtrl[mcstrText] != null) { MethodInvoker miSetText = new MethodInvoker(delegate { string strText = objCtrl[mcstrText].ToString(); if (strText.StartsWith(mcstrCurlyOpenBracket) && strText.EndsWith(mcstrCurlyCloseBracket)) { //Text contains a macro which will need replacing when known mdicMacros.Add(strName, strText); //Clear the macro text, we don't want the macro to be shown strText = string.Empty; } ctrlCurrent.Text = strText; }); if (miSetText != null) { if (ctrlCurrent.InvokeRequired) { ctrlCurrent.Invoke(miSetText); continue; } else { miSetText.Invoke(); } } } } } } if (intAdjHeight != 0) { mfrmSetup.Height = intAdjHeight; } if (intAdjWidth != 0) { mfrmSetup.Width = intAdjWidth; } mfrmSetup.ShowDialog(); }

Again with a break point on the line 'mfrmSetup.ShowDialog();', the form is displayed everytime, if the breakpoint is removed then it appears the form flashes up and immediately disappears.

Read Entire Article