Sample demo code: Error messages in WinForms
Posted: Sat Jan 10, 2009 12:06 pm
Hi all,
In VB.NET using WinForms I usally try to catch a run time error with the information ASAP.
I. E almost all my methods has a try catch!
I have made a small demo program in Cobra showing the techniques. There are a number of issues in the program that I'm sure can be solved in a more elegant way. After a rewrite, I think it is a very useful demo program.
Which editor is best for Cobra today?
Regards
Csaba
In VB.NET using WinForms I usally try to catch a run time error with the information ASAP.
I. E almost all my methods has a try catch!
I have made a small demo program in Cobra showing the techniques. There are a number of issues in the program that I'm sure can be solved in a more elegant way. After a rewrite, I think it is a very useful demo program.
Which editor is best for Cobra today?
Regards
Csaba
- Code: Select all
"""
Purpose: Demonstrates how to show a informative error message in the MessageBox
at runtime ASAP where unexpected error occured
Assumptions:
Compiled with cobra WinForms -r:System.Windows.Forms using Cobra v0.8
To improve:
1 Problem to set up global constants
2 Can't use the VB specific MsgBoxStyle.Exclamation, Cobra version is unknown
3 Don't know if there are nicer Cobra reflection methods.
4 Does Cobra have a meta method for the application title?
v 0.5 by Csaba Urbaniczky (CsabaU@HotMail.com)
"""
use System.Windows.Forms # does not work
class Demo
def doFail
try
MessageBox.show('Hello from [.getType.name].[System.Reflection.MethodBase.getCurrentMethod.name]')
zero=0 # since Cobra is to clever 1/0 can't be used!
MessageBox.show( "This will never be shown: [1/zero] ")
catch e as Exception # Cobra errror message here
MessageBox.show('EXCEPTION in [.getType.name].[System.Reflection.MethodBase.getCurrentMethod.name]: \n [e.message] ', Program.appTitle, MessageBoxButtons.OK)
class DemoForm
inherits Form
def init
.text = ' [Program.appTitle] Click me!'
listen .click, ref .handleClick # Cobra set up specific
def handleClick(sender as Object, args as EventArgs)
try
# this will only be executed if no error above
zero=0 # since Cobra is to clever 1/0 can't be used!
MessageBox.show("This will never be shown: [1/zero] ")
catch e as Exception # .NET error message here
MessageBox.show('EXCEPTION in [.getType.name].[System.Reflection.MethodBase.getCurrentMethod.name]: \n [e.message] ', Program.appTitle, MessageBoxButtons.OK)
class Program
var _appTitle as String is shared # = "WinFormsDemo" I'm not able to initialize and set ' is shared' in single line
#get appTitle from _appTitle is shared does not compile # This since Cobra has no Const
def appTitle as String is shared # work around for the non-working property
return _appTitle
def main is shared
_appTitle = "WinFormsDemo" # set here in the work around
try
MessageBox.show('Hello from [Program.appTitle].[System.Reflection.MethodBase.getCurrentMethod.name]', Program.appTitle, MessageBoxButtons.OK ) # Missing: MsgBoxStyle.Exclamation
Application.run(DemoForm())
x as dynamic = Demo
x.doFail # This object will show a Cobra specific run time error - not the .NET specific
catch e as Exception # .NET error message here
MessageBox.show('EXCEPTION in [Program.appTitle].[System.Reflection.MethodBase.getCurrentMethod.name]:\n [e.message]', Program.appTitle, MessageBoxButtons.OK ) # , MsgBoxStyle.Exclamation