Tuesday, July 10, 2007

C#: Drag'n'Drop

Been wanting to do an organizer for all the images I steal off the web. One of the first things I need to know how to do is to support drag'n'drop from the browser. Instead of going to a directory, I want the file to go to my vault.

Started out with a "C# drag drop" Google. First thing that came up was a six-year-old article from C-Sharp Corner. Was a simple file brower example that was pretty hard to follow. It was dated information anyway.

There was a very succint Microsoft knowledge base article that was more helpful. The example is doing something akin to what I'm looking to do: receiving a file or files dragged from elsewhere onto a listbox. It was pretty easy to put together and worked on first compile. Dropping an image from IE or Firefox over the list box would show the full path to the image's temporary file.

I wondered what else would be available so I went to the definition of DataFormats—there were twenty-one! They reminded me of clip board data types. Went to the MSDN summary and it covers how to create your own format for use on the clipboard.

Next I was wondering if I could access those other types. From the way I see Paste Special work in Microsoft applications, I suspected that the clipboard could make data available in multiple formats (unless they parse a rich source in your selected way). I was hoping the HTML format might include the URL source of the image I was dragging.

So I added another list box I called lstFormat and added the following code to the DragDrop event:
lstFormat.Items.Clear();
String[] formats = (string[])e.Data.GetFormats(false);
foreach (String format in formats)
{
lstFormat.Items.Add(format);
}
GetFormats has an overload that I specified false initially. By setting it to true, I got a larger list of all formats the source could be converted to. Very handy.

Now I need to spend some time figuring out how to work with these other formats.

Monday, June 18, 2007

C#: Modulus Operator

Wasn't sure what the modulus operator was in C#. Google came back with this snippet that shows how it can be used to determine even and odd integers:
The code uses C#'s Modulus Operator (%). How this works is the operator computes the remainder after it divides it's first operand by the second. In the case below, the number of controls (i.e. count) is divided by 2. If it's an even-numbered row there won't be a remainder and if it's an odd-numbered row....

if (_pnlMenuItems.Controls.Count % 2 == 0)
{
newMenuItem.BackColor = System.Drawing.Color.LightBlue;
}
else
{
newMenuItem.BackColor = System.Drawing.SystemColors.Window;
}
I just needed to know it's the percent sign; definitely miss the readability of QuickBasic's MOD command. While that snippet is pretty elementary, it's a good example of how to use the modulus.

Wednesday, May 2, 2007

ASP.NET: Returning the Name of the Current Page

Couldn't find a VB6 equivalent to Form.Name so I started Googling. Found someone on a random forum wanting to log a page that caused an error, minus the virtual or physical path.

The first response suggested: “Using Page.GetType().Name will return "YourPageName_aspx". Replace the underscore with a dot.” Interesting idea but the filename might contain other underscores, so I'd have to be sure to only replace that last one.

The second response provide a means of iterating through all of the ServerVariables:
using System.Collections.Specialized;

int loop1, loop2;
NameValueCollection coll;

// Load ServerVariable collection into NameValueCollection object.
coll = Request.ServerVariables;
// Get names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
Response.Write("Key: " + arr1[loop1] + "
");
String[] arr2 = coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
Response.Write("Value " + loop2 + ": " + arr2[loop2] + "
");
}
}
I wasn't aware of the NameValueCollection, so I'll have to look into that for future use. But I don't want to rely on accessing a named hash inside the server variables.

Since the first suggestion is a parse anyway, I decided to work with the seemingly appropriate FilePath property on the Request object instead of the type. The page doesn't use regular expressions for anything else, so I settled using String methods:
    private string GetPageName()
{
String localFile = Context.Request.FilePath;

if (localFile.Contains("/"))
{
Int32 position = localFile.LastIndexOf("/");
localFile = localFile.Substring(position + 1);
}
return localFile;
}