Tuesday, May 25, 2010

DataView sort no sorting

A common problem with developers using System.Data.DataView from ADO.Net when using the sorting feature, they think that the information is sorted physically but is not true, because the object creates only a view of the information, if we point to the Table of the view we are going to the original information without the sort applied.
See the example:

System.Data.DataTable dt = obj.getInfoAsTable();
System.Data.DataView dv = dt.DefaultView();
dv.Sort = “[name], [dateOpt] DESC”;


This is an incorrect way to access the info because we are going back to the original info without the sorting:

Foreach(System.Data.DataRow r in dv.Table.Rows)
{
String name = r[“name”].toString();
}


This is the correct way of getting the sorted data:

Foreach(System.Data.DataRow r in dv.ToTable().Rows)
{
String name = r[“name”].toString();
}


The rows never physically get sorted. The view provides a sorted view of the data. The table never changes. You have to access the data through the view
to see it sorted. See the diagram below:



Tuesday, May 18, 2010

Macro strings in file registry settings


When we hard code some paths for a CAB file we can have some problems if the user is installing in the application on an external memory or if it is installed on a different language.
To avoid this problem we can use Macro Strings.
They way these macro strings work is that if you specify a registry value like %InstallDir% the CAB installation process will replace the %InstallDir% part of your string with the device’s specified installation directory.

This is a list of some of the string macros on English devices:


%InstallDir% [directory application installed into]
%AppName% [name of application]
%CE1% \Program Files
%CE2% \Windows
%CE4% \Windows\StartUp
%CE5% \My Documents
%CE8% \Program Files\Games
%CE11% \Windows\Start Menu\Programs
%CE14% \Windows\Start Menu\Programs\Games
%CE15% \Windows\Fonts
%CE17% \Windows\Start Menu

Shortcuts in windows mobile


When you create a shortcut on windows mobile, it will be showed up in different parts depending on the folder where you paste it, these are some folders:
\Windows\StartUp
shortcuts to applications placed in this directory will automatically be started whenever the Windows Mobile device is reset.

\Windows\Start Menu
shortcuts placed in this directory will appear in the Windows start menu.

\Windows\Start Menu\Settings
Shortcuts placed in this folder will appear on the System tab of the Settings application that is accessible via the Start Menu.



Friday, May 14, 2010

Do something before the form is closed


In order to do something before the form is closing we can use the next event in the form:
public event FormClosingEventHandler FormClosing

The FormClosing event occurs as the form is being closed. When a form is closed, it is disposed, releasing all resources associated with the form.

The following code example demonstrates the use of this member. In the example, an event handler reports on the occurrence of the FormClosing event:

private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "CloseReason", e.CloseReason );

messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Cancel", e.Cancel );

messageBoxCS.AppendLine();
messageBox.Show(messageBoxCS.ToString(), "FormClosing Event" );

}

Call a parent functions from child form



In order to call a parent function from a child form you have to pass the form1 instance to form2 in the constructor.


Form1:

namespace CallingForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 tmp = new Form2(this);
tmp.ShowDialog();
}

internal void msg()
{
MessageBox.Show("Hello from parent form");
}

}
}



Form 2:

namespace CallingForm
{
public partial class Form2 : Form
{
private Form1 frmParent;

public Form2(Form1 frm1)
{
InitializeComponent();
frmParent = frm1;
}

private void button1_Click(object sender, EventArgs e)
{
frmParent.msg();
}
}
}