Friday, April 22, 2011

Set UIColor from Hex Color in iPhone

We define the HexColor in our .h file

#define HexColor(color) [UIColor
colorWithRed:((color)&0xFF)/255.0
green:((color>>8)&0xFF)/255.0
blue:((color>>16)&0xFF)/255.0
alpha:((color>>24)&0xFF)/255.0]


Then we can set the color from the hex color string

unsigned int colorValue;

[[NSScanner scannerWithString: colorString] scanHexInt: &colorValue];
UIColor *color = HexColor(colorValue);


being the colorString AARRGGBB where

AA = alpha
RR = red
GG = gren
BB = blue

Another options I've seen on the Internet


// As a C function:
UIColor* UIColorFromRGB(NSInteger rgbValue) {
return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0
green:((float)((rgbValue & 0xFF00) >> 8))/255.0
blue:((float)(rgbValue & 0xFF))/255.0
alpha:1.0];
}



// As an Objective-C function:
- (UIColor *)UIColorFromRGB:(NSInteger)rgbValue {
return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0
green:((float)((rgbValue & 0xFF00) >> 8))/255.0
blue:((float)(rgbValue & 0xFF))/255.0
alpha:1.0];
}



Cheers!

Wednesday, April 13, 2011

Combobox in datagridviews binding classes and structures

Hi, my problem today is how to show the list of data from an structure into a datagridview using combobox in some columns.

First of all I was surfing on the internet and I found this for enums:

//CF: set to null before setting it to the current to
//clear out previous results
this.bindingSource1.DataSource = null;

this.bindingSource1.DataSource = MyDal.GetDataTable();

void _formatStatusColumn()
{
DataTable dt = new DataTable("Status");

//CF: actually how it's stored in the db.
dt.Columns.Add("Status", typeof(int));
dt.Columns.Add("Status_Name", typeof(string));

//CF: this way I can add or remove enum values, and
//the combo will always reflect the correct values.
foreach(int index in Enum.GetValues(typeof(Status)){
dt.Rows.Add(index, Enum.GetName(typeof(Status), index));
}

//CF: now for the UI column
DataGridViewComboBoxColumn statusColumn =
new DataGridViewComboBoxColumn();

//CF: seems to control where the column is placed.
statusColumn.DisplayIndex = 3;
statusColumn.HeaderText = "Status";

statusColumn.DataPropertyName = "Status";
statusColumn.DataSource = dt;
statusColumn.DisplayMember = "Status_Name";
statusColumn.ValueMember = "Status";

this.dataGridView1.Columns.Add(statusColumn);
}


This was very useful but I need something easier. I have to read a list and show the field in the combobox, so I've changed the code like that:

public void FormatResidenceColumn()
{
if (this.dataGridView1.Columns.Count > 0)
{
DataTable dt = new DataTable("Residence");

dt.Columns.Add("Residence", typeof(int));
dt.Columns.Add("Residence_Name", typeof(string));

this.dataGridView1.Columns.Remove("Residence");

if (Globals.ListOfResidences.Count > 0)
{
foreach (Residence obj in Globals.ListOfResidences)
{
dt.Rows.Add(obj.Id - 1, obj.Alias);
}
}
else
{
// Empty
dt.Rows.Add(0, "Empty");
}

//CF: now for the UI column
DataGridViewComboBoxColumn combColumn =
new DataGridViewComboBoxColumn();

//CF: seems to control where the column is placed.
combColumn.DisplayIndex = 4;
combColumn.HeaderText = "Residencia";

combColumn.DataPropertyName = "Residence";
combColumn.DataSource = dt;
combColumn.DisplayMember = "Residence_Name";
combColumn.ValueMember = "Residence";

this.dataGridView1.Columns.Add(combColumn);
}
}


where I use an global variable named ListOfResidence in this example.

Hope help!!

Sunday, April 3, 2011

How to launch Apple Store from my iphone code

Hi,

Today I will show you a simple function that launches the App Store from your application.

First of all you need to know your app's link for example looking for it in iTunes. And then write a code like this:


-(IBAction) gotoFlightInstrument {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/es/app/instrumental-flight-vuelo/id413551896?mt=8"]];
}


where
"http://itunes.apple.com/es/app/instrumental-flight-vuelo/id413551896?mt=8

is my iTunes app link.

Hope help!

Bye