VB.Net & C# ReplaceFirst and ReplaceLast Extension Methods

Here are two quick extension methods that will allow you to replace just the first or last occurrence in a string with another.  I’ve provided them as extension methods but they could just as easily just be implemented as a function.  Note, that if you use them as an extension method with the Extension() call above the method you will want to import System.Runtime.CompilerServices at the top of that code file.

VB.Net

        ''' <summary>
        ''' An extension method that replaces the first occurance of a specified string.
        ''' </summary>
        ''' <param name="str"></param>
        ''' <param name="searchTxt"></param>
        ''' <param name="replaceTxt"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> _
        Public Function ReplaceFirst(ByVal str As String, searchTxt As String, replaceTxt As String) As String
            Dim pos As Integer = str.IndexOf(searchTxt)

            If pos < 0 Then
                Return str
            End If

            Return str.Substring(0, pos) + replaceTxt + str.Substring(pos + searchTxt.Length)
        End Function

        ''' <summary>
        ''' An extension method that replaces the last occurance of a specified string.
        ''' </summary>
        ''' <param name="str"></param>
        ''' <param name="searchTxt"></param>
        ''' <param name="replaceTxt"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()> _
        Public Function ReplaceLast(ByVal str As String, searchTxt As String, replaceTxt As String) As String
            Dim pos As Integer = str.LastIndexOf(searchTxt)

            If pos < 0 Then
                Return str
            End If

            Return str.Substring(0, pos) + replaceTxt + str.Substring(pos + searchTxt.Length)
        End Function

C#

        /// <summary>
        /// An extension method that replaces the first occurance of a specified string.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="searchTxt"></param>
        /// <param name="replaceTxt"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        [Extension()]
        public string ReplaceFirst(string str, string searchTxt, string replaceTxt)
        {
            int pos = str.IndexOf(searchTxt);

            if (pos < 0)
            {
                return str;
            }

            return str.Substring(0, pos) + replaceTxt + str.Substring(pos + searchTxt.Length);
        } // end ReplaceFirst

        /// <summary>
        /// An extension method that replaces the last occurance of a specified string.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="searchTxt"></param>
        /// <param name="replaceTxt"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        [Extension()]
        public string ReplaceLast(string str, string searchTxt, string replaceTxt)
        {
            int pos = str.LastIndexOf(searchTxt);

            if (pos < 0)
            {
                return str;
            }

            return str.Substring(0, pos) + replaceTxt + str.Substring(pos + searchTxt.Length);
        } // end ReplaceLast

How to see who can see your Outlook Calendar

Pretty straight forward, you want to see who has permission to view your Outlook calendar via an Exchange Server.

  1. In Outlook, click on the “Calendar Tab” on the left hand side.
  2. Under “My Calendars”, right click on the calendar you want to see the permissions for and choose “Properties”.
  3. On the “Calendar Properties” dialog, choose the “Permissions” tab.

Here you can view/setup who has access to your calendar and how much access they have.  You can add individuals to have permissions or you can also use groups, say if you wanted to give an entire department permissions.  I’ve included a screenshot below with some of the key areas highlighted in red.

image

WinRT WebView.InvokeScript–Could not complete the operation due to error 8080000c

This error may occur when interacting with JavaScript and IE (version 10 beta in my case).  My case came up with using the WebView’s InvokeScript method to get around some limitations in what the WebView exposes (in relation to what the old WinForms WebBrowser used to expose).

In my case, this error comes about because you have bad JavaScript and/or a JavaScript error is being thrown.  To test, I created a snippet of JavaScript that set the value on an HTML element on the page.  It worked fine.  If I navigate to a page that didn’t have that element and tried to execute it, I would get the above error.  Here was my test code:

Dim buf As String = wv1.InvokeScript("eval", New String() {"document.getElementById(""inputx"").value = '500';"})

“Server Application Unavailable”

When upgrading a site to the .Net Framework 4.0 I received a “Server Application Unavailable” after I asked one of our system administrators to change the site from the .Net Framework 2.0 CLR (2.0/3.5) to the .Net Framework 4.0 CLR.  After some Google searching I came across a blog entry here that had some good tips (and had the tip that fixed my problem):

In short for my problem, .Net 2.0 web apps and .Net 4.0 web apps cannot run in the same app pool.  After creating an application pool for the new 4.0 site the problem was gone.  The last thing that Frank mentions is that the Web Services Extension must be set to “Allowed” for the .Net Framework 4.0 (mine was by default).

Crystal Reports IDE to Crystal Reports .Net SDK Version

I don’t know if that title makes sense.  This is a chart of what version of the Crystal Reports IDE matches up with which version of the Crystal Reports for .Net (according to my past knowledge + a conversation I just had with an SAP representative on the phone).

Crystal Reports for .Net Version Full Crystal IDE Version
Crystal Reports for Visual Studio 2002 (Version 9) Crystal Reports 9
Crystal Reports for Visual Studio 2003 (Version 9.2) Crystal Reports 9
Crystal Reports for Visual Studio 2005 (Version 10.2) Crystal Reports 10
Crystal Reports for Visual Studio 2008 (Version 10.5) Crystal Reports 2008 (12)
Crystal Reports for Visual Studio 2010 (Version 13) Crystal Reports 2011 (Version 14)

