Wednesday, October 18, 2006

Webresource.axd or WebForm_PostBackOptions undefined problem

If would get this error if you are missing reference to Webresource.axd file.
Create an empty file with name as "Webresource.axd" and copy it to web server wwwroot and your application root folder.It should work now.

Friday, June 02, 2006

SelectALL in Compact Framework

When you want to select all the text in a textbox, you are more likely to use SelectAll function of the textbox in the GotFocus event. Ironically, once this event is fired the text is unselected. The work around is to use a timer and in timer event call SelectAll function.

private void textBox1_GotFocus(object sender, EventArgs e)
{
//Use a timer to select all
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Interval = 10;
t.Tick += new EventHandler(t_Tick);
t.Enabled = true;
}
}
///
/// Event to select full text in textbox
///
///
///
private void t_Tick(object sender, EventArgs e)
{
((System.Windows.Forms.Timer)sender).Enabled = false; // Stop the timer
// Select all the text
textBox1.SelectAll();
}

Monday, February 06, 2006

Converting HTML text to Word

The easiest way is to copy html text to clipboard and save to word.
But when you copy an html to clipboard using copy or cut, windows add initial lines which indicates
start and end of HTML, Selection and Fragment. So, to handle this we need to
create text to be copied into compatble to above requirement

The code for this is

// Save text into clipboard
Object m = System.Reflection.Missing.Value;
DataObject clipDO = new DataObject();
// Convert into HTML with all tags before adding to clipboard
clipDO.SetData(DataFormats.Html, HtmlClipboardData(strHTML));
Clipboard.SetDataObject(clipDO, true); // Save to clipbaord

object typeHtml = (object)Word.WdPasteDataType.wdPasteHTML;
// Save from clipbaord to word
// Here s is word selection
s.PasteSpecial(ref m, ref m, ref m, ref m, ref typeHtml, ref m, ref m);



Now, convert normal html text to clipboard one.

///
/// Convert to proper html like
/// Version:1.0
///StartHTML:000000137
///EndHTML:000000204
///StartFragment:000000149
///EndFragment:000000180
///StartSelection:000000149
////EndSelection:000000180
///
///
///File
///
///
///
///
///
///
private static string HtmlClipboardData(string html)
{
StringBuilder sb = new StringBuilder();
Encoding encoding = Encoding.UTF8;
string Header = @"
Version: 1.0
StartHTML: {0:000000}
EndHTML: {1:000000}
StartFragment: {2:000000}
EndFragment: {3:000000}
";
string HtmlPrefix = @"







";
HtmlPrefix = string.Format(HtmlPrefix, encoding.WebName);

string HtmlSuffix = @"



";

// Get lengths of chunks
int HeaderLength = encoding.GetByteCount(Header);
HeaderLength -= 16; // extra formatting characters {0:000000}
int PrefixLength = encoding.GetByteCount(HtmlPrefix);
int HtmlLength = encoding.GetByteCount(html);
int SuffixLength = encoding.GetByteCount(HtmlSuffix);

// Determine locations of chunks
int StartHtml = HeaderLength;
int StartFragment = StartHtml + PrefixLength;
int EndFragment = StartFragment + HtmlLength;
int EndHtml = EndFragment + SuffixLength;

// Build the data
sb.AppendFormat(Header, StartHtml, EndHtml, StartFragment, EndFragment);
sb.Append(HtmlPrefix);
sb.Append(html);
sb.Append(HtmlSuffix);

//Console.WriteLine(sb.ToString());
return sb.ToString();
} #endregion