Thursday, April 30, 2009

ASP.NET DIY Intellesence

As a time killer I decided to figure out a way to incorporate intellesence into an ASP.NET application.  Note this is something I worked on for a few hours without time to refactor; there could also be better ways to accomplish this.  I used jQuery to handle all my javascript needs and just your basic .NET standard TextBox and ListBox.

What exactly does this do?

Well first of all I must instill again that this is basic; you can take this code and modify it to fit your needs.  This simply renders your basic intellesence from a populated listbox.  Being that it is not complete, if someone would like to take the reigns and send me the code changes to re-publish that would be great too.  My email is gary.l.coxjr at gmail dot com.

Current Known Problems:

  • Intellesence is not showing below the text
  • Intellesence only displays when the OemPeriod is pressed ‘.’ (would be nice to be able to press “Ctrl”+”Shift”+”Space”)
  • Select from list as you type is not perfect (Doesn’t always find the right item)
  • Double-Click on listbox does not post to textbox

Please keep in mind this is NOT production code and shall not be used as such.  You may use this at your own risk.

What can be done to make this more useful?

Since the intellesence works off the listbox, any ListItems you load in there will be displayed when a period is pressed.  Whatever is selected in the listbox when you hit “Space” or “Enter” is what will be entered after the period in the textbox.  If you populate it with a list of object properties, like in my example of class Person, when a user types Person and then hits the period they will be presented with the pre-populated properties that can be selected.

The Javascript can be found in AspxIntellesence.js file located in ~/JS directory.

Here is what we see:

Intellesence

Notice I started to type “S” and the intellesence moved to StreetAddress.  If I had pressed the space bar then you would get Person.StreetAddress as the javascript would handle this.

What is this useful for anyway?

Well I think it could be very useful if your designing a template builder that allows your users to design custom email messages, or campaigns.  This type of tool would allow your customer to build the template and from your backend code process it from a given business object.  Take my example, assume I had a class named Person and the above template was processed.  I could send personalized emails to a select group that may look like:

Welcome Gary Cox,
    We have your current street listed as 123 Some Street.

Simply by parsing out the [object property] and replacing with its value of the current instance.

As always, I hope this helps someone.  Happy Coding!!!

 

Download Source Code

Wednesday, April 22, 2009

A Hidden Gem when it comes to string separation

Most developers based on code I have seen separate strings like so:

for (int i=0; i<someData.Count; i++)
myStringBuilder.AppendFormat("{0},", someData[i]);

Then after constructing this string they need to remove the last separator

myStringBuilder = myStringBuilder.Remove(myStringBuilder.Length-1, 1);

I too have used this concept. However, there is another way to do this that a lot of people didn’t even know existed. The String.Join class; it takes 2 params, the separator and the array.

ArrayList<String> myList = new ArrayList<String>();
for (int i=0; i<someData.Count; i++)
myList.Add(someData[i]);
return String.Join(",", myList.ToArray());


This would return a string separated by a comma such as “Text,MoreText,EvenMore,AndLast”

Monday, April 20, 2009

Going to twitter

Well I jumped on the band wagon and started using twitter.  I must say I really like it; firing off a quick text message type snippets of what you need and someone out there responds.  It’s very easy to use and didn’t take me long to understand the lingo.  When you want to send a tweet to someone you simply place a ‘@’ and their name, for example I am @GaryLCoxJr.  See you out there on twitter :)

Friday, April 17, 2009

Fade Form on Move or Resize



I had a project where we display a form to a user and perform a certain task. Well the user wanted to be able to fade the form when they move it or resize it so they can see the underlying (Main) form.  This is simple but thought since my blog has been out of date for some time now I should add something to it.


To get started, add the following to your form that you want to handle the fade or add to your constant class:

// Called when a Resize or Move loop has ended
const int WM_EXITSIZEMOVE = 0x0232;

// Called at the beginning of a Resize Move loop
const int WM_ENTERSIZEMOVE = 0x0231;


Next, override the WndProc method of your form like and handle the fade of your choice:

