How to focus text box in Blazor project

If you have to spend a lot of your time in your day doing repetitive tasks such as entering data into forms then you will likely agree that it can be quite convenient to focus on a textbox in a form.

When you’re accessing a form over and over filling out the same field it can be a huge time saver to have the cursor already in the box ready for you to type and hit enter.

It can greatly reduce the stress of repetitive task by not having to use the mouse as much.

According to the Microsoft training portal, you can easily accomplish this convenient feature in a Blazor project.

All you have to do is add a reference to the input element, then a line of code to activate it. Here is a summary of the information I learned in the Microsoft Training Portal.

  1. The Blazor directive @ref="formInputName" lets the code block create an ElementReference to reference the input element. You can then use this element reference to call FocusAsync after a page has been rendered.
  2. Add code to call FocusAsync after a page has loaded
<input @ref="formInputName" @bind="MyForm.formInputName" />

Now add a variable to activate and use that reference, and the code to call FocusAsync after a page has loaded:

private ElementReference formInputName;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender) {
        await formInputName.FocusAsync();
    }
}
Form with a text box focused by the Blazor FocusAsync().
Screenshot taken from the results of taking a Microsoft Training.

Unity Rotate Object

A simple script to rotate object in Unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotate : MonoBehaviour
{
    Vector3 movement;
    public int xi, yi, zi;

   
    void Start()
    {
        movement = new Vector3(xi, yi, zi);
    }

    
    void Update()
    {
        transform.Rotate(movement * Time.deltaTime);
    }
}


Add values to the public variables xi, yi, zi depending on the speed and direction you would like the object to rotate.

Use variables from the web config C# Visual Studio

We create a helper class to use. This will return a variable that is set to the correct type. Credits to Steam Webhosting!

 public static class AppSettingsHelper
    {
        public static T GetValue<T>(string key, T defaultValue)
        {
            var value = ConfigurationManager.AppSettings[key];

            if (string.IsNullOrEmpty(value))
                return defaultValue;

            return (T)Convert.ChangeType(value, typeof(T));
        }
    }

We use the helper function to get the value and set the types of the variables.

        // debug / test settings
        static readonly bool testmode       = AppSettingsHelper.GetValue<bool>("TestMode", false); // toggle test mode
        static readonly bool disableupload  = AppSettingsHelper.GetValue<bool>("DisableUpload", false); // disables the upload to server if true

        // sftp settings
        static readonly int port            = AppSettingsHelper.GetValue<int>("SFTPPort", 22);
        static readonly string host         = AppSettingsHelper.GetValue<string>("SFTPHost", "feeds.dwdenney.com");

Split a comma delimited string into an array in C#

The ability to split a string is a common feature in programming. The way to implement this in c# is a bit different than most languages.

We have a string such as:

string myVars = “var1, var2, var3, var4”;

We need to split this string into an array of individual variables. That’s where our String.Split() method comes into play. Its pretty easy once you know the syntax. The Microsoft docs don’t mention this way:

string[] myArray = myVars.Split(‘,’);

The key is the single quotes around your delimiter in the split method.

Get the value from between parenthesis c#

Many times you have data in a format that needs to be parsed. One thing I have come across lately is needing to get a value from between parenthesis.

Data: Here is my description and it has a code.(1234)

I need to get the code of 1234 from the data.

string value = "Here is my description and it has a code.(1234)";
int startIndex = value.IndexOf('(');
int endIndex = value.IndexOf(')');
int length = endIndex - startIndex;
if (startIndex > -1 && endIndex > -1)
{
return value.Substring(startIndex + 1, length-1);
}
else   return "There are no parenthesis in the value.";

Local email delivery : Dump email from an app into a local directory for testing

While devving an app on localhost, the email  scripts error out and you cant test what the message would have looked like. With this handy snippet, you can dump any email from your app into a local directory. Slap this into your dev web config file.

<mailSettings>

    <smtp deliveryMethod="SpecifiedPickupDirectory">

      <specifiedPickupDirectory pickupDirectoryLocation="C:\MailDump\" />

      <network host="localhost"/>

    </smtp>

  </mailSettings>

 

Add an ID to the rows of a GridView table

It can be handy to have each row have an ID for use with client side scripting. By default the asp gridview does not come with ids for the rows. Use the row databound event to add them

protected void GridViewFullView_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         GridViewRow row = e.Row;
          row.Attributes["id"] = GridViewFullView.DataKeys[e.Row.RowIndex].Value.ToString();
     }
}