Help Convert to vb.net .net 3.5(VS 2008)
-
2012年4月25日 15:33
Hello a create a extension method in C# .net 3.5 , with the help of this forum to catch the selection change in cells [msexcel 2007], and i like to now if it is possible convert this to vb.net vs2008?
This is the static method.
static class ExcelExtensions { public static IObservable<Excel.Range> WhenCellSelect(this Excel.Workbook WB) { var t= Observable.Create<Excel.Range>(observer => { Excel.WorkbookEvents_SheetSelectionChangeEventHandler handler = (Object sh,Excel.Range target) => { observer.OnNext(target); }; WB.SheetSelectionChange += handler; return () => WB.SheetSelectionChange -= handler; }); return t; }Thank's, sorry for my english
- 已编辑 carman68 2012年4月25日 15:40
全部回复
-
2012年4月25日 16:49
Hi,
Yes, it's possible to write your code in VB.NET instead of C#. Try asking your question in the VB.NET forum for help converting from C# to VB.NET.
Though your query can be simplified by using Observable.FromEvent or Observable.FromEventPattern.
- Dave
-
2012年4月25日 17:29
Thank's Dave i try the other forum for the conversion.
in C# how to simplify? can you show how or point directions (i newbie in rx thank's).
carman
-
2012年4月25日 18:23
Hi Carman,
It should look something like this, though I haven't tested it myself.
public static IObservable<Excel.Range> WhenCellSelect(this Excel.Workbook WB) { return Observable.FromEvent<Excel.WorkbookEvents_SheetSelectionChangeEventHandler, Excel.Range>( h => WB.SheetSelectionChange += h, h => WB.SheetSelectionChange -= h); }- Dave
-
2012年4月29日 8:20
Hi, Dave Thank's to point the direction, but i receved a
ArgumentException "Error binding to target method." with this StackTrace:
at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure) at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method) at System.Reactive.Linq.Observable.<>c__DisplayClass19c`2.<fromevent>b__19a(IObserver`1 observer) at System.Reactive.AnonymousObservable`1.<>c__DisplayClass1.<subscribe>b__0() at System.Reactive.Concurrency.Scheduler.Invoke(IScheduler scheduler, Action action) at System.Reactive.Concurrency.ScheduledItem`2.InvokeCore() at System.Reactive.Concurrency.ScheduledItem`1.Invoke() at System.Reactive.Concurrency.CurrentThreadScheduler.Trampoline.Run() at System.Reactive.Concurrency.CurrentThreadScheduler.Schedule[TState](TState state, TimeSpan dueTime, Func`3 action) at System.Reactive.Concurrency.CurrentThreadScheduler.Schedule[TState](TState state, Func`3 action) at System.Reactive.Concurrency.Scheduler.Schedule(IScheduler scheduler, Action action) at System.Reactive.AnonymousObservable`1.Subscribe(IObserver`1 observer) at System.ObservableExtensions.Subscribe[TSource](IObservable`1 source, Action`1 onNext, Action`1 onError, Action onCompleted) at System.ObservableExtensions.Subscribe[TSource](IObservable`1 source, Action`1 onNext) at ReactiveProgrammingConsole.InteropLab.Main() in D:\Metropolitano\Escalas\Excel_RX_events_C\Excel_RX_events_C\Program.cs:line 88</subscribe></fromevent>
I suspect that is in signature of method , the second parameter await a TEventArg and I am giving an Excel.Range, it is??
how to wrap the arguments of the excel workbook in TEventsAgs?
I call the method this way
try { WB.WhenCellSelect().Subscribe(cell => Console.WriteLine(cell.Row)); } catch(ArgumentException ex) { Console.WriteLine(ex.StackTrace); }
Thank's
carman68
-
2012年4月29日 10:18
Hi,
Try specifying the delegate conversion parameter:
public static IObservable<Excel.Range> WhenCellSelect(this Excel.Workbook WB) { return Observable.FromEvent<Excel.WorkbookEvents_SheetSelectionChangeEventHandler, Excel.Range>( h => h, // delegate conversion h => WB.SheetSelectionChange += h, h => WB.SheetSelectionChange -= h); }- Dave
- 已编辑 Dave Sexton 2012年4月29日 10:19 Forgot a comma
- 已标记为答案 carman68 2012年4月30日 1:06
-
2012年4月30日 1:07
Thank's Dave
apreciate the support
carman

