Home>
I am creating a calculator that can be clicked in C#
In "var IniEvent = new Initialize()" of Main window process
It will be an infinite loop and the process will not proceed.
namespace Calculator
{
public partial class MainWindow: Window
{
///<summary> /// constructor
///</summary> public MainWindow()
{
InitializeComponent();
// Event initialization
var IniEvent = new Initialize();
IniEvent.InitializeEvent();
}
}
}
namespace Calculator
{
public partial class Initialize: MainWindow
{
///<summary> /// Event initialization
///</summary> public void InitializeEvent()
{
//rootGrid looks for the following controls
foreach (var ctrl in LogicalTreeHelper.GetChildren(rootGrid))
{
// ignore all but buttons
if (!(ctrl is Button))
{
continue;
}
// process registration when the button is clicked
(ctrl as Button).Click += (sender, e) => {
string inKey = (sender as Button).Content.ToString();
switch (inKey)
{
//clear
case "AC":
formula.Text = "0";
break;
case "=":
if (formula.Text == MessageList.CFNM0001)
{
formula.Text = "0";
}
else
{
//Display the calculation result
var Calc = new CalculateProcess();
formula.Text = Calc.Calculator(formula.Text);
}
break;
default:
if (formula.Text.Length >= ItemProperty.FormulaMax)
{
break;
}
else if (formula.Text == "0"&&(inKey != "/"&&inKey != "*"&&inKey != "-"&&inKey != "+"))
{
formula.Text = inKey;
}
else if (formula.Text == MessageList.CFNM0001)
{
//If 0 is displayed and a number is entered, set that value
formula.Text = inKey;}
else
{
// Add the entered value to the expression
formula.Text += inKey;
}
break;
}
};
}
}
}
}
Addendum
namespace Calculator
{
public partial class CalculateProcess: MainWindow
{
///<summary> /// calculation execution
///</summary> ///<param name="formula">Calculation formula</param> ///<returns>calculation result</returns> public string Calculator(string formula)
{
try
{
var p = new Process();
// Get system folder
string systemFolder = Environment.GetFolderPath(Environment.SpecialFolder.System);
// assemble the full PowerShell path
string powerShellFullPath = System.IO.Path.Combine(systemFolder, @"WindowsPowerShell\v1.0\powershell.exe");
// executable file
p.StartInfo.FileName = powerShellFullPath;
// parameter (formula)
p.StartInfo.Arguments = "-Command" + formula;
// do not redirect standard input
p.StartInfo.RedirectStandardInput = false;
// redirect standard input
p.StartInfo.RedirectStandardOutput = true;
// do not use shell function
p.StartInfo.UseShellExecute = false;
// do not open window
p.StartInfo.CreateNoWindow = true;
// run
p.Start();
// read a line of results
string calcResult = p.StandardOutput.ReadLine();
p.WaitForExit();
p.Close();
if (string.IsNullOrEmpty(calcResult))
{
// No calculation result (calculation failure)
return MessageList.CFNM0001;
}
else
{
// return the calculation result
return calcResult;
}
}
catch
{
return MessageList.CFNM0001;
}
}
}
}
What I tried
I tried to check it with "Instantiation infinite loop", "Main infinite loop", etc., but it didn't come out, and the cause is unknown.
Why is it in an infinite loop?
Also, how should we improve?
If i do not mind, please teach me
Windows10 Pro 64bit
Visual Studio 2015
Please let me know if you have any other necessary information.
Solution method (done)namespace Calculator
{
public partial class Initialize
{
///<summary> /// Event initialization
///</summary> public void InitializeEvent(MainWindow w)
{
//rootGrid looks for the following controls
foreach (var ctrl in LogicalTreeHelper.GetChildren(w.rootGrid))
{
// ignore all but buttons
if (!(ctrl is Button))
{
continue;
}
// process registration when the button is clicked
(ctrl as Button).Click += (sender, e) => {
string inKey = (sender as Button).Content.ToString();
switch (inKey)
{
//clear
case "AC":
w.formula.Text = "0";
break;
case "=":
if (w.formula.Text == MessageList.CFNM0001)
{
w.formula.Text = "0";
}
else
{
//Display the calculation result
var Calc = new CalculateProcess();
w.formula.Text = Calc.Calculator(w.formula.Text);
}
break;
default:
if (w.formula.Text.Length >= ItemProperty.FormulaMax)
{
break;
}
else if (w.formula.Text == "0"&&(inKey != "/"&&inKey != "*"&&inKey != "-"&&inKey != "+"))
{
w.formula.Text = inKey;
}
else if (w.formula.Text == MessageList.CFNM0001)
{
//If 0 is displayed and a number is entered, set that value
w.formula.Text = inKey;
}
else
{
// Add the entered value to the expression
w.formula.Text += inKey;
}
break;
}
};
}
}
}
}
-
Answer # 1
Related articles
- c# - method detection by instantiated gameobjects
- c# - [unity] i get a "not instantiated" error in the script, but i'm having trouble understanding the cause
- c# - want to destroy objects instantiated by players | photon bolt
- c# - how to access the instantiated game object after changing the scene
- javascript calculator creation
- c#pdf creation ⇒ binary conversion ⇒ concept of encoding
- calculator creation with html + javascript = does not work
- java - calculator creation with androidstudio
Trends
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs
If you inherit MainWindow, the constructor of MainWindow will be called when this class is created, which will result in an infinite loop.
You need to rethink your class design.