Thursday, May 27, 2010

Online Quran Project

http://al-quran.info

This is a very useful site for all Muslims who wish to recite, read or understand Quran online while sitting in home or office. They have added numerous translations in all languages and are now working to add commentaries also. So far they have managed to upload only famous commentaries and I hope that they will upload more in all languages. The text is quite readable in all the languages and there are no typos or any sort of mistakes in digitizing such huge amounts of data.

I hope that the mods of this site keep up the good work and continue to provide this great service for the Muslims in an unbiased fashion as they have so far done. I hope that you like it and will become a frequent user of this service. May Allah bless all the people behind such a great work.
Aameen

Wednesday, July 15, 2009

Simple way to validate DataTypes

The importance of data validation can never be ignored by any programmer since we can never trust users to enter data in proper format. While special controls like combo box,listbox etc limit the users to the options provided there comes always some need to place in text boxes.
There are many methods to make sure that the data entered is in proper format. For instance you have a MaskedTextBox control that provides pre-defined data formats to compare the user input with. Also you can always use the System.Text.RegularExpressions namespace to provide validation.
But why go into complexities of making strange looking expressions when you want to provide validation for really simple things? What if you want the user to enter some Float, DateTime or even Int values?
Thanks to the .Net framework we can manage such tasks easily by taking a different route or perhaps I should say a short cut.

Lets C . . .

Assuming that we have a TextBox control named text1 on our windows form and we want to allow input in only double/float format.

All we have to do is to place a few lines to the submit event of the form or any other event as per required:

public void SomeEvent(object sender, EventArgs e)
{
try
{

double.Parse(text1.Text);

}
catch
{
MessageBox.Show("Please provide a valid data. Only floating point numbers allowed.");
}
}

Monday, July 13, 2009

Updating the User Interface in a thread safe manner in .Net

By default the user interface of any Windows application is managed on separate UI thread by the .Net framework. Apparently it appears to make an application's look and feel more responsive but sometimes it causes things to mess up a bit when developing an application in which there are a lot of multiple threads involved in updating the UI and the framework immediately throws an exception if you try to make other threads update the controls on the UI.
Thankfully there is a simple solution to this, seemingly, complex problem. The key here is to use .Net delegates or in "layman" terms delegate the task to be performed by one thread to the UI thread instead.
First, you would need to create a method that updates the UI as per required for instance:


public void UpdateUI(string anydata)
{

listBox1.Items.Add(anydata);

}

Then create a delegate and assign it the above method.
//Declaring delegate
public delegate void UpdateData(string anyData);

//The signature of your delegate must be similar to the UpdateUI method

and finally instantiating the delegate

UpdateData UpdateDelegate = new UpdateData(UpdateUI);

Now you may call this delegate in any method that any of your thread may be executing as under:

public void SomeThreadedMethod()
{
//some code...
.
.
this.Invoke(UpdateDelegate,anydata);
.
.
}

Saturday, July 11, 2009

Reading from and Writing to simple XML Files in .Net

While developing applications it is often required to store and communicate data using XML Files. The following XML Access Class is made with the purpose to simplify the process of accessing XML Files. The code is in C#.Net.


// Class for reading and writing to xml files

class XML_Access
{
private XmlDocument xdoc;
private XmlElement root;
private XmlNode xn;
private XmlNodeList nodelist;
private XmlAttributeCollection xattr;


// Constructor load settings from specified xml file eg: config.xml

public XML_Access(string filename)
{
xdoc = new XmlDocument();
xdoc.Load(filename);
}

// Read specified Element value from xml file

public virtual string ReadXMLConfig(string ElementName)
{
nodelist = xdoc.GetElementsByTagName(ElementName);
return nodelist[0].InnerText;

}


// Read an Elements attributes

public virtual double ReadXMLAttributes(string ElementName,string Attribute)
{
nodelist = xdoc.GetElementsByTagName(ElementName);
xattr=nodelist[0].Attributes;
return double.Parse(xattr[Attribute].Value);
}

// Returns specified element's specified attribute in String format

public virtual string ReadXMLAttributesToString(string ElementName, string Attribute)
{
nodelist = xdoc.GetElementsByTagName(ElementName);
xattr = nodelist[0].Attributes;
return xattr[Attribute].Value.ToString();
}


// Edit specific element in xml file

public virtual void WriteXMLConfig(string ElementName,string Value,string FileName)
{
root = xdoc.DocumentElement;
xn = root.SelectSingleNode(ElementName);
xn.InnerXml = Value;
xdoc.Save(FileName);
}

// Edit specific element's specified attributes in xml file

public virtual void WriteXMLAttributes(string ElementName,string Attribute, string Value,string FileName)
{
root = xdoc.DocumentElement;
xn = root.SelectSingleNode(ElementName);
xattr = xn.Attributes;
xattr[Attribute].InnerXml = Value;
xdoc.Save(FileName);
}
}


All you will need to do is to declare an object of this class and use its simplified methods in your application