.NET

Strip HTML Content from a String

To?string?HTML?tags,?use?HttpUtility???s?HTMLDecode?method?with?a?RegEx?expression.?HTMLDecode?is?available?in?System.Web?assembly. [email protected]”Decision?Making?Techniques:?Why?should?you?be?prepared”; string?plainText?=?string.Empty; if?(!string.IsNullOrWhiteSpace(htmlText)) { ?????plainText?=?Regex.Replace(HttpUtility.HtmlDecode(htmlText),[email protected]””,?string.Empty).Trim(); }

Show the Page Download Status in a WebBrowser Control

The Webbrowser control provides us with the “ProgressChanged” event. We can use the “CurrentProgress” and “MaximumProgress” Properties of the WEbBrowserProgressChangedEventArgs in this method to determine the percentage of the page that is downloaded. Drag and drop a Progressbar. Leave the name set to its default (progressbar1). Add an event handler

List all Environment Variables

The system variables are exposed through the GetEnvironmentVariables method in the System.Environment class. var environmentVariables = System.Environment.GetEnvironmentVariables();foreach (var ev in environmentVariables){ DictionaryEntry de = (DictionaryEntry) ev; Console.WriteLine(string.Format(“Key: {0}, Value: {1}”, de.Key, de.Value)); Console.WriteLine();}

Generate Random Passwords

Use the following method to generate random passwords. private string GenerateRandomPassword(int minLength, int maxLength) { StringBuilder randomPassword = new StringBuilder(); Random rand = new Random((int)DateTime.Now.Ticks); int randLength = rand.Next(minLength, maxLength); for (int i = 0; i { int charint = 0; while (charint == 0) charint = rand.Next(48, 58); randomPassword.Append((char)charint);

Check if an HttpRequest is an Ajax Request

A quick way to check if an HTTP Request is an Ajax request is by examining the X-Requested-With Header value. Please see below: bool isAjaxRequest = request.Headers[“X-Requested-With”] != null && request.Headers[“X-Requested-With”] == “XMLHttpRequest”;

Check if a String Contains a Phrase

The following snippet will allow you quickly determine whether or not a specified phrase exists in a string: If (!String.IsNullOrEmpty(STRINGTOCHECK.ToString()) && STRINGTOCHECK.ToString().ToLower().Contains(“PHRASETOSEARCHFOR”)){//Found}Else{//Not Found}

Determine Whether or Not a Form Is Open

Sometimes in the myriad of forms, it can become quite difficult figuring out whether or not a form has already been loaded and if is still open or not. To determine if a form has already been loaded into memory, you can use the following procedure that makes use of

Bypassing a Dialog Window’s DialogResult

Whenever you show a Modal Dialog a DialogResult is expected. This DialogResult can be OK, Cancel, Yes, No and so forth. Now, imagine this scenario: You show a Modal Dialog from inside another Form. The parent form expects a result, whatever that may be. Imagine further that a user forgot

Find and Activate External Windows

It is very easy to obtain access to an external running window and activate it. All you need is the actual title of the window ( the text written in the titlebar of the window) and FindWindow and SetForegroundWindow APIs. FindWindow gets access to the window in question. SetForegroundWindow activates

No more posts to show