Coming from Visual basic, I kind of miss the With statement that is lacking in C#, I'm looking for ways to refactor the code so it's not so crowded.
I have the following code:
Globals.Ribbons.RibbonMain.chkCalculation.Checked = (Globals.ThisAddIn.Application.Calculation == Microsoft.Office.Interop.Excel.XlCalculation.xlCalculationAutomatic);
Globals.Ribbons.RibbonMain.chkScreenUpdating.Checked = Globals.ThisAddIn.Application.ScreenUpdating;
Globals.Ribbons.RibbonMain.chkEvents.Checked = Globals.ThisAddIn.Application.EnableEvents;
Globals.Ribbons.RibbonMain.chkDisplayAlerts.Checked = Globals.ThisAddIn.Application.DisplayAlerts;
How can I extract the common denominators to trim down the code to make it more legible?
What I thought is creating the 2 variables below, but i don't know the variable type I should use. Is there somewhere I could look for the variable type?
variableType R = Globals.Ribbons.RibbonMain
variableType A = Globals.ThisAddIn.Application
var should work fine, let the compiler figure out the type:
var R = Globals.Ribbons.RibbonMain;
var A = Globals.ThisAddIn.Application;
R.chkCalculation.Checked = (A.Calculation == Microsoft.Office.Interop.Excel.XlCalculation.xlCalculationAutomatic);
R.chkScreenUpdating.Checked = A.ScreenUpdating;
R.chkEvents.Checked = A.EnableEvents;
R.chkDisplayAlerts.Checked = A.DisplayAlerts;
Thank you #madreflection for sharing the knowledge.
As I'm using the 2 variables in multiple places, I extracted them as Static variables.
using EXCEL = Microsoft.Office.Interop.Excel;
internal class clsMacros
{
//GLOBAL VARIABLES
static RibbonMain RIBBON = Globals.Ribbons.RibbonMain;
static EXCEL.Application APPLICATION = Globals.ThisAddIn.Application;
public static void GetStatus()
{
RIBBON.chkCalculation.Checked = (APPLICATION.Calculation == EXCEL.XlCalculation.xlCalculationAutomatic);
RIBBON.chkScreenUpdating.Checked = APPLICATION.ScreenUpdating;
RIBBON.chkEvents.Checked = APPLICATION.EnableEvents;
RIBBON.chkDisplayAlerts.Checked = APPLICATION.DisplayAlerts;
}
}
Related
New Info:
I thought I would paste this in full as I can not seem to find any samples on the web of a c# solution for StarLink so hopefully anyone else looking for something may find this helpful and may contribute.
My New Proto File - (partial) - I took the advise of Yuri below. Thanks for the direction here. I was able to I have been using this tool and it has brought a lot of insight but I am still stuck on the c# side of the solution. I am an old VB.Net developer though I have done a bunch in c# I am by no means savvy in it and am probably missing something so simple. Again, any insight would be awesome. I can not post the full proto here as stack has char limit on posts. this is the first bit with messages etc. I can post more if it helps but trying to keep it to the important part.
syntax = "proto3";
option csharp_namespace = "SpaceX.API.Device";
package SpaceX.API.Device;
service Device {
//rpc Handle (.SpaceX.API.Device.Request) returns (.SpaceX.API.Device.Response) {}
//rpc Stream (stream .SpaceX.API.Device.ToDevice) returns (stream .SpaceX.API.Device.FromDevice) {}
rpc Handle (Request) returns (Response);
rpc Stream (Request) returns (Response);
}
message ToDevice {
string message = 1;
}
message Request {
uint64 id = 1;
string target_id = 13;
uint64 epoch_id = 14;
oneof request {
SignedData signed_request = 15;
RebootRequest reboot = 1001;
SpeedTestRequest speed_test = 1003;
GetStatusRequest get_status = 1004;
AuthenticateRequest authenticate = 1005;
GetNextIdRequest get_next_id = 1006;
GetHistoryRequest get_history = 1007;
GetDeviceInfoRequest get_device_info = 1008;
GetPingRequest get_ping = 1009;
SetTrustedKeysRequest set_trusted_keys = 1010;
FactoryResetRequest factory_reset = 1011;
GetLogRequest get_log = 1012;
SetSkuRequest set_sku = 1013;
UpdateRequest update = 1014;
GetNetworkInterfacesRequest get_network_interfaces = 1015;
PingHostRequest ping_host = 1016;
GetLocationRequest get_location = 1017;
EnableFlowRequest enable_flow = 1018;
GetHeapDumpRequest get_heap_dump = 1019;
RestartControlRequest restart_control = 1020;
FuseRequest fuse = 1021;
GetPersistentStatsRequest get_persistent_stats = 1022;
GetConnectionsRequest get_connections = 1023;
FlushTelemRequest flush_telem = 1026;
StartSpeedtestRequest start_speedtest = 1027;
GetSpeedtestStatusRequest get_speedtest_status = 1028;
ReportClientSpeedtestRequest report_client_speedtest = 1029;
InitiateRemoteSshRequest initiate_remote_ssh = 1030;
SelfTestRequest self_test = 1031;
SetTestModeRequest set_test_mode = 1032;
DishStowRequest dish_stow = 2002;
DishGetContextRequest dish_get_context = 2003;
DishSetEmcRequest dish_set_emc = 2007;
DishGetObstructionMapRequest dish_get_obstruction_map = 2008;
DishGetEmcRequest dish_get_emc = 2009;
DishSetConfigRequest dish_set_config = 2010;
DishGetConfigRequest dish_get_config = 2011;
StartDishSelfTestRequest start_dish_self_test = 2012;
WifiSetConfigRequest wifi_set_config = 3001;
WifiGetClientsRequest wifi_get_clients = 3002;
WifiSetupRequest wifi_setup = 3003;
WifiGetPingMetricsRequest wifi_get_ping_metrics = 3007;
WifiGetDiagnosticsRequest wifi_get_diagnostics = 3008;
WifiGetConfigRequest wifi_get_config = 3009;
WifiSetMeshDeviceTrustRequest wifi_set_mesh_device_trust = 3012;
WifiSetMeshConfigRequest wifi_set_mesh_config = 3013;
WifiGetClientHistoryRequest wifi_get_client_history = 3015;
TransceiverIFLoopbackTestRequest transceiver_if_loopback_test = 4001;
TransceiverGetStatusRequest transceiver_get_status = 4003;
TransceiverGetTelemetryRequest transceiver_get_telemetry = 4004;
}
reserved 1025, 3011, 3014;
}
message SignedData {
bytes data = 1;
bytes signature = 2;
}
My New .cs
I have tried many things from Microsoft's examples to thing I can gather from other samples. I simply can not get it to work and am lost. Again, any insight would be amazing and hopefully helpful to others looking for a solution in c#. You will see my commented code of this I have been playing with. Basically I am attempting to achieve three things and have made some movement in one of them.
Goals:
1 - Use Server Reflection to discover services.
I think I got this one resolved with dot-net grpc.
2 - Simply want to check available methods under a service and potentially either check or generate a new .proto file in case things change. StaLink does not publish its proto schema so I assume it could change anytime without warning.
3 - Just run any one of the available methods. I have tried the GetDeviceInfoRequest but can not seem to construct the request message properly. I have not been able to get this accomplishe in the gRPCurl tool either. I can do it on the basic service shown by Microsoft of course but these methods seem to be more complex and I simply get all kinds of errors.
Again, any insight or assistance would be amazing. Thanks to any and all in advance.
New .cs File
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Net.Client;
using Grpc.Reflection.V1Alpha;
using ServerReflectionClient = Grpc.Reflection.V1Alpha.ServerReflection.ServerReflectionClient;
using SpaceX.API.Device;
public class Program
{
static async Task Main(string[] args)
{
//SETUP CHANNEL AND CLIENT
using var channel = GrpcChannel.ForAddress("http://192.168.100.1:9200");
var client = new ServerReflectionClient(channel);
var StarLinkClient = new Device.DeviceClient(channel);
//using var call = StarLinkClient.StreamAsync(new ToDevice { Request = GetDeviceInfoRequest });
//await foreach (var response in call.ResponseStream.ReadAllAsync())
//var request = Device.GetDeviceInfoRequest;
//var reply = await StarLinkClient.HandleAsync(
// new Request {'"getDeviceInfo" : {} '});
//Console.WriteLine(reply.Message);
//=============================================SERVER REFLECTION=============================================================
Console.WriteLine("Calling reflection service:");
var response = await SingleRequestAsync(client, new ServerReflectionRequest
{
ListServices = "" // Get all services
});
Console.WriteLine("Services:");
foreach (var item in response.ListServicesResponse.Service)
{
Console.WriteLine("- " + item.Name);
Console.WriteLine();
var StarLink = item.Name;
//Console.WriteLine(StarLink.getStatus());
}
//=============================================SERVER REFLECTION=============================================================
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
void setupchannel()
{
}
private static Task SingleRequestAsync(ServerReflectionClient client, Metadata metadata)
{
throw new NotImplementedException();
}
private static async Task<ServerReflectionResponse> SingleRequestAsync(ServerReflectionClient client, ServerReflectionRequest request)
{
using var call = client.ServerReflectionInfo();
await call.RequestStream.WriteAsync(request);
Debug.Assert(await call.ResponseStream.MoveNext());
var response = call.ResponseStream.Current;
await call.RequestStream.CompleteAsync();
return response;
}
}
Again, thanks in advance to anyone willing to assist here. Hopefully this helps others as well.
I need to get the available OpenType features for a given font in my app (C#). I'm using DirectWrite through SharpDX and I'm having a really bad time.
I know that the best solution is to use this method:
SharpDX.DirectWrite.TextAnalyzer2.GetTypographicFeatures
but I don't know how to get a result from it, as I don't know where to get the parameters.
These are the parameters I need to provide in order to get the Font Features:
fontFace FontFace
scriptAnalysis ScriptAnalysis
localName String
maxTagCount int
actualTagCount int
tags FontFeatureTag
Can someone please provide me a better explanation or (ideally) some code. There is almost no documentation about it so I don't know where I can get these parameters and/or what they mean.
Thanks in advance.
I figure it out lastly. Thanks to Buglehead who gave me the final piece of the puzzle.
Here is an example. In this code I first load all system fonts, then get a specific font, and then get the FontFeatureTags for that specific font.
using SharpDX.DirectWrite;
private void LoadFontFeatureTags()
{
Factory f = new Factory(FactoryType.Isolated);
Factory4 _factory = new Factory4(f.NativePointer);
_factory.CreateFontCollectionFromFontSet(_factory.SystemFontSet, out FontCollection1 collection);
List<SharpDX.DirectWrite.FontFamily> loadedFonts = new List<SharpDX.DirectWrite.FontFamily>();
for (int i = 0; i < collection.FontFamilyCount; i++)
{
var family = collection.GetFontFamily(i);
loadedFonts.Add(family);
}
var gabriolaFont = loadedFonts.FirstOrDefault(x => x.FamilyNames.GetString(0).Contains("Gabriola"));
var gabriolaFirstChild = gabriolaFont.GetFont(0);
Font3 f3 = new Font3(gabriolaFirstChild.NativePointer);
f3.CreateFontFace(out FontFace3 face3);
ScriptAnalysis analysis = new ScriptAnalysis();
TextAnalyzer analyzer = new TextAnalyzer(f);
TextAnalyzer2 analyzer2 = new TextAnalyzer2((IntPtr)analyzer);
int maxTagCount = 32;
FontFeatureTag[] featuresArray = new FontFeatureTag[maxTagCount];
analyzer2.GetTypographicFeatures(face3, analysis, "es-US", maxTagCount, out int actualCount, featuresArray);
}
I am working on an application that should compile and debug C# code on the fly.
A simplified version of the code is included below.
What should be changed in this code to run the generated method step by step and get the state of the variables x and y after each step?
If everything should be changed that is okay, I am happy with any constructive response.
EDIT: to clarify: what I want to do is have my code debug the code that is generated with reflection, not the debug function in Visual Studio.
string code =
#"
namespace MyNameSpace
{
public class MyClass
{
public static int MyMethod()
{
var x = 3;
var y = 4;
return x * y;
}
}
}";
string namespaceName = "MyNameSpace";
string className = "MyClass";
string methodName = "MyMethod";
string language = "csharp";
string classFullname = namespaceName + "." + className;
CodeDomProvider provider = CodeDomProvider.CreateProvider(language);
CompilerParameters parameters = new CompilerParameters();
CompilerResults results;
parameters.OutputAssembly = "Compiler";
parameters.CompilerOptions = "/t:library";
parameters.GenerateInMemory = true;
parameters.GenerateExecutable = false;
parameters.IncludeDebugInformation = true;
results = provider.CompileAssemblyFromSource(parameters, code);
if (results.Errors.Count != 0)
{
throw new Exception("Code compilation errors occurred.");
}
var instance = results.CompiledAssembly.CreateInstance(classFullname, false);
// TODO run the method step by step and get the state after each step
This configuration may help you:
parameters.GenerateInMemory = false; //default
parameters.TempFiles = new
TempFileCollection(Environment.GetEnvironmentVariable("TEMP"), true);
parameters.IncludeDebugInformation = true;
parameters.TempFiles.KeepFiles = true
To debug the generated code you will need the pdb files. To have those while debugging your application, just have to tell the compiler where to save the temporary files. For this you can just add the following line to your parameters:
parameters.TempFiles = new TempFileCollection(Environment.GetEnvironmentVariable("TEMP"), true);
You can then step into the Invokation of your targeted method. The Code could look like this:
var method = instance?.GetType().GetMethod(methodName);
method?.Invoke(instance, BindingFlags.InvokeMethod, null, null, CultureInfo.CurrentCulture);
If you want the Debugger to automatically stop, when entering your "MyMethod", you can modify your string likes this:
string code =
#"using System.Diagnostics;
namespace MyNameSpace
{
public class MyClass
{
public int MyMethod()
{
Debugger.Break();
var x = 3;
var y = 4;
return x * y;
}
}
}";
Elchido pointed out in the comments that maybe I should look for an interpreter. After a bit of searching I came across CSI: A Simple C# Interpreter.
https://www.codeproject.com/Articles/10212/CSI-A-Simple-C-Interpreter
After investigating, my conclusion is that it is possible to use either and interpreter or the Codedom compiler to create debugger-like functionality, but it takes a significant effort.
The solution that I am working on involves splitting the code into separate statements and put all variables in an array.
The 'MyMethod' function is split into parts:
public static object Line1()
{
return 3;
}
public static object Line2()
{
return 4;
}
public static object Line3(object x, object y)
{
return x*y;
}
After compiling the code using Codedom compiler, I can do the following:
Dictionary<string, object> vars = new Dictionary<string, object>();
List<MethodInfo> lines = new List<MethodInfo>();
lines.Add(type.GetMethod("Line1"));
lines.Add(type.GetMethod("Line2"));
lines.Add(type.GetMethod("Line3"));
vars["x"] = lines[0].Invoke(instance, new object[] { });
vars["y"] = lines[1].Invoke(instance, new object[] { });
vars["#return"] = lines[2].Invoke(instance, new object[] { vars["x"], vars["y"] });
Note that this is not a working solution yet, a lot of work still has to be done to convert the 'MyMethod code' into separate lines and extract the variables. I will post an update when I have more/better code.
Click just left side of your code it will mark red dot that is called break point.After that when your code execute at the point it will break at the point and you can debug step by step bt pressing F10 key.
I'm trying to use word to automatically correct some text that is not in English the problem is that when i use the SpellCheck function the "Spell and Grammar" dialog box pop-up and waits for users input and i want the text to be corrected automatically. So my question is how do i solve this ?
using System.Collections.Generic;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using TobyCL.ro.toby.StringOperations;
namespace namespace.ro.toby
{
class WordProofing:IProof
{
private readonly Word.Application _wordApp;
private readonly Word.Document _wordDoc;
private static object _oEndOfDoc = "\\endofdoc";
public WordProofing()
{
_wordApp = new Word.Application {Visible = false};
_wordDoc = _wordApp.Documents.Add();
}
public void Close()
{
object obj = Word.WdSaveOptions.wdDoNotSaveChanges;
_wordDoc.Close(ref obj);
_wordApp.Quit(ref obj);
}
#region Implementation of IProof
public string Proof(string proofText)
{
Range wRng = _wordDoc.Bookmarks.get_Item(ref _oEndOfDoc).Range;
wRng.Text = proofText;
_wordDoc.CheckSpelling(IgnoreUppercase: true,AlwaysSuggest:false);
string str = wRng.Text;
wRng.Text = "";
return str;
}
#endregion
}
}
I wrote this code a few days ago and it worked. The problem is that i uninstall proofing tools to run some tests and now i keep getting that dialog so i'm thinking that may i have to set some Word settings or i've changed something in my code without knowing. Any help would be greatly appreciated.
I am using Microsoft Office Word 2010
For whoever might be interested this is the way i managed to solve it, but it really takes a lot of time so any improvements or new ideas are welcomed.
using Microsoft.Office.Interop.Word;
class WordProofing
{
private Application _wordApp;
private readonly Document _wordDoc;
private static object _oEndOfDoc = "\\endofdoc";
public WordProofing()
{
_wordApp = new Application { Visible = false };
_wordDoc = _wordApp.Documents.Add();
}
public void Close()
{
_wordDoc.Close(WdSaveOptions.wdDoNotSaveChanges);
_wordApp.Quit();
}
public string Proof(string proofText)
{
Range wRng = _wordDoc.Bookmarks.get_Item(ref _oEndOfDoc).Range;
wRng.Text = proofText;
ProofreadingErrors spellingErros = wRng.SpellingErrors;
foreach (Range spellingError in spellingErros)
{
SpellingSuggestions spellingSuggestions =
_wordApp.GetSpellingSuggestions(spellingError.Text,IgnoreUppercase:true);
foreach (SpellingSuggestion spellingSuggestion in spellingSuggestions)
{
spellingError.Text = spellingSuggestion.Name;
break;
}
}
string str = wRng.Text;
wRng.Text = "";
return str;
}
}
Which MS Word version are you using?
By default the spell checker will show you the dialog box. To disable the dialog box there are two ways that I know.
1) Using Code, automatically choose the first option from Auto Correct.
It is something like this
AutoCorrect.Entries.Add Name:="AdSAD", Value:="Assad"
2) Or use the menu option. Please refer to this link.
Topic: Automatically correct spelling with words from the main dictionary
Link: http://office.microsoft.com/en-us/word-help/automatically-correct-spelling-with-words-from-the-main-dictionary-HA010174790.aspx
Do let me know if this is not what you want?
Everywhere I look for how to use excel function inside of a C# application tells me to do the following:
From the COM TabAdd the following using statement:using IExcel = Microsoft.Office.Interop.Excel
Then declare and initialise a IWorkSheetFunction objectIExcel.IWorksheetFunction iwf = new IExcel.IWorksheetFunction();
Now you can use the functions as in int result = iwf.Math.Sum(1,2);
The problem im having is that while intellisense is detecting IExcel, it is not showing IWorksheetFunction. It is however showing WorksheetFunction. Either way, it is not letting me instantiate it as an object. I am getting the error: Cannot create an instance of the abstract class or interface 'Microsoft.Office.Interop.Excel.WorksheetFunction'
any ideas?
Try:
Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.WorksheetFunction wsf = xl.WorksheetFunction;
int result = wsf.Percentile(obj, 0.75);
Basically it comes down to, instead of:
IExcel.IWorksheetFunction iwf =
new IExcel.IWorksheetFunction(); // You can't instantiate an interface
use the WorksheetFunction property in Excel.Application:
IExcel.IWorksheetFunction iwf = xl.WorksheetFunction;
Its a function off the application object.
using Microsoft.Office.Interop.Excel;
static void Main(string[] args)
{
Application a = new Application();
double x = a.WorksheetFunction.Sum(1, 2);
Console.WriteLine("Sum = {0}", x);
}