VbCodeHelper.DesignExt.Mono.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Reflection;
  6. using Microsoft.VisualBasic;
  7. using System.Drawing;
  8. using FastReport.Utils;
  9. using FastReport.Data;
  10. using FastReport.Design.PageDesigners.Code;
  11. namespace FastReport.Code
  12. {
  13. partial class VbCodeHelper
  14. {
  15. #region Public Methods
  16. public override List<string> GetEvents(Type eventType)
  17. {
  18. // build a string containing the event params.
  19. // for example, event type "EventHandler" will generate the following string: "object,EventArgs,"
  20. string eventParams = "";
  21. MethodInfo invoke = eventType.GetMethod("Invoke");
  22. System.Reflection.ParameterInfo[] evparams = invoke.GetParameters();
  23. foreach (System.Reflection.ParameterInfo p in evparams)
  24. {
  25. eventParams += p.ParameterType.Name + ",";
  26. }
  27. List<string> eventNames = new List<string>();
  28. string[] lines = Editor.Text.Split('\n');
  29. foreach (string line in lines)
  30. {
  31. string codeLine = line.Trim();
  32. if (codeLine.StartsWith("Private Sub"))
  33. {
  34. // split code line to words
  35. // for example: "Private Sub NewClick(ByVal sender As object, ByVal e As System.EventArgs)"
  36. string[] codeWords = codeLine.Split(new string[] { " ", ",", "(", ")", "Private", "Sub", "ByVal", "As" }, StringSplitOptions.RemoveEmptyEntries);
  37. int i = 0;
  38. if (i < codeWords.Length)
  39. {
  40. // now we get: "NewClick sender object e System.EventArgs"
  41. string eventName = codeWords[i];
  42. string pars = "";
  43. i += 2;
  44. // first argument
  45. if (i < codeWords.Length)
  46. pars = codeWords[i] + ",";
  47. i += 2;
  48. if (i < codeWords.Length)
  49. {
  50. string secondArg = codeWords[i];
  51. if (secondArg.IndexOf('.') != -1)
  52. {
  53. // if second argument is, for example, "System.EventArgs", take only "EventArgs" part
  54. string[] splitSecondArg = secondArg.Split('.');
  55. secondArg = splitSecondArg[splitSecondArg.Length - 1];
  56. }
  57. pars += secondArg + ",";
  58. }
  59. if (String.Compare(eventParams, pars, true) == 0)
  60. eventNames.Add(eventName);
  61. }
  62. }
  63. }
  64. return eventNames;
  65. }
  66. public override void LocateHandler(string eventName)
  67. {
  68. string[] lines = Editor.Text.Split('\n');
  69. int i = 0;
  70. foreach (string line in lines)
  71. {
  72. string codeLine = line.Trim();
  73. if (codeLine.StartsWith("Private Sub " + eventName))
  74. {
  75. Editor.Focus();
  76. Editor.Select(i, 0);
  77. break;
  78. }
  79. i += line.Length + 1;
  80. }
  81. }
  82. public override bool AddHandler(Type eventType, string eventName)
  83. {
  84. // get delegate params
  85. MethodInfo invoke = eventType.GetMethod("Invoke");
  86. System.Reflection.ParameterInfo[] pars = invoke.GetParameters();
  87. string eventParams = "";
  88. if (pars.Length == 2)
  89. eventParams = "(ByVal sender As object, ByVal e As " + pars[1].ParameterType.Name + ")";
  90. else
  91. {
  92. FRMessageBox.Error(String.Format(Res.Get("Messages,DelegateError"), eventType.ToString()));
  93. return false;
  94. }
  95. string[] lines = Editor.Text.Split('\n');
  96. int charIndex = 0;
  97. for (int i = 0; i < lines.Length; i++)
  98. {
  99. string line = lines[i];
  100. charIndex += line.Length + 1;
  101. line = line.Trim();
  102. if (line.StartsWith("Public Class ReportScript"))
  103. {
  104. string insert = " Private Sub " + eventName + eventParams + "\r\n ";
  105. Editor.Text = Editor.Text.Insert(charIndex, insert + "\r\n End Sub\r\n\r\n");
  106. Editor.Focus();
  107. Editor.Select(charIndex + insert.Length, 0);
  108. return true;
  109. }
  110. }
  111. FRMessageBox.Error(Res.Get("Messages,EventError"));
  112. return false;
  113. }
  114. #endregion
  115. }
  116. }