30 June 2011

Javascript and IE - Get the selection start and end

In Opera and other browsers it is fairly easy to get the start and end positions of the selection in a textarea. Exept, of course, in Internet Explorer, where you need these:

Get the start and end

function synGetCaret(editor) {
  if(document.selection){
    editor.focus();
    var range = document.selection.createRange();
    var range2 = range.duplicate();
    range2.moveToElementText(editor);
    range2.setEndPoint('EndToEnd', range);
    var selStart = range2.text.length - range.text.length;
    return new Array(selStart, selStart + range.text.length);
  }
}
The return value is an array with 0 = start and 1 = end.

Set the selection

As opposed to Opera and other browsers, IE also looses the selection in a textarea when that textarea looses the focus. Therefore you feed the array that was retrieved above in this function to restore the selection:

function synSetCaret(editor, selection) {
  if (document.selection) {
    if (selection[0] == NaN) selection[0] = 0;
    if (selection[1] == NaN) selection[1] = selection[0];
    if (editor.setSelectionRange) {
      editor.setSelectionRange(selection[0], selection[1]);
    }
    else if (editor.createTextRange) {
      var range = editor.createTextRange();
      range.collapse(true);
      range.moveEnd('character', selection[1]);
      range.moveStart('character', selection[0]);
      range.select();
    }
  }
}

09 June 2011

WPF - Store the position of a GridSplitter

Storing and restoring the position of a GridSplitter element in WPF is rather easy.
Hereby an example on a horizonal splitter:

The layout

<Grid.RowDefinitions>
  <Rowdefinition Height="32">
  <Rowdefinition Height="7*" x:Name="rowDefTables">
  <Rowdefinition Height="3*">
</Grid.RowDefinition>

Store the position

This line would run when the program closes.
Note that rowdefTables is the name of the RowDefinition.
The GridSplitter itself does not even need a name!
mSettings.SplitterTablesColumnsPosition =
          Convert.ToInt32(rowdefTables.Height.Value);
mSettings.SaveSettings(); //To a .ini file

Restore the position

This is done when the program starts:
mSettings.LoadSettings(); //From the .ini file
if (mSettings.SplitterTablesColumnsPosition > 0)
  rowdefTables.Height = new GridLength(
                        (double)mSettings.SplitterTablesColumnsPosition,
                        GridUnitType.Pixel);

ASP.NET MVC - Store uploaded files

Here's how you can store uploaded files in ASP.NET MVC.
To work, the HTML file input control must be - and this is specific to HTML, not MVC - in a form that has it's enctype set to multipart/form-data.

public static void Store(HttpFileCollectionBase files,
                         EntityCollection list) {
  List attachments = new List();
  Attachment attachment = null;
  HttpPostedFileBase file = null;
  FileStream stream = null;
  string fullName = string.Empty;
  byte[] buffer = null;
  int length = 0;

  if (files != null && files.Count > 0) {
    foreach (string inputName in files) {
      if (!string.IsNullOrEmpty(inputName)) {
        file = files[inputName] as HttpPostedFileBase;
        if (file != null && !string.IsNullOrEmpty(file.FileName)) {
          //Generate file name
          fullName = Guid.NewGuid().ToString() + '-' + file.FileName;

          //Store the actual file
          using (stream = File.Create(
                 HttpContext.Current.Server.MapPath(
                 App.FOLDER_ATTACHMENTS + '/' + fullName))) {
            buffer = new byte[file.ContentLength];
            length = file.InputStream.Read(buffer, 0, file.ContentLength);
            stream.Write(buffer, 0, length);
          }

          //Create and attach to the entities
          attachment = new Attachment() {
            Name = fullName,
            Size = file.ContentLength
          };
          list.Add(attachment);
        }
      }
    }
  }
}

The files parameter above gets (HttpRequestBase)request.Files passed.
The function above will also associate the files with an entity, but that's another story.