123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Reflection;
- using Microsoft.VisualBasic;
- using System.Drawing;
- using FastReport.Utils;
- using FastReport.Data;
- using FastReport.Design.PageDesigners.Code;
- namespace FastReport.Code
- {
- partial class VbCodeHelper
- {
- #region Public Methods
- public override List<string> GetEvents(Type eventType)
- {
- // build a string containing the event params.
- // for example, event type "EventHandler" will generate the following string: "object,EventArgs,"
- string eventParams = "";
- MethodInfo invoke = eventType.GetMethod("Invoke");
- System.Reflection.ParameterInfo[] evparams = invoke.GetParameters();
- foreach (System.Reflection.ParameterInfo p in evparams)
- {
- eventParams += p.ParameterType.Name + ",";
- }
- List<string> eventNames = new List<string>();
- string[] lines = Editor.Text.Split('\n');
- foreach (string line in lines)
- {
- string codeLine = line.Trim();
- if (codeLine.StartsWith("Private Sub"))
- {
- // split code line to words
- // for example: "Private Sub NewClick(ByVal sender As object, ByVal e As System.EventArgs)"
- string[] codeWords = codeLine.Split(new string[] { " ", ",", "(", ")", "Private", "Sub", "ByVal", "As" }, StringSplitOptions.RemoveEmptyEntries);
- int i = 0;
- if (i < codeWords.Length)
- {
- // now we get: "NewClick sender object e System.EventArgs"
- string eventName = codeWords[i];
- string pars = "";
- i += 2;
- // first argument
- if (i < codeWords.Length)
- pars = codeWords[i] + ",";
- i += 2;
- if (i < codeWords.Length)
- {
- string secondArg = codeWords[i];
- if (secondArg.IndexOf('.') != -1)
- {
- // if second argument is, for example, "System.EventArgs", take only "EventArgs" part
- string[] splitSecondArg = secondArg.Split('.');
- secondArg = splitSecondArg[splitSecondArg.Length - 1];
- }
- pars += secondArg + ",";
- }
- if (String.Compare(eventParams, pars, true) == 0)
- eventNames.Add(eventName);
- }
- }
- }
- return eventNames;
- }
- public override void LocateHandler(string eventName)
- {
- string[] lines = Editor.Text.Split('\n');
- int i = 0;
- foreach (string line in lines)
- {
- string codeLine = line.Trim();
- if (codeLine.StartsWith("Private Sub " + eventName))
- {
- Editor.Focus();
- Editor.Select(i, 0);
- break;
- }
- i += line.Length + 1;
- }
- }
- public override bool AddHandler(Type eventType, string eventName)
- {
- // get delegate params
- MethodInfo invoke = eventType.GetMethod("Invoke");
- System.Reflection.ParameterInfo[] pars = invoke.GetParameters();
- string eventParams = "";
- if (pars.Length == 2)
- eventParams = "(ByVal sender As object, ByVal e As " + pars[1].ParameterType.Name + ")";
- else
- {
- FRMessageBox.Error(String.Format(Res.Get("Messages,DelegateError"), eventType.ToString()));
- return false;
- }
- string[] lines = Editor.Text.Split('\n');
- int charIndex = 0;
- for (int i = 0; i < lines.Length; i++)
- {
- string line = lines[i];
- charIndex += line.Length + 1;
- line = line.Trim();
- if (line.StartsWith("Public Class ReportScript"))
- {
- string insert = " Private Sub " + eventName + eventParams + "\r\n ";
- Editor.Text = Editor.Text.Insert(charIndex, insert + "\r\n End Sub\r\n\r\n");
- Editor.Focus();
- Editor.Select(charIndex + insert.Length, 0);
- return true;
- }
- }
- FRMessageBox.Error(Res.Get("Messages,EventError"));
- return false;
- }
- #endregion
- }
- }
|