Asp.Net: keyboard sort items

As proof of concept I wanted to sort images in a Grid by keyboard. The sort logic needed to be implemented on the server. My solution for this problem is a combination of Javascript and C#.

First add following html to you .aspx. Notice that the body tag has runat=”server” and a ID.


&lt;body<strong> runat="server" ID="bodyTag"</strong>&gt;
<form id="form1">

<br />


<br />
<br />

</form>


Now add the following JavaScript to your page. This script will fetch all keyboard input and press the corresponding button.



document.onkeydown = checkKeycode
function checkKeycode(e) {
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
switch (keycode) {
case 37:
var obj = document.getElementById('');
obj.focus();
obj.click();
break;
case 38:
var obj = document.getElementById('');
obj.focus();
obj.click();
break;
case 39:
var obj = document.getElementById('');
obj.focus();
obj.click();
break;
case 40:
var obj = document.getElementById('');
obj.focus();
obj.click();
break;
}

}


At last we need to add the following C# code to the page.


protected void Page_Load(object sender, EventArgs e)
{
//Ad clientside onkeypress event to the body
bodyTag.Attributes.Add("OnKeyPress", "keyhandlers()");
}

protected void DownButton_Command(object sender, CommandEventArgs e)
{
//Just for testing
clickedLabel.Text = (string)e.CommandArgument;
}

Enjoy, Pieter

C#: Get Parent Control with Generics

I use the following method to return a parent control of a specific type. This method is recursive and uses generics.

<br />
private Control GetParentControl(Control control)<br />
{<br />
if (control.Parent.GetType() == typeof(T1))<br />
{<br />
return control.Parent;<br />
}<br />
else<br />
{<br />
return GetParentControl(control.Parent);<br />
}<br />
}</p>
<p>

MemoryStream to Byte Array (Byte[])

With the following code you can convert your MemoryStream to a Byte Array.


//create new Bite Array
byte[] biteArray = new byte[memoryStream.Length];

//Set pointer to the beginning of the stream
memoryStream.Position = 0;

//Read the entire stream
memoryStream.Read(biteArray, 0, (int)memoryStream.Length);

Create a Visual Studio add-in with contextmenu and selected text as input

Create a Visual Studio add-in with contextmenu and selected text as input

When working with a new way of storing settings in a database. I was frustrated how much work it was to check the value of setting from code. So I deceided to make my life a bit easier by creating a VS2008 contextmenu add-in. With this add-in I can select text within VS and use the value of the selected text within the add-in popup. The hardest part was figuring out how to create a contextmenu and how to use the selected text as input value.

In this blogpost I will show how to create a Visual Studio contextmenu add-in and pass the selected text to the pop-up. I’m not going to explain how to create an add-in you can easily find articles about this on MSDN or blogs (just try Google).

Now let’s get started. Create an new Visual Studio add-in project and add the following code to the OnConnetion Method within the Connect.cs. This code will insert add the contextmenu.


_applicationObject = (DTE2)application;
CommandBars cBars = (CommandBars)_applicationObject.CommandBars;

CommandBar editorCommandBar = (CommandBar)cBars["Editor Context Menus"];
CommandBarPopup editPopUp = (CommandBarPopup)editorCommandBar.Controls["Code Window"];

Command command = commands.AddNamedCommand2(_addInInstance,
 "GetSetting", "Bekijk Setting", "Executes the command for test", true, 733, ref contextGUIDS,
 (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
 (int)vsCommandStyle.vsCommandStylePictAndText,
 vsCommandControlType.vsCommandControlTypeButton);

Then to get the selected text I use the following method within the Exec of the Connect.cs and pass the selected text (return value) to a property of a Windows Form pop-up.


private string GetSelection()
{
    string setting = "";

    //Check active document
    if (_applicationObject.ActiveDocument != null)
    {
        //Get active document
        TextDocument objTextDocument = (TextDocument)_applicationObject.ActiveDocument.Object("");
        TextSelection objTextSelection = objTextDocument.Selection;

        if (!String.IsNullOrEmpty(objTextSelection.Text))
        {
 //Get selected text
            setting = objTextSelection.Text;
        }
    }
    return setting;
}

Hope it helps.

Cheers,
Pieter

C#: Remove line from textfile

With the following code you can remove a line from a textfile (web.config). If the string is within a line the line will be removed.


string configFile = @"C:devweb.config";
List lineList = File.ReadAllLines(configFile).ToList();
lineList = lineList.Where(x =&gt; x.IndexOf("&lt;!--&quot;) &lt;= 0).ToList();
File.WriteAllLines(configFile, lineList.ToArray());