tag:blogger.com,1999:blog-53449821841197053202024-03-13T03:10:01.337-07:00Greg ReddickGreg Reddick is a noted speaker, author, and software engineer. This blog covers all aspects of programming, particularly for Windows, and other related topics.Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.comBlogger101125tag:blogger.com,1999:blog-5344982184119705320.post-29915599696572590032024-02-09T20:19:00.000-08:002024-02-09T20:19:01.484-08:00Where Does Office Store the Most Recently Used List?<p>Where does Microsoft Office save the Most Recently Used list for Word, Excel, etc.? The answer is in a file under this directory:<br /></p><p>C:\Users\<i>UserName</i>\AppData\Local\Microsoft\Office\<i>VersionNumber</i>\MruServiceCache</p><p>There will be a directory under that that is randomly generated, then under that is the product name. Then under that is a file with the name Documents_en-US (or whatever the language). That file is a JSON file that has the list in it.<br /></p>Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-64043231864785687002022-11-07T14:29:00.000-08:002022-11-07T14:29:03.875-08:00The Correct Way to Handle Multiple Exceptions in C#<p>
This is just a quick article on the correct way to handle multiple exceptions
with the same code in C#. In older versions of C#, one way to handle multiple
exceptions was code like this:
</p>
<pre><code>
try
{
// Code that can fail
}
catch (IOException)
{
// Failure code A
}
catch (ArgumentException)
{
// Failure code A
}
</code></pre>
<p>
This violates this violates the DRY (Don't Repeat Yourself), concept with <i>Failure code A</i> being repeated. Yes, you can call a method (or even a lamda
function) from the failure code, but that creates code that is elsewhere for
something that should be handled here. Another attempt would be something like
this:
</p>
<pre><code>
try
{
// Code that can fail
}
catch (Exception ex)
{
if (ex is IOException || ex is ArgumentException)
{
// Failure code A
}
else
{
throw;
}
}
</code></pre>
<p>
This is better (and the right way in C# before version 6), but it is clumsy.
Incidentally, in this code, it would be important to use just <i>throw</i>,
not <i>throw ex</i> as C# does not treat them the same. Using just
<i>throw</i> preserves the stack trace so that the call stack shows the
origination of the exception, whereas <i>throw ex</i> shows the exception
originating in this code.
</p>
<p>Here is the right way to handle it with current versions of C#:</p>
<pre><code>
try
{
// Code that can fail
}
catch (Exception ex) when (ex is IOException or ArgumentException)
{
// Failure code A
}
</code></pre>
<p>
Incidentally, the convention in C# is to use "ex" as the variable for exceptions, as "e" is
used for the second argument in event handlers, and if you have exception
handling code in an event handler, you don't want them to conflict.<br />
</p>Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-69317869992325080822021-11-10T10:10:00.003-08:002021-11-23T12:01:13.714-08:00Excel Bug Changes Color on Cell Not in Range<p>
I'm in the process of writing an
<a href="https://docs.microsoft.com/en-us/office/dev/add-ins/">Excel add-in</a
>, using the relatively new mechanism of writing a TypeScript library. I had
previously written this Excel add-in in C# on .NET Framework 4.8, but this
mechanism doesn't work in .NET 5 or 6 for architectural reasons. (With a bunch
of tricks, I got Excel calling .NET 5 code configured as a COM DLL, but it
won't work if the user's machine is configured in some ways and I couldn't get
the add-in recognized correctly.) The TypeScript actually calls a web API that
then calls the C# on the server side, so this wasn't a rewrite from the ground
up but a layer thrown on top of my previous code, but Excel never sees that.
From Microsoft's perspective using JavaScript has the advantage that it is
portable, meaning that these add-ins can work on Windows, iOS, the web,
phones, or anywhere else that Microsoft can place Excel.
</p>
<p>
<a href="https://www.typescriptlang.org/">TypeScript</a> is a language that
adds strongly typed features to JavaScript. It compiles into JavaScript, so it
works anywhere that JavaScript does. I dislike weakly typed languages like
JavaScript, so TypeScript makes JavaScript acceptable.
</p>
<p>
So in the process of writing my add-in, I ran into a bug in the Excel
Javascript/Typescripts APIs. I formatted cells A6:A11 to have a blue
background, and oddly enough, cell A13 turned blue as well. I don't know what
the internals of the Javascript API or Excel look like, but this bug is really
strange, as it seems to be dependent on the data that is in a formatted cell.
In any case, I reduced the code to a small easily reproducible case, wrote it
up, and
<a
href="https://github.com/OfficeDev/office-js/issues/2237#issuecomment-964841898"
>submitted it to Microsoft</a
>. They confirmed the bug the same day I submitted it and have assigned it to
a developer.
</p>
<p>
More info: It turns out that this is not a Javscript API bug. It is a design
flaw in Excel. If you format the backcolor of three or more cells (doesn't
happen with two), say A6:A8, then change a cell within three cells, say A11,
it will also change color. This came back as BY DESIGN. What a stupid design.
It is unfathomable to me that this is what a user would want or expect.
</p>
<p>
Here is some equivalent VBA code (press Alt+F11, double-click on Sheet1,
insert the code, press F5 to run it):
<code>
<pre>
Public Sub ExcelBug()
Sheet1.Range("A1:A3").Interior.Color = vbBlue
Sheet1.Range("A1").Value = "a"
Sheet1.Range("A2").Value = "b"
Sheet1.Range("A3").Value = "c"
End Sub
</pre
>
</code>
Run the code. Now type anything into cell A5 and press enter. What do you
think...should the backcolor change to be blue? Well, it does. Now delete the
value and change the backcolor to be no fill. Add the following line just
before the End Sub:
<code>
<pre>
Sheet1.Range("A5").Value = "d"
</pre
>
</code>
Now run the code. A5 doesn't change to be blue. Now delete the value and type
it again. No blue. What are the rules on when it will change to be blue? Now
put the same data into
<a href="https://www.office.com/launch/excel?ui=en-US&rs=US&auth=1"
>Excel on the web</a
>. Will it be blue or not? Answer: not. Regardless of whether they claim it is
by design, this is a bug.
</p>
Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-14367650006815254972021-11-01T12:00:00.000-07:002021-11-01T15:41:19.524-07:00Microsoft Access Combo Box Search While Typing<p>If you start typing in a Microsoft Access combo box, it might not search as you type. I eventually figured out the trick to making the search work. The problem occurs when the combo box is based on a query. For the query to work, it must be a DISTINCT query. In other words, if you go to SQL View (select View SQL from the ribbon), and the query is:</p>
<code><pre>
SELECT tblCategory.pcCategoryId, tblCategory.strCategory
FROM tblCategory
ORDER BY tblCategory.strCategory;
</pre></code>
<p>Change it to:</p>
<code><pre>
SELECT DISTINCT tblCategory.pcCategoryId, tblCategory.strCategory
FROM tblCategory
ORDER BY tblCategory.strCategory;
</pre></code>
After that the search while typing should work.Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-67341339645705538762021-05-13T23:13:00.001-07:002021-05-13T23:13:59.583-07:00Displaying an Image in a Microsoft Access Image Control<p>If you have a column in a Microsoft Access table that has the name of an image that refers to a file on the disk, you can update an image control in either a form or report with the code below. Put this code into a standard VBA module that you create with Insert Module from the menu. This code assumes that all the images are in a subdirectory of the location of the database called "images" if there are relative paths to the image file.</p>
<pre>Public Const strImageFolder = "images"
Public Sub DisplayImage(ctlImageControl As Control, strImagePath As Variant)
On Error GoTo ErrorHandler
If IsNull(strImagePath) Then
ctlImageControl.Visible = False
Else
If InStr(1, strImagePath, "\") = 0 Then
strImagePath = Application.CurrentProject.Path & "\" & strImageFolder & "\" & strImagePath
End If
ctlImageControl.Visible = True
ctlImageControl.Picture = strImagePath
End If
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 2114 'Doesn't support the format of the file
ctlImageControl.Visible = False
Case 2220 ' Can't find the picture.
ctlImageControl.Visible = False
Case Else ' Some other error.
MsgBox "Unexpected Error #" & Err.Number & " " & Err.Description, vbExclamation, "Unexpected Error"
End Select
End Sub
</pre>
<p>To use this code in a form, if the control that contains the image name txtPicture and the image control is named imgPicture then add this code to the module for the form:</p>
<pre>Private Sub Form_AfterUpdate()
Call DisplayImage(Me!imgPicture, Me!txtPicture)
End Sub
Private Sub Form_Current()
Call DisplayImage(Me!imgPicture, Me!txtPicture)
End Sub
Private Sub txtPicture_AfterUpdate()
Call DisplayImage(Me!imgPicture, Me!txtPicture)
End Sub
</pre>
<p>To use it in a report, assuming there is a column in the table or query the report is based on called strPicture and an image control named imgPicture, add this code to the report:</p>
<pre>Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
Call DisplayImage(Me!imgPicture, Me!strPicture)
End Sub
</pre>Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-24448791161325809672021-05-13T07:13:00.003-07:002021-05-13T07:36:45.367-07:00Open a URL using VBA<p>This is just a quick tip on opening a hyperlink using the default browser in VBA. It uses the ShellExecute Windows API call. Call the OpenHyperlink function shown below with the URL that you want to open.
<pre>
Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hWnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long _
) As Long
Public Function OpenHyperlink(ByRef strHyperlink As String) As Long
OpenHyperlink = ShellExecute(0, "Open", strHyperlink, vbNullString, vbNullString, vbNormalFocus)
End Function
</pre>
<p>An example of calling it is:</p>
<pre>
Call OpenHyperlink("http://blog.xoc.net")
</pre>Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-17782462167823265442021-05-09T23:27:00.007-07:002021-05-13T23:14:38.495-07:00Extracting Microsoft Access OLE Object Field Items<p>I created a Microsoft Access database for a small set of data (<1000 rows). Access was a perfect database for this particular problem, allowing easy input and good reporting, and the Access accdb database format allows easy installation on different computer that have Access. However, I made a mistake in storing bitmaps in an OLE object column. Access still has a limit of two gigabytes for its native database format. Bitmaps don't compress and quickly consume all of that limit. I had entered several hundred items before running into that limit. The two gigabyte limit was reasonable in the 1990s when a 1 gigabyte drive cost over $1000 (I have a receipt!), but is ridiculously small by today's standard.</p><p>The VBA code below works on the table tblExample. It extracts the bitmap from an OLE Object column (olePicture) and writes it to a file on the disk. It then updates another column (strPicture) with the name of the file it wrote. The filename is constructed by the name of the primary key field (ID) followed by .bmp, thus ID of 1 becomes 1.bmp in the same directory as the database.</p><p>An OLE Object field has a Package Header, an OLE header, the actual data of the bitmap, some optional other stuff, and an OLE footer. The problem is that the headers are variable length with sizes embedded into them, so the actual bitmap has to be located within the data before it can be extracted. So this code extracts the sizes and skips to the appropriate place and extracts the data. It uses a helper function that constructs a long from the first four bytes of an array of bytes (although it will break if a size is over 2^31 as it would try to convert an unsigned count to a signed count, which should never happen here).</p><p>After running this code successfully (use the Windows File Explorer to view the bitmaps), the OLE Object column can be deleted. Other VBA code will be necessary to display the picture in the external file, which is beyond the scope of what I want to show here. The code is not very fast as it writes the file one byte at a time, but it should be a one-time thing, at least for my purpose. It also probably has some boundary conditions related to some kinds of OLE objects that break it under some conditions, but it worked for what I needed.<br /></p><pre>Option Compare Database
Option Explicit
Public Sub ExtractImages()
' Need a reference to the Microsoft ActiveX Data Objects 6.1 Library
Dim rst As ADODB.Recordset
Dim varByte As Variant
Dim i As Long
Dim lngLength As Long
Dim byteVal As Byte
Dim strFileName As String
Dim strFilePath As String
Set rst = New ADODB.Recordset
rst.Open "tblExample", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
Do While Not rst.EOF
If Not IsNull(rst.Fields.Item("olePicture").Value) Then
' Create the filename from the primary key ID field.
strFileName = rst.Fields.Item("ID").Value & ".bmp"
' Fill strPicture with the filename
rst.Fields.Item("strPicture").Value = strFileName
rst.Update
strFilePath = Application.CurrentProject.Path & "\" & strFileName
If Dir(strFilePath) = "" Then
' Read the package header the package header, the second byte is the size
varByte = rst.Fields.Item("olePicture").GetChunk(3)
'Extract the offset to the start of the OLE header
varByte = rst.Fields.Item("olePicture").GetChunk(varByte(2) + 5)
'Get the first four bytes which holds the OLE size
varByte = rst.Fields.Item("olePicture").GetChunk(4)
' Use to size to of the header to move to the end of the header
varByte = rst.Fields.Item("olePicture").GetChunk(GetLong(varByte))
' Skip the next eight bytes
varByte = rst.Fields.Item("olePicture").GetChunk(8)
' The next four bytes retrieves the size of the Bitmap
varByte = rst.Fields.Item("olePicture").GetChunk(4)
' Turn those bytes into a length
lngLength = GetLong(varByte)
' Get the bitmap
varByte = rst.Fields.Item("olePicture").GetChunk(lngLength)
' Write the bitmap to the file
Open strFilePath For Binary As #1
For i = 0 To lngLength - 1
byteVal = varByte(i)
Put #1, , byteVal
Next i
Close #1
End If
End If
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
MsgBox "Done"
End Sub
Public Function GetLong(ByRef varByte As Variant) As Long
' Convert the first four bytes of varByte into a long
Dim i As Long
Dim lngResult As Long
For i = 3 To 0 Step -1
lngResult = lngResult * 256 + varByte(i)
Next i
GetLong = lngResult
End Function
</pre>Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-72232063264715500492021-03-22T07:13:00.005-07:002021-03-22T09:53:54.122-07:00What Does the "Home" Key Do in Microsoft Editors?<p>I want to give a little anecdote on how I changed how people program. It was some time around 1990, maybe plus or minus a year. I was working on the Microsoft Access programming team at Microsoft. Actually they still hadn't settled on a name yet and eventually stole the name of a communications product Microsoft had shipped earlier and re-used it for their database, but I digress. I spent the majority of my hours in the buildings at Microsoft. My friends also worked at Microsoft, so when I wasn't working, I'd frequently head over to their office and just hang out. We all worked long hours.<br /></p><p>One of my friends there was a programmer named Richard. Richard ostensibly worked on the database engine side of Microsoft Access, whereas I was on the user-interface side, but Richard had kind of "god-level" access to all of the code across Microsoft's Application Division. Anything that wasn't part of the operating system at Microsoft, he was authorized and trusted to go in and change.</p><p>I was sitting in Richard's office one night, and he said there had been a debate on what the "Home" key should do in Microsoft's editors. One camp said it should go to the beginning of the line. The other camp said it should go to the first non-whitespace character on the line, after any spaces or tabs. He asked, "what do you think it should do?" I said, "what if the first time you pressed it, it went to the first non-whitespace character, but if you were already there, it went to the start of the line?" He said, "that's a good idea!" And as I sat there, he went in and changed the code in Visual Studio and the Visual Basic editors. Try it, because it still does that today.<br /></p><p>Now this was kind-of the dark ages in software development. Microsoft was just putting its first usability testing groups together, so today you'd probably do some usability tests to see if that really worked for people. Or at least have a meeting about it. But I just happened to be in the right place at the right time, with the right programmer with the right access and skills. I got asked the question on what I thought it should do and everyone else gets to live with it for all time.<br /></p>Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-8617277190590143322021-02-12T23:34:00.003-08:002021-11-10T10:16:58.501-08:00Calling a .NET 5 (.NET Core) COM DLL from Microsoft Office (Excel, Word, Outlook, etc.)<p>
I have finally got a .NET 5 DLL written in C# able to be called from Microsoft
Office! This process actually works for any application to call the DLL
through COM.This has been a bear to figure out. It is not easy, but it can be
done. There are some mentions of the process on the web, but I have seen no
examples. I am going to post the code on GitHub with a sample project, and the
full description over there.
</p>
<p>
So before launching into the process, here is my most basic recommendation. If
possible, for the time being don't use .NET 5 or .NET Core at all. Use the
.NET Framework 4.x, because it does much of the work for you. For my current
project, that wasn't possible, because a library it needed to call was only
going to be maintained on .NET 5, as it was also being called from at .NET 5
front end. This recommendation will likely change as the .NET core does the
work of providing a type library in the comhost file they create in some
future release.<br />
</p>
<p>
But here is the basic process: 1) Create a project with a class and interface.
2) Decorate them with appropriate attributes. 3) Add the appropriate method
and the commands to create the correct registry entries to the project. 4) Add
the appropriate settings to project file. 5) Build the DLL. 6) Create a
parallel IDL file that describes the interface. 7) Compile it with the MIDL
compiler. 8) Register the DLL. 9) In office, add a reference to the DLL.
</p>
<p>
There are various gotchas in this process. One of the things that is tricky is
making sure that your DLL, the comhost wrapper, and the version of Microsoft
Office you are calling it from all have the same bitness (32 bits or 64 bits).
</p>
<p>
So given all that, you can find the test project with more detailed
documentation here:
<a
href="https://github.com/GregReddick/ComTestLibrary/tree/master/ComTestLibrary1"
>https://github.com/GregReddick/ComTestLibrary/tree/master/ComTestLibrary1</a
>
</p>
Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-4532545505295833172021-01-24T14:14:00.003-08:002021-01-25T11:00:45.287-08:00Solving Mastermind<p>A question was posted online recently about the best strategy for winning the game of <a href="https://en.wikipedia.org/wiki/Mastermind_(board_game)">Mastermind</a>. Mastermind is a game played between two players, a Code Maker and a Code Breaker. The Maker makes a code of colored pegs, and the Breaker has to guess the code. After each guess, the Maker gives feedback of how many of the pegs were the right color in the right place and how many are the right color in the wrong place, indicated by black and white pegs in the board. The Breaker then makes another guess.</p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://upload.wikimedia.org/wikipedia/commons/2/2d/Mastermind.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="637" data-original-width="600" height="197" src="https://upload.wikimedia.org/wikipedia/commons/2/2d/Mastermind.jpg" width="185" /></a></div>The parameters of the game are how many possible colors there are, how many pegs are in the code, and whether the same color is allowed to be repeated in the code, as in (4 of 6, repeats).<br /><p></p><p>Donald Knuth <a href="http://www.cs.uni.edu/~wallingf/teaching/cs3530/resources/knuth-mastermind.pdf">wrote a paper</a> on optimal play for the Breaker and showed that in four pegs in the code of six possible colors with repeats, it can be solved in no more than five tries. Donald Knuth is a deity of Computer Science, having written <i><a href="https://en.wikipedia.org/wiki/The_Art_of_Computer_Programming">The Art of Computer Programming</a></i>. I wrote a program to implement Knuth's algorithm in C#. It also creates a table at the end of how to make perfect play.</p><p>In my program, I replace colors with numerals since the colors are arbitrary. I have <a href="https://github.com/GregReddick/Mastermind">placed the code on GitHub</a>. You can try the suggested algorithm on <a href="https://www.onlinespiele-sammlung.de/mastermind/mastermindgames/madglibs/index.html">this site</a>.</p><p>The program uses a <a href="https://en.wikipedia.org/wiki/Minimax">MinMax algorithm</a>, which finds the code that will reduce the number of possible remaining codes on each play. Because of the way it works, sometimes it will make a code that might not actually solve it on the next play, but instead guarantee that it solves it in the least number of tries. There are some other algorithms that will solve it in a smaller average number of tries, but possibly having a larger maximum.<br /></p>Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-37067627709621696412020-06-17T12:35:00.001-07:002020-06-17T12:49:32.010-07:00Creating a Break Timer in PowerPoint using VBA<div>When I teach live classes, I use the <a href="https://docs.microsoft.com/en-us/sysinternals/downloads/zoomit">SysInternals Zoomit</a> application, which has a break timer built in. However, I was teaching a online class, and Zoomit did not seem to get along with WebEx. I decided to write a break timer directly into the PowerPoint slides I was using.</div><div><br /></div><div>The first step is to create a slide at the end of the presentation that looks like this:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-7zkU6ADoISQ/XuprxUlcAhI/AAAAAAAAFvU/8t5olGBCEVMX0KMXVpjAUT8SZ3P4ZlxxgCK4BGAsYHg/s3851/PowerPoint1.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="2136" data-original-width="3851" src="https://1.bp.blogspot.com/-7zkU6ADoISQ/XuprxUlcAhI/AAAAAAAAFvU/8t5olGBCEVMX0KMXVpjAUT8SZ3P4ZlxxgCK4BGAsYHg/s320/PowerPoint1.jpg" width="320" /></a></div><div><br /></div><div>In other words, it is a standard slide with a title at the top and bullet points section below. I centered both and removed the bullet, so it just had text on the time. The code below counts on this slide as being the last in the presentation.</div><div><br /></div><div>Next I brought up the PowerPoint Visual Basic Editor. You can do this with Alt+F11. Insert a module with Insert > Module from the menu. In the module, add this VBA code:</div><div><br /></div><div>Option Explicit<br /><br />Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)<br />Private lngPreviousSlide As Long<br />Private boolEndBreak As Boolean<br /><br />Public Sub BreakTimer()<br /> Dim dtmStart As Date<br /> Dim dtmEnd As Date<br /> Dim slidesCollection As slides<br /> Dim slideBreak As slide<br /> Dim lngCurrentSlide As Long<br /> <br /> dtmStart = Now<br /> dtmEnd = DateAdd("n", 10, dtmStart)<br /> <br /> Set slidesCollection = Application.ActivePresentation.slides<br /> Set slideBreak = slidesCollection(slidesCollection.Count)<br /> <br /> lngCurrentSlide = SlideShowWindows(1).View.slide.SlideIndex<br /> If lngCurrentSlide = slidesCollection.Count Then<br /> 'On the break slide, end the break early<br /> boolEndBreak = True<br /> Else<br /> ' Go on break<br /> lngPreviousSlide = lngCurrentSlide<br /> boolEndBreak = False<br /> SlideShowWindows(1).View.GotoSlide slidesCollection.Count, msoTrue<br /> DoEvents<br /> Do Until (Now > dtmEnd) Or boolEndBreak<br /> slideBreak.Shapes(2).TextFrame.TextRange.Text = Format(dtmEnd - Now, "n:ss")<br /> Sleep 900<br /> DoEvents<br /> Loop<br /> SlideShowWindows(1).View.GotoSlide lngPreviousSlide, msoFalse<br /> End If<br />End Sub</div><div><br /></div><div>When this code run, it remembers the current slide, changes the code to the last slide, and starts a 10 minute countdown (Change the 10 in the DateAdd function to another number to do a different number of minutes in your break).</div><div><br /></div><div>I then went to the master slide (View > Slide Master) and added a small button in the lower right hand corner. To add a button, you need to have the Developer ribbon turned on. Use File > Options > Customize Ribbon and check the checkbox next to Developer in the dialog and press OK. Then switch to your Developer ribbon.<br /></div><div><br /></div><div>On the Developer ribbon, Click the Command Button icon, then draw the button on the master slide. Then click the Properties button on the ribbon. Set the name of the button to cmdBreak, and select a clock type image file in the Picture property by hitting the ... button on the right. Then double-click on the button you just created. This creates an Event Handler for the button. In the Event Handler, add this code:</div><div><br /></div><div>Option Explicit<br /><br />Private Sub cmdBreak_Click()<br /> Call BreakTimer<br />End Sub</div><div><br /></div><div>Close the Master slide. Run your presentation. Whenever you want to call a break, click the button in the lower right of the current slide. It will jump to your break slide and start counting down. At the end of the break, it will jump back to the slide it was on. If you want to end the break early, on the break slide, click the break button and it will end it (the code is re-entrant, so it can be processing and the button is hit again, which executes it a second time while the first instance is still running).<br /></div>Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-72308694206642384892019-05-01T10:40:00.000-07:002019-05-01T10:40:12.425-07:00Visual Studio 2019 Community has CodeLensI have been asking for many years in this blog that Visual Studio get CodeLens on the cheaper versions.of Visual Studio. CodeLens shows the number of references to piece of code immediately below the method first line, and quickly allows getting to those references. It is actually configurable, so it can show other information as well, but the reference count is the default.<br />
<br />
When Microsoft first made CodeLens available, it was only available on the $12000 version of Visual Studio. It has finally made its way into the Community Edition as of the recent release of Visual Studio 2019. The Community Edition is the version available for free for very small companies, educational, and personal use.<br />
<br />
Now if they would make Code Coverage available on the Community Edition, it would make me happy. Code Coverage allows seeing what code has been hit by test suites and where additional tests need to be written. There is the free AxoCover that does pretty well, but having the Microsoft version available would be better. This is my biggest missing feature in the Visual Studio that I use.Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-66290509750226501282019-01-30T23:58:00.000-08:002019-02-01T13:59:55.815-08:00How to Delete an Excessively Long Directory on NTFSThe current version of Windows has some throwbacks to the days of the DOS operating system. Back in the day, DOS had a limit on filenames that they could not be more than 260 or so characters long (there was a little fudging between the limit on directories and filenames, but let's just call it 260).<br />
<br />
The current NTFS file system doesn't have that 260 character limit. However, many of the tools that talk to NTFS, like the Windows Explorer and the command line still do have the limit. So if you have a tool that doesn't have the limit, it can create a directory that you cannot delete from the Windows Explorer or the command line. Arg!<br />
<br />
The solution is to make the entire directory path shorter than 260 characters, then you can delete the directory. Go to Windows explorer, and drill down into your excessively long directory path until you cannot go any deeper. Then drag that deepest directory you can reach and move it to be a subdirectory of the root (or somewhere much further up the directory tree if the name conflicts). Delete the directory path that you dragged from, which should now be short enough to delete. Then drill down in your new path from the root...you should be able to reach further down than you could before because the limit only applies from the root. Repeat again and again until you can get to the bottom of the tree.<br />
<br />
As an additional note: The most common reason for the long directory name problem is that Windows has a bug where it creates a recursive reparsepoint for a directory called "Application Data". This puts the directory as a subdirectory of itself. This means that you can into this subdirectory forever. So if you use a tool like robocopy to copy the directory tree, it will drill down until it hits the NTFS limit on the directory name (much longer than 260) and keep copying the directory. Then you can't delete the directory it copied. To remove a reparsepoint, you can do the following commands from a command prompt:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">cd "\users\myacct\appdata\local\application data"</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">fsutil reparsepoint delete "application data"</span>Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-40091797572350475772018-08-30T11:14:00.000-07:002018-08-30T12:00:27.648-07:00Using the New Features in the Latest Versions of C#The current version of Visual Studio 2017 (15.8.2 the day this is posted) actually supports C# version 7.3. You can see the new features by looking at the <a href="https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/" target="_blank">C# feature list</a>. However, by default, Visual Studio will use C# version 7.0. To use versions after 7.0, you will need to go to the project properties, select Build, then click the Advanced button. In the dialog is a setting for Language Version. Changing this to 7.3, for example, will enable the latest features.<br />
<br />
You can use this same setting for turning off features. If you don't like the stuff they added to C# version 7, you can go back to 6, or even back to 3. They have been pretty good, however, at not screwing up the language with features added in later versions. I can't think of a feature where I went, "I wish they didn't put that in the language." I think lambda expressions are overused by a lot of people, but there are places where they are appropriate. I also use "var" as little as possible, but there are places where var is necessary and useful. The usage of these features is a coding style issue, not a problem with the language itself.<br />
<br />
You can see the features that might be coming in future versions of C# at <a href="https://github.com/dotnet/roslyn/blob/master/docs/Language%20Feature%20Status.md" target="_blank">this page</a>. The biggest feature that is being discussed is <a href="https://github.com/dotnet/csharplang/blob/master/proposals/nullable-reference-types.md" target="_blank">non-nullable reference types</a>. With these, you can specify that a specific reference type cannot ever be null. This will likely change how a lot of C# code gets written.Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-46984334642648363102018-06-10T01:01:00.000-07:002018-06-10T20:12:12.268-07:00Using UserControls with Caliburn.MicroIt is common to want to create a reusable UserControl, to be placed into a WPF (Windows Presentation Foundation) screen. This can be done one of two ways:<br />
<ul>
<li>ViewModel First</li>
<li>View First</li>
</ul>
The techniques below will show how to do both of these schemes using Caliburn.Micro to perform the plumbing to connect them up. It took me quite a bit of research to figure out how to make these happen, particularly the View first, scheme. Both of these techniques can be used to create a UserControl in a Window that was itself generated using the other technique. For example, a window that was created using the ViewModel First scheme can include a UserControl that is created using the View First scheme.<br />
<br />
In the example code below, the main window View is called MainWindowView and has a ViewModel called MainWindowViewModel. The ViewModel First control has a ViewModel called ViewModelFirstTestControlViewModel, which is displayed with the View called ViewModelFirstTestControlView. The View First control has a View called ViewFirstTestControlView and has a ViewModel called ViewFirstTestControlViewModel.<br />
<br />
The ViewModel first scheme places a ContentControl into the MainWindowView, with a x:Name attribute. For example:<br />
<br />
<pre style="background: white; color: black; font-family: Consolas; font-size: 13;"><span style="color: blue;"><</span><span style="color: #a31515;">ContentControl</span>
<span style="color: red;">x</span><span style="color: blue;">:</span><span style="color: red;">Name</span><span style="color: blue;">=</span><span style="color: blue;">"ViewModelFirstTestControlViewModel"</span><span style="color: blue;"> /></span></pre>
<br />
The MainWindowViewModel then has this code:<br />
<br />
<pre style="background: white; color: black; font-family: Consolas; font-size: 13;"><span style="color: blue;">namespace</span> TestSystem.ViewModels
{
<span style="color: blue;">using</span> Caliburn.Micro;
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span><span style="color: green;">A ViewModel for the main window.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">seealso</span><span style="color: grey;"> cref</span><span style="color: grey;">=</span><span style="color: grey;">"</span><span style="color: grey;">T:Caliburn.Micro.PropertyChangedBase</span><span style="color: grey;">"</span><span style="color: grey;">/></span>
<span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">MainWindowViewModel</span> : <span style="color: #2b91af;">PropertyChangedBase</span>
{
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span><span style="color: green;">Initializes a new instance of the </span><span style="color: grey;"><</span><span style="color: grey;">see</span><span style="color: grey;"> cref</span><span style="color: grey;">=</span><span style="color: grey;">"</span><span style="color: #2b91af;">MainWindowViewModel</span><span style="color: grey;">"</span><span style="color: grey;">/></span><span style="color: green;"> class.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: blue;">public</span> MainWindowViewModel()
{
<span style="color: blue;">this</span>.ViewModelFirstTestControlViewModel = <span style="color: blue;">new</span> <span style="color: #2b91af;">ViewModelFirstTestControlViewModel</span>(<span style="color: #a31515;">"ViewModel First Set Content"</span>);
}
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span><span style="color: green;">Gets the ViewModelFirst test control view model.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">value</span><span style="color: grey;">></span><span style="color: green;">The ViewModelFirst test control view model.</span><span style="color: grey;"></</span><span style="color: grey;">value</span><span style="color: grey;">></span>
<span style="color: blue;">public</span> <span style="color: #2b91af;">ViewModelFirstTestControlViewModel</span> ViewModelFirstTestControlViewModel
{
<span style="color: blue;">get</span>;
<span style="color: blue;">private</span> <span style="color: blue;">set</span>;
}
}
}</pre>
<br />
So the constructor of the MainWindowViewModel instantiates the ViewModel of the UserControl, passing any arguments to initialize the values in the control. A property with the same name as the x:Name of the ContentControl exposes that ViewModel to the ContentControl. When the ContentControl needs to display the ViewModel, Caliburn.Micro finds the appropriate View and displays that as the content of the ContentControl.<br />
<br />
The content of the actual UserControl View in this example looks like this, but could be virtually anything you want:<br />
<br />
<br />
<pre style="background: white; color: black; font-family: Consolas; font-size: 13;"><span style="color: blue;"><</span><span style="color: #a31515;">UserControl</span>
<span style="color: red;"> x</span><span style="color: blue;">:</span><span style="color: red;">Class</span><span style="color: blue;">=</span><span style="color: blue;">"TestSystem.Views.ViewModelFirstTestControlView"</span>
<span style="color: red;"> xmlns</span><span style="color: blue;">=</span><span style="color: blue;">"http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span>
<span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">x</span><span style="color: blue;">=</span><span style="color: blue;">"http://schemas.microsoft.com/winfx/2006/xaml"</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: #a31515;">StackPanel</span>
<span style="color: blue;"><</span><span style="color: #a31515;">TextBlock</span>
<span style="color: red;"> Text</span><span style="color: blue;">="{</span><span style="color: #a31515;">Binding</span><span style="color: red;"> Path</span><span style="color: blue;">=</span><span style="color: blue;">Caption</span><span style="color: blue;">}</span><span style="color: blue;">"</span><span style="color: blue;"> /></span>
<span style="color: blue;"></</span><span style="color: #a31515;">StackPanel</span><span style="color: blue;">></span>
<span style="color: blue;"></</span><span style="color: #a31515;">UserControl</span><span style="color: blue;">></span></pre>
<br />
The ViewModel for the control in this example look like this:<br />
<br />
<pre style="background: white; color: black; font-family: Consolas; font-size: 13;"><span style="color: blue;">namespace</span> TestSystem.ViewModels
{
<span style="color: blue;">using</span> Caliburn.Micro;
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span><span style="color: green;">A ViewModel for the ViewModelFirst test control. This class cannot be inherited.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">seealso</span><span style="color: grey;"> cref</span><span style="color: grey;">=</span><span style="color: grey;">"</span><span style="color: grey;">T:Caliburn.Micro.PropertyChangedBase</span><span style="color: grey;">"</span><span style="color: grey;">/></span>
<span style="color: blue;">public</span> <span style="color: blue;">sealed</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ViewModelFirstTestControlViewModel</span> : <span style="color: #2b91af;">PropertyChangedBase</span>
{
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span><span style="color: green;">The caption.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: blue;">private</span> <span style="color: blue;">string</span> caption = <span style="color: #a31515;">"Default ViewModel first caption"</span>;
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: grey;">///</span><span style="color: green;"> Initializes a new instance of the </span><span style="color: grey;"><</span><span style="color: grey;">see</span><span style="color: grey;"> cref</span><span style="color: grey;">=</span><span style="color: grey;">"</span><span style="color: #2b91af;">ViewModelFirstTestControlViewModel</span><span style="color: grey;">"</span><span style="color: grey;">/></span><span style="color: green;"> class.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: blue;">public</span> ViewModelFirstTestControlViewModel()
{
}
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: grey;">///</span><span style="color: green;"> Initializes a new instance of the </span><span style="color: grey;"><</span><span style="color: grey;">see</span><span style="color: grey;"> cref</span><span style="color: grey;">=</span><span style="color: grey;">"</span><span style="color: #2b91af;">ViewModelFirstTestControlViewModel</span><span style="color: grey;">"</span><span style="color: grey;">/></span><span style="color: green;"> class.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">param</span><span style="color: grey;"> name</span><span style="color: grey;">=</span><span style="color: grey;">"</span>caption<span style="color: grey;">"</span><span style="color: grey;">></span><span style="color: green;">The caption.</span><span style="color: grey;"></</span><span style="color: grey;">param</span><span style="color: grey;">></span>
<span style="color: blue;">public</span> ViewModelFirstTestControlViewModel(<span style="color: blue;">string</span> caption)
{
<span style="color: blue;">this</span>.caption = caption;
}
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span><span style="color: green;">Gets or sets the caption.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">value</span><span style="color: grey;">></span><span style="color: green;">The caption.</span><span style="color: grey;"></</span><span style="color: grey;">value</span><span style="color: grey;">></span>
<span style="color: blue;">public</span> <span style="color: blue;">string</span> Caption
{
<span style="color: blue;">get</span>
{
<span style="color: blue;">return</span> <span style="color: blue;">this</span>.caption;
}
<span style="color: blue;">set</span>
{
<span style="color: blue;">if</span> (<span style="color: blue;">value</span> != <span style="color: blue;">this</span>.caption)
{
<span style="color: blue;">this</span>.caption = <span style="color: blue;">value</span>;
<span style="color: blue;">this</span>.NotifyOfPropertyChange(() => <span style="color: blue;">this</span>.Caption);
}
}
}
}
}</pre>
<br />
The main point about the code is that there is a constructor that takes any initial values to be set for the control. You may not actually need the default constructor.<br />
<br />
Now, let's examine how to do virtually the same thing, but do it View First. In the MainWindowView, there is this code to place the control into the View:<br />
<br />
<pre style="background: white; color: black; font-family: Consolas; font-size: 13;"><span style="color: blue;"><</span><span style="color: #a31515;">ctl</span><span style="color: blue;">:</span><span style="color: #a31515;">ViewFirstTestControlView</span>
<span style="color: red;"> cm</span><span style="color: blue;">:</span><span style="color: red;">Bind.Model</span><span style="color: blue;">=</span><span style="color: blue;">"TestSystem.ViewModels.ViewFirstTestControlViewModel"</span>
<span style="color: red;"> Caption</span><span style="color: blue;">=</span><span style="color: blue;">"View First Set Content"</span><span style="color: blue;"> /></span></pre>
<br />
For this Xaml to work, two namespace must be defined:<br />
<br />
<pre style="background: white; color: black; font-family: Consolas; font-size: 13;"><span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">cm</span><span style="color: blue;">=</span><span style="color: blue;">"http://www.caliburnproject.org"</span>
<span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">ctl</span><span style="color: blue;">=</span><span style="color: blue;">"clr-namespace:TestSystem.Views"</span> </pre>
<br />
The cm namespace comes from the Caliburn.Micro project. Many people use "cal" instead of "cm", but I've got a namespace for "calendrics" in some of my projects, so use cm instead. The "ctl" namespace is where your views reside.<br />
<br />
The cm:Bind.Model specifies the ViewModel for the control. The Caption passes in the initial value of the control.<br />
<br />
This retrieves the View for the control. The View looks very similar the the ViewModel First View, with some additions:<br />
<br />
<pre style="background: white; color: black; font-family: Consolas; font-size: 13;"><span style="color: blue;"><</span><span style="color: #a31515;">UserControl</span>
<span style="color: red;"> x</span><span style="color: blue;">:</span><span style="color: red;">Class</span><span style="color: blue;">=</span><span style="color: blue;">"TestSystem.Views.ViewFirstTestControlView"</span>
<span style="color: red;"> xmlns</span><span style="color: blue;">=</span><span style="color: blue;">"http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span>
<span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">vm</span><span style="color: blue;">=</span><span style="color: blue;">"clr-namespace:TestSystem.ViewModels"</span>
<span style="color: red;"> xmlns</span><span style="color: blue;">:</span><span style="color: red;">x</span><span style="color: blue;">=</span><span style="color: blue;">"http://schemas.microsoft.com/winfx/2006/xaml"</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: #a31515;">UserControl.Resources</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: #a31515;">vm</span><span style="color: blue;">:</span><span style="color: #a31515;">ViewFirstTestControlViewModel</span>
<span style="color: red;"> x</span><span style="color: blue;">:</span><span style="color: red;">Key</span><span style="color: blue;">=</span><span style="color: blue;">"ViewFirstTestControlViewModel"</span><span style="color: blue;"> /></span>
<span style="color: blue;"></</span><span style="color: #a31515;">UserControl.Resources</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: #a31515;">StackPanel</span>
<span style="color: red;"> x</span><span style="color: blue;">:</span><span style="color: red;">Name</span><span style="color: blue;">=</span><span style="color: blue;">"root"</span>
<span style="color: red;"> DataContext</span><span style="color: blue;">="{</span><span style="color: #a31515;">StaticResource</span><span style="color: red;"> ViewFirstTestControlViewModel</span><span style="color: blue;">}</span><span style="color: blue;">"</span><span style="color: blue;">></span>
<span style="color: blue;"><</span><span style="color: #a31515;">TextBlock</span>
<span style="color: red;"> Text</span><span style="color: blue;">="{</span><span style="color: #a31515;">Binding</span><span style="color: red;"> Path</span><span style="color: blue;">=</span><span style="color: blue;">Caption</span><span style="color: blue;">}</span><span style="color: blue;">"</span><span style="color: blue;"> /></span>
<span style="color: blue;"></</span><span style="color: #a31515;">StackPanel</span><span style="color: blue;">></span>
<span style="color: blue;"></</span><span style="color: #a31515;">UserControl</span><span style="color: blue;">></span></pre>
<br />
<br />
The additions specify the ViewModel for the control as a resource and binds the DataContext of the first child control to that ViewModel. However, with View First, the thing you can't avoid is having code behind. The code behind for the UserControl looks like this:<br />
<br />
<pre style="background: white; color: black; font-family: Consolas; font-size: 13;"><span style="color: blue;">namespace</span> TestSystem.Views
{
<span style="color: blue;">using</span> System.Windows.Controls;
<span style="color: blue;">using</span> TestSystem.ViewModels;
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span><span style="color: green;">A view first test control view.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">seealso</span><span style="color: grey;"> cref</span><span style="color: grey;">=</span><span style="color: grey;">"</span><span style="color: grey;">T:System.Windows.Controls.UserControl</span><span style="color: grey;">"</span><span style="color: grey;">/></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">seealso</span><span style="color: grey;"> cref</span><span style="color: grey;">=</span><span style="color: grey;">"</span><span style="color: grey;">T:System.Windows.Markup.IComponentConnector</span><span style="color: grey;">"</span><span style="color: grey;">/></span>
<span style="color: blue;">public</span> <span style="color: blue;">partial</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ViewFirstTestControlView</span> : <span style="color: #2b91af;">UserControl</span>
{
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span><span style="color: green;">The view model.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: blue;">private</span> <span style="color: #2b91af;">ViewFirstTestControlViewModel</span> vm;
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span><span style="color: green;">Initializes a new instance of the </span><span style="color: grey;"><</span><span style="color: grey;">see</span><span style="color: grey;"> cref</span><span style="color: grey;">=</span><span style="color: grey;">"</span><span style="color: #2b91af;">ViewFirstTestControlView</span><span style="color: grey;">"</span><span style="color: grey;">/></span><span style="color: green;"> class.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: blue;">public</span> ViewFirstTestControlView()
{
<span style="color: blue;">this</span>.InitializeComponent();
<span style="color: blue;">this</span>.vm = (<span style="color: #2b91af;">ViewFirstTestControlViewModel</span>)<span style="color: blue;">this</span>.root.DataContext;
}
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span><span style="color: green;">Gets or sets the caption.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">value</span><span style="color: grey;">></span><span style="color: green;">The caption.</span><span style="color: grey;"></</span><span style="color: grey;">value</span><span style="color: grey;">></span>
<span style="color: blue;">public</span> <span style="color: blue;">string</span> Caption
{
<span style="color: blue;">get</span>
{
<span style="color: blue;">return</span> <span style="color: blue;">this</span>.vm.Caption;
}
<span style="color: blue;">set</span>
{
<span style="color: blue;">this</span>.vm.Caption = <span style="color: blue;">value</span>;
}
}
}
}</pre>
<br />
The code behind does the InitializeComponent(), then sets the ViewModel to the DataContext that was set in the view. This, in turn, is used to have the property of the control talk to the ViewModel. The ViewModel of the control looks like this:<br />
<br />
<pre style="background: white; color: black; font-family: Consolas; font-size: 13;"><span style="color: blue;">namespace</span> TestSystem.ViewModels
{
<span style="color: blue;">using</span> Caliburn.Micro;
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span><span style="color: green;">A ViewModel for the ViewFirst test control. This class cannot be inherited.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">seealso</span><span style="color: grey;"> cref</span><span style="color: grey;">=</span><span style="color: grey;">"</span><span style="color: grey;">T:Caliburn.Micro.PropertyChangedBase</span><span style="color: grey;">"</span><span style="color: grey;">/></span>
<span style="color: blue;">public</span> <span style="color: blue;">sealed</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">ViewFirstTestControlViewModel</span> : <span style="color: #2b91af;">PropertyChangedBase</span>
{
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span><span style="color: green;">The text.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: blue;">private</span> <span style="color: blue;">string</span> caption = <span style="color: #a31515;">"Default View first caption"</span>;
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">summary</span><span style="color: grey;">></span><span style="color: green;">Gets or sets the text.</span><span style="color: grey;"></</span><span style="color: grey;">summary</span><span style="color: grey;">></span>
<span style="color: grey;">///</span><span style="color: green;"> </span><span style="color: grey;"><</span><span style="color: grey;">value</span><span style="color: grey;">></span><span style="color: green;">The text.</span><span style="color: grey;"></</span><span style="color: grey;">value</span><span style="color: grey;">></span>
<span style="color: blue;">public</span> <span style="color: blue;">string</span> Caption
{
<span style="color: blue;">get</span>
{
<span style="color: blue;">return</span> <span style="color: blue;">this</span>.caption;
}
<span style="color: blue;">set</span>
{
<span style="color: blue;">if</span> (<span style="color: blue;">value</span> != <span style="color: blue;">this</span>.caption)
{
<span style="color: blue;">this</span>.caption = <span style="color: blue;">value</span>;
<span style="color: blue;">this</span>.NotifyOfPropertyChange(() => <span style="color: blue;">this</span>.Caption);
}
}
}
}
}</pre>
<br />
This is almost the same as the ViewModel of the ViewModel First control, except it does not need the constructors, since the property is changed from the MainWindowView. (It has a default constructor that does nothing.)<br />
<br />
A zip file for the entire project is found <a href="http://www.xoc.net/downloads/TestSystem.zip" target="_blank">here</a>. Included are all the files, including the Caliburn.Micro bootstrapper that sets up the files.<br />
<br />
If you know of more efficient ways of doing any of the things I've described, please let me know in the comments.Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com1tag:blogger.com,1999:blog-5344982184119705320.post-81543464654430479412018-01-23T23:09:00.001-08:002018-01-24T00:59:41.597-08:00Compiling Help File as Part of Visual Studio SolutionI think that Microsoft has dropped the ball on creating help files. The technology has not changed in about 25 years, and was never simple in the first place. The tools are primitive. Furthermore, there is no simple way to incorporate the building of the help file into a Visual Studio solution.<br />
<br />
Building a help file is pretty much the same as building a web site. The pages are authored in HTML. The only difference is that there are some supplemental files that tell it how to build the Table of Contents (.hhc file) and the Index (.hhk file) to the help file, as well as a file to tell it what all the all the other files and provide the settings (.hhp file). There is a compiler that compiles the web site into a single .chm file.<br />
<br />
To start with, let's go over the tools needed to build a help file. You need the <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms669985(v=vs.85).aspx" target="_blank">Microsoft HTML Help Workshop</a>. This provides the help compiler (hhc.exe), as well as a rudimentary Windows application for managing the files (hhw.exe). The content files are HTML. If you know HTML well, you can create them in any text editor. Despite knowing HTML backwards and forwards, I still prefer to edit them in an interface that understands HTML as it allows me to reformat the HTML and other features. Microsoft produced a tool for editing HTML that they have since abandoned called <a href="https://www.microsoft.com/en-us/download/details.aspx?id=36179" target="_blank">Microsoft Expression Web</a>. You can download it for free from the Microsoft web site.<br />
<br />
Visual Studio does not have a template that works with help file projects. So we have to kind of fake it out. Create a console application that will act as the help file project. The console application does not need to do anything, as we will be ignoring the compiled executable, and instead using the build events for the project to accomplish what we need.<br />
<br />
Use hhw.exe to create the help project. Add HTML files to the
project. The stuff below assumes that the name of the .hhp file is
HelpProject.hhp, but you can rename it to anything else by making the appropriate changes below. The HelpProject.hhp should be added to the root of the
help project.<br />
<br />
Then add compiling the help file to the build events for the console application. However, we have to work around one minor problem: the help compiler returns one on success and zero on failure, rather than the Windows standard of the other way around. Visual Studio considers a build event that returns something non-zero as a failure and terminates the build of the project. To reverse that, we need a short batch file. Add to the help project a file named helpcompiler.bat file that looks like this:<br />
<pre><code>
"%ProgramFiles(x86)%\HTML Help Workshop\hhc.exe" %1
if not errorlevel 1 exit /B 1</code></pre>
<br />
Then add this to the pre-build event in the Project Properties:<br />
<pre><code>
$(ProjectDir)helpcompiler.bat $(ProjectDir)HelpProject.hhp
</code></pre>
<br />
When the help file compiles, it will produce HelpProject.chm in the same directory as where the HelpProject.hhp file is created. This is the compiled help file that you need. Add a line to the post-build event that copies the HelpProject.chm file to the final location where it is needed. For example:<br />
<br />
<pre><code>xcopy /Y $(ProjectDir)HelpProject.chm $(SolutionDir)SomeOtherProject\bin\$(ConfigurationName)</code></pre>
<br />
With this hack, Visual Studio will build the help file and copy it to where it needs to go as part of the build of the Solution.Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-30189156413778486032017-12-13T16:31:00.000-08:002017-12-13T16:31:21.269-08:00Use .editorconfig File to Enforce Coding ConventionsAs you may know, I literally wrote the book on C# coding conventions. You can get my book, <a href="https://www.amazon.com/Reddick-Style-Guide-practices-writing/dp/0692531742" target="_blank">The Reddick C# Style Guide</a> on Amazon. Since the book was published, C# and Visual Studio have changed a little, as they have added new features to both. There is nothing that I would change in the book, but a few of the new features they added to version 7.x of C# that are not mentioned, such as tuples and pattern matching. Until I can get around to updating the book, there is a nifty feature in Visual Studio 2017 that you can use to enforce what I consider to be the proper coding style for C#.<br />
<br />
In the root of your code, add a text file called .editorconfig. The basic format for this file is defined at <a href="http://editorconfig.org/">http://EditorConfig.org</a>. There are specific entries that are understood in Visual Studio 2017, starting with version 15.3, that can be found at <a href="https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference">https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference</a>. This is the file that I use, that uses the I consider to be the right style. Even if you don't agree, feel free to use it as a template for your own style.<br />
<br />
<br />
<pre><code># http://EditorConfig.org
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference
root=true
[*]
indent_style=tab
indent_size=tab
tab_width=4
end_of_line=crlf
charset=utf-8
trim_trailing_whitespace=true
insert_final_newline=false
max_line_length=140
[*.{cs,vb}]
# "This." and "Me." qualifiers
dotnet_style_qualification_for_field=true:warning
dotnet_style_qualification_for_property=true:warning
dotnet_style_qualification_for_method=true:warning
dotnet_style_qualification_for_event=true:warning
# Language keywords instead of framework type names for type references
dotnet_style_predefined_type_for_locals_parameters_members=true:warning
dotnet_style_predefined_type_for_member_access=true:warning
# Modifier preferences
dotnet_style_require_accessibility_modifiers=always:warning
csharp_preferred_modifier_order=public, private, protected, internal, static, extern, new, virtual, abstract, sealed, override, readonly, unsafe, volatile, async:warning
visual_basic_preferred_modifier_order=Partial, Default, Private, Protected, Public, Friend, NotOverridable, Overridable, MustOverride, Overloads, Overrides, MustInherit, NotInheritable, Static, Shared, Shadows, ReadOnly, WriteOnly, Dim, Const,WithEvents, Widening, Narrowing, Custom, Async:nonewarning
# Expression-level preferences
dotnet_style_object_initializer=true:warning
dotnet_style_collection_initializer=true:warning
dotnet_style_explicit_tuple_names=true:warning
dotnet_style_coalesce_expression=true:warning
dotnet_style_null_propagation=true:warning
# Implicit and explicit types
csharp_style_var_for_built_in_types=false:warning
csharp_style_var_when_type_is_apparent=false:warningn
csharp_style_var_elsewhere=false:warning
# Expression-bodied members
csharp_style_expression_bodied_methods=false:warning
csharp_style_expression_bodied_constructors=false:warning
csharp_style_expression_bodied_operators=false:warning
csharp_style_expression_bodied_properties=false:warning
csharp_style_expression_bodied_indexers=false:warning
csharp_style_expression_bodied_accessors=false:warning
# Inlined variable declarations
csharp_style_inlined_variable_declaration=true:warning
# Pattern matching
csharp_style_pattern_matching_over_is_with_cast_check=true:warning
csharp_style_pattern_matching_over_as_with_null_check=true:warning
# Expression-level preferences
csharp_prefer_simple_default_expression=true:warning
csharp_style_deconstructed_variable_declaration=true:warning
csharp_style_pattern_local_over_anonymous_function=true:warning
# "Null" checking preferences
csharp_style_throw_expression=false:warning
csharp_style_conditional_delegate_call=true:warning
# Code block preferences
csharp_prefer_braces=true:warning
# Organize Usings
dotnet_sort_system_directives_first=true
# Newline Options
csharp_new_line_before_open_brace=all
csharp_new_line_before_else=true
csharp_new_line_before_catch=true
csharp_new_line_before_finally=true
csharp_new_line_before_members_in_object_initializers=true
csharp_new_line_before_members_in_anonymous_types=true
csharp_new_line_between_query_expression_clauses=true
# Indentation Options
csharp_indent_case_contents=true
csharp_indent_switch_labels=true
csharp_indent_labels=flush_left
# Spacing Options
csharp_space_after_cast=false
csharp_space_after_keywords_in_control_flow_statements=true
csharp_space_between_method_declaration_parameter_list_parentheses=false
#csharp_space_between_parentheses=
# Wrapping Options
csharp_preserve_single_line_statements=false
csharp_preserve_single_line_blocks=false</code></pre>
Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com1tag:blogger.com,1999:blog-5344982184119705320.post-21067986673542821632017-11-21T10:06:00.001-08:002017-11-21T10:06:46.485-08:00WPF RibbonSplitButton Activates TwiceOn a WPF (Windows Presentation Foundation) RibbonSplitButton there are two parts. There is a button at the top, and a down arrow. The down arrow causes a menu to appear. If you click one of the menu items, there is what I would consider to be a bug, but what <a href="https://social.msdn.microsoft.com/Forums/vstudio/en-US/da13b5f5-5252-4332-9ec3-0b78c6395693/ribbonapplicationsplitmenuitem-triggers-command-twice?forum=wpf" target="_blank">Microsoft considers to be "By Design"</a> where it triggers the event code twice. Essentially, it triggers it once for the menu item, and once for the button.<br />
<br />
There is a way to handle the problem. Essentially on the first trigger, you need to set the "Handled" property of the RoutedEventArgs to be true. The solution posted on the Microsoft site requires an event handler in code-behind, which isn't compatible with the MVVM architecture. Here is how I handled it using Caliburn.Micro for a button in my application that is supposed to start Excel in one of two different ways. The button at the top executes it with #0, and the two menu items executes it with #1 and #0.<br />
<br />
First, here is the XAML. The key part of this is to pass the $executionContext as an argument to the method. This gets the necessary property to where it can be modified.<br />
<br />
<pre><code>
<ribbon:RibbonSplitButton
cal:Message.Attach="[Event Click]=[Excel(0, $executionContext)]"
IsEnabled="{Binding CanExcel}"
KeyTip="X"
Label="{x:Static loc:ShellViewResources.Excel}"
LargeImageSource="/Xoc.MayaCalendar.Windows;component/Assets/Images/Ribbon/ExcelLarge.png"
SmallImageSource="/Xoc.MayaCalendar.Windows;component/Assets/Images/Ribbon/ExcelSmall.png">
<ribbon:RibbonMenuItem
Header="{x:Static loc:ShellViewResources.Excel}"
ImageSource="/Xoc.MayaCalendar.Windows;component/Assets/Images/Ribbon/PrintSmall.png"
cal:Message.Attach="[Event Click]=[Excel(1, $executionContext)]" />
<ribbon:RibbonMenuItem
Header="{x:Static loc:ShellViewResources.ExcelExample}"
ImageSource="/Xoc.MayaCalendar.Windows;component/Assets/Images/Ribbon/PrintSmall.png"
cal:Message.Attach="[Event Click]=[Excel(0, $executionContext)]" />
</ribbon:RibbonSplitButton>
</code></pre>
<br />
The next part is to handle the event. In the Caliburn.Micro code, it starts with:<br />
<br />
<pre><code>
public void Excel(ContentLevel contentLevel, ActionExecutionContext executionContext)
{
RoutedEventArgs routedEventArgs = (RoutedEventArgs)executionContext.EventArgs;
routedEventArgs.Handled = true;
// other code
}
</code></pre>
<br />
This handles the event, which causes it not to cause the second event.Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-74488988119871199182017-11-02T10:37:00.001-07:002018-01-23T23:17:41.372-08:00Do Not Buy Avi-On Light SwitchesI bought a GE Avi-On light switch for my daughter's room. This replaces the regular wall switch with one that can be remotely controlled from her phone. The idea was to allow her to turn off the overhead light from the bed. It worked for about 10 minutes until it started downloading a firmware update to the switch, at which point the update failed. The switch no longer works. Lots of people are reporting the same issue, which means there are thousands of these switches throughout the country with this problem.<br />
<br />
The Avi-On switches have these problems:<br />
<br />
<ol>
<li>The phone software always requires an internet connection and a login to the Avi-On site. Why? This is a Bluetooth app. There is no reason whatsoever that it needs a connection to a remote network. It only needs a connection between the phone and the switch.</li>
<li>It requires location services turned on and access to the file system on the phone. Why? This just smacks of them just wanting to harvest information from the phone, because there is no need for these permissions. The only thing that would require these accesses is for a firmware update to be downloaded from the network to be pushed to the switch. Which leads to...</li>
<li>The firmware update failed. You cannot push down a firmware update to remote switches if the firmware destroys the switch! And if your testing is abysmal and you somehow screw up and release a bad update, you must pull the firmware update off your site the moment you realize it. People have been reporting that the Avi-On current firmware update is causing problems for the last two months, but they are still pushing out the update.</li>
<li>These switches supposedly make a Bluetooth mesh with other switches
in the area to allow spanning past the normal range of Bluetooth.
Bluetooth isn't the right technology for home control. The range is too short, which means you either need a huge number of these devices, or you need repeaters about every 33 feet apart. That's an expensive solution to home control. The goal on home control is to have connectivity that ends at the walls of your house, but not before.</li>
</ol>
<br />
In summary, do not buy Avi-On GE Bluetooth light switches, or anything else made by Avi-On Labs. I will be returning this switch to where I bought it.Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-76205788293621168012017-09-02T01:17:00.000-07:002018-06-10T20:19:57.392-07:00Outlook VBA to Move Spam from Top Level Domains to JunkThere is a spammer who has been active for the last couple of months. The majority of my spam email has been coming from the top level domains .trade, .bid, .club, .stream, and .date. I get no legitimate mail from any of those top level domains.<br />
<br />
I wrote a VBA routine to go through my inbox and move all email from those domains to my junk folder. It appears below:<br />
<br />
<pre><code>
Public Sub JunkSpamDomains()
On Error GoTo ErrorHandler
Dim mailItem As Outlook.mailItem
Dim folderInbox As Outlook.folder
Dim folderJunk As Outlook.folder
Dim i As Long
Dim accessor As Outlook.PropertyAccessor
Dim strHeaders As String
Dim lngOffset1 As Long
Dim lngOffset2 As Long
Dim lngDomainOffset As Long
Dim strFrom As String
Dim strDomain As String
</code><code><code> Const PR_TRANSPORT_MESSAGE_HEADERS = "http://schemas.microsoft.com/mapi/proptag/0x007D001E"</code> Const strBanned As String = ".trade|.bid|.club|.stream|.date"
Set folderInbox = Application.Session.GetDefaultFolder(olFolderInbox)
Set folderJunk = Application.Session.GetDefaultFolder(olFolderJunk)
For Each mailItem In folderInbox.Items
If mailItem.Class = OlObjectClass.olMail Then
Set accessor = mailItem.PropertyAccessor
strHeaders = accessor.GetProperty(PR_TRANSPORT_MESSAGE_HEADERS)
lngOffset1 = InStr(1, strHeaders, vbCrLf & "From: ") + 8
lngOffset2 = InStr(lngOffset1, strHeaders, ">")
strFrom = Mid(strHeaders, lngOffset1, lngOffset2 - lngOffset1)
lngDomainOffset = InStrRev(strFrom, ".")
strDomain = Mid(strFrom, lngDomainOffset)
If InStr(1, strBanned, strDomain) > 0 Then
mailItem.Move folderJunk
End If
Set accessor = Nothing
Set mailItem = Nothing
End If
Next mailItem
Set folderInbox = Nothing
Set folderJunk = Nothing
Exit Sub
ErrorHandler:
Select Case Err.Number
Case Else
MsgBox "Unexpected Error #" & Err.Number & " " & Err.Description
Resume Next
End Select
End Sub</code></pre>
<br />
Press Alt-F11 to open the VBA Editor. Select Insert Module from the menu to insert a new module. Paste this code into the window. You can bind a button on the Quick Access Toolbar (QAT) to this macro. Click the drop-down button on the right of the QAT, and select "More Commands...". Drop the "Choose Commands from:" list and select "Macros". Select "JunkSpamDomains" and click the "Add>>" button. Click the OK button.<br />
<br />
After moving message, review the Junk folder to make sure that only spam got moved.Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-80651635863481949342017-06-03T21:30:00.000-07:002017-06-03T21:48:49.707-07:00VBA Runtime Error CodesI was teaching a VBA (Visual Basic for Applications) class for Excel today, and the question came up, "is there a list of all of the runtime error code numbers and what they mean?" You might be able to find one, but the easiest thing to do is to generate the list. Here is a small piece of VBA code that shows all of the runtime error code numbers and their descriptions.<br />
<br />
<pre><code>Public Sub DisplayErrors()
Dim i As Long
For i = 1 To 65535
If Error(i) <> "Application-defined or object-defined error" Then
Debug.Print i & " " & Error(i)
End If
Next i
End Sub</code></pre>
<br />
When you run the code, it will print the list to the Immediate Window in the VBA Editor. Press Ctrl+G to make the Window visible.<br />
<br />
It is possible to get other runtime errors, but only from some component that is called by VBA, not from VBA itself. When I run the code, this is the list that I get:<br />
<br />
3 Return without GoSub<br />
5 Invalid procedure call or argument<br />
6 Overflow<br />
7 Out of memory<br />
9 Subscript out of range<br />
10 This array is fixed or temporarily locked<br />
11 Division by zero<br />
13 Type mismatch<br />
14 Out of string space<br />
16 Expression too complex<br />
17 Can't perform requested operation<br />
18 User interrupt occurred<br />
20 Resume without error<br />
28 Out of stack space<br />
35 Sub or Function not defined<br />
47 Too many DLL application clients<br />
48 Error in loading DLL<br />
49 Bad DLL calling convention<br />
51 Internal error<br />
52 Bad file name or number<br />
53 File not found<br />
54 Bad file mode<br />
55 File already open<br />
57 Device I/O error<br />
58 File already exists<br />
59 Bad record length<br />
61 Disk full<br />
62 Input past end of file<br />
63 Bad record number<br />
67 Too many files<br />
68 Device unavailable<br />
70 Permission denied<br />
71 Disk not ready<br />
74 Can't rename with different drive<br />
75 Path/File access error<br />
76 Path not found<br />
91 Object variable or With block variable not set<br />
92 For loop not initialized<br />
93 Invalid pattern string<br />
94 Invalid use of Null<br />
96 Unable to sink events of object because the object is already firing events to the maximum number of event receivers that it supports<br />
97 Can not call friend function on object which is not an instance of defining class<br />
98 A property or method call cannot include a reference to a private object, either as an argument or as a return value<br />
321 Invalid file format<br />
322 Can't create necessary temporary file<br />
325 Invalid format in resource file<br />
380 Invalid property value<br />
381 Invalid property array index<br />
382 Set not supported at runtime<br />
383 Set not supported (read-only property)<br />
385 Need property array index<br />
387 Set not permitted<br />
393 Get not supported at runtime<br />
394 Get not supported (write-only property)<br />
422 Property not found<br />
423 Property or method not found<br />
424 Object required<br />
429 ActiveX component can't create object<br />
430 Class does not support Automation or does not support expected interface<br />
432 File name or class name not found during Automation operation<br />
438 Object doesn't support this property or method<br />
440 Automation error<br />
442 Connection to type library or object library for remote process has been lost. Press OK for dialog to remove reference.<br />
443 Automation object does not have a default value<br />
445 Object doesn't support this action<br />
446 Object doesn't support named arguments<br />
447 Object doesn't support current locale setting<br />
448 Named argument not found<br />
449 Argument not optional<br />
450 Wrong number of arguments or invalid property assignment<br />
451 Property let procedure not defined and property get procedure did not return an object<br />
452 Invalid ordinal<br />
453 Specified DLL function not found<br />
454 Code resource not found<br />
455 Code resource lock error<br />
457 This key is already associated with an element of this collection<br />
458 Variable uses an Automation type not supported in Visual Basic<br />
459 Object or class does not support the set of events<br />
460 Invalid clipboard format<br />
461 Method or data member not found<br />
462 The remote server machine does not exist or is unavailable<br />
463 Class not registered on local machine<br />
481 Invalid picture<br />
482 Printer error<br />
735 Can't save file to TEMP<br />
744 Search text not found<br />
746 Replacements too longGreg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-87658878159228668272017-06-01T14:24:00.001-07:002017-06-23T11:35:52.401-07:00C# Optimization of Switch Statement with StringsC# does some interesting things when you have a switch statement comparing a lot of strings: Suppose you have this:<br />
<br />
<pre class="cscode"><code> <span class="key">switch</span> (input)
{
<span class="key">case</span> <span class="str">"AAAA"</span>:
Console.WriteLine(<span class="str">"AAAA branch"</span>);
<span class="key">break</span>;
<span class="key">case</span> <span class="str">"BBBB"</span>:
Console.WriteLine(<span class="str">"BBBB branch"</span>);
<span class="key">break</span>;
<span class="key">default</span>:
Console.WriteLine(<span class="str">"default branch"</span>);
<span class="key">break</span>;
}
Console.WriteLine(<span class="str">"Complete"</span>);</code></pre>
<br />
<br />
When you look at the IL (intermediate language) that it compiles into, it is essentially the same as a bunch of <i>if</i> and <i>else if</i> statements. Converted back into C# code, it is as if you wrote this:<br />
<br />
<pre class="cscode"><code><span class="key"> if</span> (input == <span class="str">"AAAA"</span>)
{
Console.WriteLine(<span class="str">"AAAA branch"</span>);
}
<span class="key">else</span> <span class="key">if</span> (input == <span class="str">"BBBB"</span>)
{
Console.WriteLine(<span class="str">"BBBB branch"</span>);
}
<span class="key">else</span>
{
Console.WriteLine(<span class="str">"default branch"</span>);
}
Console.WriteLine(<span class="str">"Complete"</span>);</code></pre>
<br />
However, if you continue to add case statements, this becomes inefficient. There are a lot of string comparisons that are really expensive. At a certain point, as you add cases, the compiler uses an entirely different technique to handle the cases. It creates a <a href="https://en.wikipedia.org/wiki/Hash_table" target="_blank">hash table</a> of the strings. The IL looks like this, if it were converted back into C# code (assume there are more case statements):<br />
<br />
<pre class="cscode"><code> <span class="key">string</span> s = input;
<span class="key">switch</span> (ComputeStringHash(s))
{
<span class="key">case</span> 0x25bfaac5:
<span class="key">if</span> (s == <span class="str">"BBBB"</span>)
{
Console.WriteLine(<span class="str">"BBBB branch"</span>);
<span class="key">goto</span> Label_0186;
}
<span class="key">break</span>;
<span class="key">case</span> 0xff323f9:
<span class="key">if</span> (s == <span class="str">"AAAA"</span>)
{
Console.WriteLine(<span class="str">"AAAA branch"</span>);
<span class="key">goto</span> Label_0186;
}
<span class="key">break</span>;
}
Console.WriteLine(<span class="str">"default branch"</span>);
Label_0186:
Console.WriteLine(<span class="str">"Complete"</span>);</code></pre>
<br />
The ComputeStringHash method is a pretty simple hash function that looks like this:<br />
<br />
<pre class="cscode"><code> <span class="key">internal</span> <span class="key">static</span> <span class="key">uint</span> ComputeStringHash(<span class="key">string</span> s)
{
<span class="key">uint</span> num = 0;
<span class="key">if</span> (s != <span class="key">null</span>)
{
num = 0x811c9dc5;
<span class="key">for</span> (<span class="key">int</span> i = 0; i < s.Length; i++)
{
num = <span class="key">unchecked</span>((s[i] ^ num) * 0x1000193);
}
}
<span class="key">return</span> num;
}</code></pre>
<br />
This is a version of the <a href="http://isthe.com/chongo/tech/comp/fnv/" target="_blank">FNV-1a hashing algorithm</a>. <br />
<br />
The change to using hashing seems to occur at about eight string case statements. The advantage is that there will be, on average, just one string comparison, the other comparisons are all comparing uint values. There is some overhead in performing the computing of the hash, which is why it doesn't use it for small number of case statements.<br />
<br />
This actually becomes important when you are trying to write unit tests for the code. If you are trying to cover all of the branches in the unit tests, you will need to write code that hashes to 0xff323f9 but is not "AAAA" to get the <i>goto Label_0186</i> branches to get covered. Your chances of finding something that hashes to the same value as your legitimate "AAAA" string without being "AAAA" is unlikely unless you are specifically trying to get a hash collision. This means that your code coverage will show branches as not being covered, even though you test every case statement in the switch statement. This will show a failure in your code coverage branch statistics (usually around only 60% covered), even though your unit test are actually adequate.<br />
<br />
I have been working with the AxoCover and OpenCover programmers to try to get the coverage statistics for branches to be meaningful, but there may be no way to handle this correctly.<br />
<br />
Addendum: The logic of the switch statements when it optimizes is slightly more complicated that what is presented above. The C# compiler actually performs a binary search on the hash index rather than just linearly searching through them, before getting to the comparison of the string. Performing hash collisions will raise your coverage to more than 90%, but will not go through all of the code for the binary search.Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-74703465631104190902017-05-30T00:47:00.000-07:002017-05-30T00:47:51.145-07:00How to Convert Project from MSTest (V1) to MSTestV2To convert a MSTest version 1 project to version 2, you need to perform several steps:<br />
<br />
<ol>
<li>In the project references, remove the reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.</li>
<li>Edit the project file and remove the line that contains <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids></li>
<li>Add the NuGet package for MSTest.TestAdapter.</li>
<li>Add the NuGet package for MSTest.TestFramework</li>
</ol>
That should be all that is necessary to make the transition.Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com2tag:blogger.com,1999:blog-5344982184119705320.post-70316926360119016092017-05-29T13:30:00.000-07:002018-08-28T23:28:21.388-07:00Don't Indicate Status with Just Color<br />
Note: Since this article was written, AxoCover added configuration of colors in the settings dialog. The original point, though, was that you shouldn't indicate status with just color in your own projects. <br />
<br />
There is a flaw in the AxoCover code coverage tool: The colors used for coverage,
red and green, are hard coded. If someone were red/green color blind,
this tool would be difficult to use. That creates difficulty for 8% of
men and 0.5% of women. Showing status with hard coded colors with no
other visual indicator is a mistake in user interface design, not just
in AxoCover but in all programs. Either colors must be configurable, or
an additional way of differentiating the colors (such as hatching one of
them) should be used. <br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://1.bp.blogspot.com/-avoALOzvYvo/WSx5Pr1md0I/AAAAAAAABm0/MdPM6RNAHp8sgtSuI83Zb17Q3pmymQ0PACEw/s1600/axocover3.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="718" data-original-width="936" height="306" src="https://1.bp.blogspot.com/-avoALOzvYvo/WSx5Pr1md0I/AAAAAAAABm0/MdPM6RNAHp8sgtSuI83Zb17Q3pmymQ0PACEw/s400/axocover3.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Status indicated with red and green bars</td></tr>
</tbody></table>
You'll notice that a traffic stop light
indicates color with both red and green, but also with position of the
colors; it does not have one light that changes color.<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://3.bp.blogspot.com/-_sc4sqaARTY/WSyCNw_jCJI/AAAAAAAABnM/7WTm6QPRJPoOoE1QgdQiqm4jLwhXdgWuQCLcB/s1600/trafficlight.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="443" data-original-width="167" height="200" src="https://3.bp.blogspot.com/-_sc4sqaARTY/WSyCNw_jCJI/AAAAAAAABnM/7WTm6QPRJPoOoE1QgdQiqm4jLwhXdgWuQCLcB/s200/trafficlight.png" width="75" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Traffic light uses both color and position to indicate status</td></tr>
</tbody></table>
Indicating status with just color, particularly red and green, is a very common UI mistake among programmers who are not color blind. While I am not color blind, I try to be aware of places where color is used wrong. This is not just an inconvenience to some people. In some cases, particularly in the United States which has the Americans with Disabilities Act (ADA), using just color to indicate status might be a violation of the law, and could have legal repercussions.Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com0tag:blogger.com,1999:blog-5344982184119705320.post-17887259708775508122017-05-29T13:11:00.002-07:002017-06-01T14:45:48.376-07:00Code Coverage with AxoCoverWhen performing unit tests on code, how do you know if your unit tests are covering all of the code? The answer is a code coverage tool. I first used a tool like this when I was on the Microsoft Access 1.0 programming team, where we got weekly reports on how well the unit tests were covering the code. Microsoft has a code coverage tool in Visual Studio 2017, but only in the Enterprise edition. Unit tests and code coverage go together, and for Microsoft to have unit tests in the Community and Professional releases but not code coverage is kind of dumb.<br />
<br />
Fortunately, there is a pretty competent, free, third party choice called AxoCover. You can download it from the Extensions and Updates menu item on the Tools menu in Visual Studio. It places an AxoCover menu item on the Tools menu that brings up a window to control its use. Build your solution once for AxoCover to figure out what is in there to cover. Then on the AxoCover window, click the Run button at the top. This is what it will look like:<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://3.bp.blogspot.com/-1h-nP2nah7I/WSx0KuViSWI/AAAAAAAABmY/9qQBlKOnGycFmMSPdSXuvFTVmcm5Ivv8ACLcB/s1600/axocover1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="769" data-original-width="779" height="393" src="https://3.bp.blogspot.com/-1h-nP2nah7I/WSx0KuViSWI/AAAAAAAABmY/9qQBlKOnGycFmMSPdSXuvFTVmcm5Ivv8ACLcB/s400/axocover1.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">AxoCover window in Visual Studio 2017</td></tr>
</tbody></table>
<br />
The critical information is on the Report tab on the left. After running the tests, AxoCover shows how much of the code got covered by percent. There are two numbers: what percent of lines and what percent of branches got covered. If there is an "if" statement, a test that hits the line counts will result in the line counting 100%, but only if both the true and false conditions are tested will the branches show 100%.<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://1.bp.blogspot.com/-qkGJpF8hZuc/WSx2Tz5wtJI/AAAAAAAABmk/QZNLyHDHUr4-Qs60_2SWf2eqOYlWrcGrgCLcB/s1600/axocover2.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="768" data-original-width="779" height="393" src="https://1.bp.blogspot.com/-qkGJpF8hZuc/WSx2Tz5wtJI/AAAAAAAABmk/QZNLyHDHUr4-Qs60_2SWf2eqOYlWrcGrgCLcB/s400/axocover2.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">AxoCover report tab</td></tr>
</tbody></table>
The goal here is to get the branches to over 90%. Why not 100%? In production code, there are sometimes error handling code that can happen in only the rarest of conditions. For example: there may be code to handle if the fixed hard drive that you are writing to fails while the program is running. Sometimes trying to create a unit test for these conditions is difficult or impossible. So in general, greater than 90% is considered covered. Obviously, higher is better, but if all of your code is at 90%, you are doing pretty well. So in the example above, the coverage is 85.7%, which is good, but not good enough.<br />
<br />
After running the tests, you can right click on a procedure in the report window and select "Show source". The source will show, with some information on the left border on what was covered and what wasn't.<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://4.bp.blogspot.com/-avoALOzvYvo/WSx5Pr1md0I/AAAAAAAABmw/dYfNtc380_Axpc66xi0P9qCOBD15CGV7wCLcB/s1600/axocover3.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="718" data-original-width="936" height="306" src="https://4.bp.blogspot.com/-avoALOzvYvo/WSx5Pr1md0I/AAAAAAAABmw/dYfNtc380_Axpc66xi0P9qCOBD15CGV7wCLcB/s400/axocover3.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Code window after running AxoCover</td></tr>
</tbody></table>
The green bar show lines that were covered by the unit tests. The red bar shows lines that were uncovered. The little circles show if branches were covered. In an "if" statement, you want both circles filled in. In the example above, there needs to be a unit test that covers the case "a" branch of the switch statement. After adding a unit test that covers that statement, go back to the Tests tab, build and run the tests. The report tab, then looks like this:<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://4.bp.blogspot.com/-Sg-Wsu3ojYo/WSx7N8gIeZI/AAAAAAAABm8/5N0Unl5fIOUJvZd8e0H_wg9gO2Rd5432wCLcB/s1600/axocover4.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="771" data-original-width="779" height="395" src="https://4.bp.blogspot.com/-Sg-Wsu3ojYo/WSx7N8gIeZI/AAAAAAAABm8/5N0Unl5fIOUJvZd8e0H_wg9gO2Rd5432wCLcB/s400/axocover4.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">AxoCover report after adding new test case</td></tr>
</tbody></table>
Here, the coverage is perfect.<br />
<br />
If there is one thing lacking on AxoCover, it's the documentation. The Settings tab has pretty much all of the documentation that exists.<br />
<br />
AxoCover is really just a UI that integrate into Visual Studio. The engine that drives the code coverage is a different open source project called OpenCover, that has a command-line only interface. OpenCover is installed automatically when you install AxoCover. Greg Reddickhttp://www.blogger.com/profile/04469566612778276012noreply@blogger.com5