0

I have a application in which i want to open a calculator on a button click and once the operation are performed on calculator and calculator is closed, I want that value back to my text box. How can I achieve this functionality.

  private void btnDollarTransferHelp_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process myProcess = System.Diagnostics.Process.Start("calc.exe");
    }

I am using C# 4.0 and its a window based application.

Regards and Thanks

Shashank
  • 6,117
  • 20
  • 51
  • 72
  • I'm not aware of any API which exposes the values/functionality from `calc.exe`. Did you find one somewhere and you're trying to use it? Or are you just assuming there must be one? (If the latter, there probably isn't.) – David Mar 29 '13 at 11:02
  • @David:- http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7065af59-00a0-4899-98c6-495bc704c41e – Shashank Mar 29 '13 at 11:04
  • 1
    Might be quicker to write a calculator ... how much work could it be? – Rob Mar 29 '13 at 11:05
  • 3
    @Shashank: Nice to see the Microsoft MVPs are still as vague and unhelpful as always :) That does seem to indicate that there might be a way, but given the lack of information I'm just not seeing it. Perhaps something within the depths of COM, which I've spent a career successfully avoiding. Sorry :( – David Mar 29 '13 at 11:07
  • Start your research with SPY++ which is already installed with your Visual Studio. – G.Y Mar 29 '13 at 11:52

3 Answers3

0

You may use the Microsoft Script Control to implement the calculator functionality in your C# app. The control has a simple Eval() method to evaluate expressions that may range from simple "2+2" to VBScript commands. More information here:

http://msdn.microsoft.com/en-us/library/aa227637(v=vs.60).aspx

Prahlad Yeri
  • 3,567
  • 4
  • 25
  • 55
0

It is indeed simpler to implement a calculator in your app: reading the content of another's process window is not trivial! And being notified when the calculator is "about to close" is even harder.

It involves, for sure, widows API (FindMessage/SendMessage) (see for example https://stackoverflow.com/a/360247/863564), probably P/Invoke, probably even hooking calc.exe (the technique used by Spy++ -- you need it to capture the WM_CLOSE message - see here what happens when you are closing a window)

(You need to capture WM_CLOSE, because it is the latest sensible moment to grab the result from the control on the window)

So, the alternative: a great one is scriptcs, but it may be well too much! A quick search revealed
some
great
projects

Either way, have fun!

Community
  • 1
  • 1
Lorenzo Dematté
  • 7,638
  • 3
  • 37
  • 77
0

you could use an existing library e.g. http://nclac.codeplex.com to provide calculation within your application.

GreyCloud
  • 3,030
  • 5
  • 32
  • 47