protected override void WndProc(ref Message m)
        {
            // Check if user is/isn't dragging the form
            switch (m.Msg)
            {
                case WM_ENTERSIZEMOVE:
                    // Form is being Moved or Resized, set opacity
                    this.Opacity = 0.65;
                    break;
                case WM_EXITSIZEMOVE:
                    // Form is done moving/resizing, set opacity back to 100%
                    this.Opacity = 1;
                    break;
            }

            base.WndProc(ref m);
        }


And thats it, when you move or resize the form the opacity will drop to 65%.  Once the resizing or moving is complete the form will return to 100% opacity.

Load an Xml File into a Class Collection using LINQ


Work a lot with Xml using the C# language? You will know that to load an Xml
file it can be quite extensive at times depending on how deep your requirements
send you. I personally prefer to work with classes that are populated with the
data from the Xml and bind my controls to such. I have put together a simple
example of loading data from an xml and building a collection of a given class.
Then binding a combobox to this list.


Here is my Xml file:
xmlversion="1.0"encoding="utf-8" ?>
<StageTypes>
<StageTypename="SUB">
<StageItemvalue="CP">Customer PlanStageItem>
<StageItemvalue="FF"> ContactStageItem>
<StageItemvalue="OC">Other ContactStageItem>
<StageItemvalue="MD">Medical/DentalStageItem>
<StageItemvalue="LG">LegalStageItem>
<StageItemvalue="OT">OtherStageItem>
StageType>
<StageTypename="FSU">
<StageItemvalue="FP">Family PlanStageItem>
<StageItemvalue="FF"> ContactStageItem>
<StageItemvalue="OC">Other ContactStageItem>
<StageItemvalue="MD">Medical/DentalStageItem>
<StageItemvalue="LG">LegalStageItem>
<StageItemvalue="OT">OtherStageItem>
StageType>
StageTypes>


Here is my StageTypeItem Class I will use to store the Text and Value for a combobox:
public class StageTypeItem
{
///
/// Initializes a new instance of the StageTypeItem class.
///
public StageTypeItem()
{
}
private string dataValue;
public string DataValue
{
get { return dataValue; }
set
{
dataValue = value;
}
}
private string dataText;
public string DataText
{
get { return dataText; }
set
{
dataText = value;
}
}
}


Here is the code to build the list:
public static List<StageTypeItem> LoadStageType(string stageType)
{
XDocument doc = XDocument.Parse(Properties.Resources.StageTypes, LoadOptions.None);
var stageTypes = from st in doc.Descendants("StageType").Descendants("StageItem")
where st.Parent.Attribute("name").Value == stageType
select new StageTypeItem
{
DataText = st.Value,
DataValue = st.Attribute("value").Value
};
return stageTypes.ToList<StageTypeItem>();
}


So I have a Root of StageType, with children StageItem in the Xml. I want to load the values where StageType name = passed parameter stageType. So assume I pass “SUB” in Xml I want the SUB children (StageItem only) to be returned:
<StageTypename="SUB">
<StageItemvalue="CP">Customer PlanStageItem>
<StageItemvalue="FF">ContactStageItem>
<StageItemvalue="OC">Other ContactStageItem>
<StageItemvalue="MD">Medical/DentalStageItem>
<StageItemvalue="LG">LegalStageItem>
<StageItemvalue="OT">OtherStageItem>
StageType>


Here I am saying load all Descendants of StageItem:
var stageTypes = from st in doc.Descendants("StageType").Descendants("StageItem")


But then I say, only StageItem where its parent has the name “SUB”, remember stageType is the parameter and I passed “SUB”
where st.Parent.Attribute("name").Value == stageType


This part creates a class of type StageTypeItem and I populate it from the Xml values:
select new StageTypeItem
{
DataText = st.Value, // the value of the Element, example first one is “Customer Plan”
DataValue = st.Attribute("value").Value // The Attribute named value (Case Sensitive) in the first case is “CP”
};


And then I return a list:
return stageTypes.ToList<StageTypeItem>();


Now I can bind a combobox to this list:
ComboBox cbo = new ComboBox();
cbo.DataSource = LoadStageType(“SUB”);
cbo.DisplayMember = “DataText”; // StageTypeItem.DataText
cbo.ValueMember = “DataValue”; // StageTypeItem.DataValue


Happy Coding!!!

 
Creative Commons License
Blogged Information and Code is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.