“Hey, go read a book”

Had to share this.  Woke up this morning, turned on Sesame Street via Netflix on my Roku box for my son while finding some food and was left with this screen.  The closed captioning was on and the episode failed to load.  A joke played by Netflix or whoever did the closed captioning for that Sesame Street episode?  I wondered if it something they were able to fit in only if the episode didn’t load, or on the first frame.  I couldn’t find much Googling it. 

543643_10151508962985153_543520152_23893826_852453518_n

Fill in a Bitmap with a solid color (Windows 8 Metro, WinRT)

For those of you that found your way here, you’re probably wanting to learn how to fill in a bitmap with a specific color OR manipulate individual pixels.  This example is the first I’ve got working and I will be posting more as I create more and then turn them into reusable objects.

Unfortunately, the ease of System.Drawing.Bitmap’s GetPixel and SetPixel are gone (for now).  WinRT will not allow you to access that part of the framework (or many other parts of the framework) even if they exist.  Here is my first example on how to fill in a WriteableBitmap with a solid color, pixel by pixel. 

In my example, I’ve created a 128×128 WriteableBitmap.  Then, you have to return the PixelBuffer as a stream, to do so, Import (using in C#) System.Runtime.InteropServices.WindowsRuntime to allow the AsStream() extension to be called.  The other class we’re going to use is Windows.UI.Color (and Windows.UI.Colors) so you may want to include an import to that also.

        Dim bmp As New WriteableBitmap(128, 128)
        Dim stream As Stream = bmp.PixelBuffer.AsStream()
        Dim c As Color = Colors.Red

        For x As Integer = 1 To bmp.PixelWidth
            For y As Integer = 1 To bmp.PixelHeight
                stream.WriteByte(c.B)
                stream.WriteByte(c.G)
                stream.WriteByte(c.R)
                stream.WriteByte(c.A)
            Next
        Next

For each pixel, 4 bytes are written to the stream (Blue, Green, Red, Alpha).  Since this is a stream and not an array that’s 128×128 it’s a little harder to get your head around (my natural instinct is to want to write to pixel 6,4).  Ideally, I want to create something that has the simplicity of System.Drawing.Bitmap.  For now, this is where I’m starting.  Hope this helps sometime.  Below is C# that has been run through a code converter, I haven’t tried to compile it but I thought I’d share it also.

WriteableBitmap bmp = new WriteableBitmap(128, 128);
Stream stream = bmp.PixelBuffer.AsStream();
Color c = Colors.Red;

for (int x = 1; x <= bmp.PixelWidth; x++) {
    for (int y = 1; y <= bmp.PixelHeight; y++) {
        stream.WriteByte(c.B);
        stream.WriteByte(c.G);
        stream.WriteByte(c.R);
        stream.WriteByte(c.A);
    }
}

Windows 8 Consumer Preview with VirtualBox Guest Additions (4.1.12r77245) Issues

Issues is putting it lightly.  I’ve struggled to get a Windows 8 CP instance working correctly with Virtual Box.  My first attempt worked, Windows 8 installed and seemingly worked.  My issue came in that Visual Studio 11 either did not install correctly, didn’t have proper permissions to some folders or just wouldn’t work when it came to using the XAML designer.  I applied all fixes that Microsoft recommended to no avail.

My next attempt was to re-install Windows.  Upon doing this (and every subsequent time) the OS would crash/lock up after the installation of the VirtualBox Guest Additions.  The screen would go to the default color for Metro and nothing else would display.  The only fix I found that has worked is to fully repair the OS (which is pretty much a re-install considering I have nothing else on the VM anyway).  I should note, I did have the guest additions installed a few weeks ago so I don’t know if a recent update has caused new issues that didn’t occur before or not.  The end result is, I cannot get Windows 8 functioning in a way that I can develop Metro apps with it (e.g. with Visual Studio 11).

On an up note, the WinForms editor seems to work as trusty as ever.  There is something to be said for software that works (and works well).  There’s also something to be said about software that doesn’t work well (*cough*, the XAML designer…. “Designer process terminated unexpectedly!”). 

Execute JavaScript after a PostBack (VB and C#)

Here is an example of how to execute JavaScript after a PostBack in a WebForms page (this specific case is not using ASP.NET Ajax, but rather a synchronous PostBack).  This will allow you to run custom JavaScript after the server side code has executed.

VB.Net

Page.Validate

If Page.IsValid = True Then
   Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "script", js, True)
End If

C#

Page.Validate();

if (Page.IsValid)
{
   Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", js, true);
}

Excel Formula to Subtract Hours

This is just a simple formula that will get the duration in between two times.  If just a time is used (e.g. 9:00 PM) it will assume the two values are on the same day.  If the date is also included (4/3/2012 9:00 PM) then it will get the difference that goes over days.  If there isn’t a valid second cell or the difference is negative I’ve included a IF statement to return 0 (say, if you don’t have an end time yet because it hasn’t occurred, in the case of a time tracking spreadsheet).

=IF(B1-A1 >= 0, B1-A1, 0)

The above assumes your values are in cells A1 and B1.  Below is just an example of what it would look like on a sheet.

image