Satish Vemula

in

November 2006 - Posts

Role of DockStyle property when adding controls

I recently ran into scenario where I had to rearrange some controls in an existing form. In a form, I had a splitter, topGrid and bottomGrid. The DockStyle for topGrid was set to Top, for splitter, it was set to top and for bottomGrid it was set to Fill. I had to change the dockstyle of topGrid to Fill and bottomGrid to bottom. As soon as I changed it, the topGrid would span the whole form and thus, I could not see a part of it. I had no idea why this was happening. I tried different things like updating the values in the code (and not in design mode). I even switched the grid positions to see if something was wrong with the layout itself but the bottomGrid always worked fine and seemed to stick to it's position.

   I almost gave up and was on the verge of putting them in separate panels when I realized that this weird behavior is being caused by the order the controls were added to the form. The control that is added last takes the topmost priority. And when I looked in the generated code, sure enough, the topGrid was the last one added and hence whenever I set it's DockStyle to Fill, it would occupy the whole grid and then the other controls would be added on top of it.

So I went into the generated code and manually rearranged the order in which controls are added and it worked exactly as I wanted.

You can find more info at:

http://www.bluevisionsoftware.com/WebSite/TipsAndTricksDetails.aspx?Name=DockStyle

http://msdn2.microsoft.com/en-us/library/system.windows.forms.dockstyle.aspx

Happy Coding!

Sending Http Authentication Request from a Windows Form

 

If you need to perform authentication using HttpWebRequest from a Windows application, you can do it the following way:

 

// Prepare the data that needs to be sent

string postData = "userID=" + HttpUtility.UrlEncode(userID);

postData += "&password=" + HttpUtility.UrlEncode(_password);

byte[] data = ASCIIEncoding.ASCII.GetBytes(postData);

 

// Prepare web request

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(myURL);

 

// Add the required headers

webRequest.Method = "POST";

webRequest.ContentType = "application/x-www-form-urlencoded";

 

// Since we are sending data, specify the content and write to the stream

webRequest.ContentLength = data.Length;

Stream newStream = webRequest.GetRequestStream();

// Send the data.

newStream.Write(data, 0, data.Length);

newStream.Close();

 

// Retrieve the response

HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

XmlTextReader reader = new XmlTextReader(webResponse.GetResponseStream());

XmlDocument doc = new XmlDocument();

doc.Load(reader);

 

// And then read the XmlDocument

 

The above immplementation is pretty straightforward. If you plan to send the credentials in the header, you can use Basic Authentication. This should only be used when sending the request over SSL as the information is still sent openly. Using SSL ensures that the contents of this request are encrypted. This can be done in the following way:

 

//Prepare the Authentication parameters

String auth = userID + ":" + password;

byte[] binaryData = Encoding.UTF8.GetBytes(auth);

auth = Convert.ToBase64String(binaryData); // Using base64 encoding

auth = "Basic " + auth; //This specifies it to use Basic Authentication

 

// Prepare web request

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(myURL);

 

// Add the standard headers

webRequest.Method = "POST";

webRequest.ContentType = "application/x-www-form-urlencoded";

webRequest.ContentLength = 0;

 

// Add this additional header

webRequest.Headers[“AUTHORIZATION”] = auth;

 

HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

XmlTextReader reader = new XmlTextReader(webResponse.GetResponseStream());

XmlDocument doc = new XmlDocument();

doc.Load(reader);

 

This is a more secure call!

 

You can find more information about HttpWebRequest and Headers at:

 

http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

 

http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.headers.aspx