發表文章

目前顯示的是有「Code Snippet」標籤的文章

取得Master Page的物件

有時候我們在開發asp.net 的時候會使用master page,但是如果master page上有某個物件或是控製元件,我們希望他進到不同的頁面時,可以有做一些改變,或是取值,這個要怎麼做呢? 方法如下。 在Master Page的c#檔中的PageLoad裡定義屬性 /// DropdownList   public string ddl_menu {         get { return nav.MenuValue;}         set { nav.MenuValue = value; }         } 在index.aspx中 給值 this.Master.ddl_menu = "1"; 取值 String temp = this.Master.ddl_menu;

筆記Entity SQL的用法

找了滿多篇如何做到像SQL 語法中的 inner join或是Left join,但看了還是不太熟,在這邊筆記一下。讓自已不要忘記。 如果有兩個Table一個是基本資料、一個是教育程度,在基本資料中關於學位的部份,只存放的教育程度的ID,以方便日後再增加其他的項目。如果想要把資料顯示的時候不要顯示basic時,在學位的部份只有id這邊可以使用inner join的方式。 var query = from basic in context.BasicInfo                    join ed in context.Educations on basic.Education equals ed.ID                   select new{                        姓名=basic.LocalName,                        學位=ed.Name                    } gvResult.Datasource = query; gvResult.DataBind();

c# 把List中重復的資料去掉

using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { // List with duplicate elements. List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); list.Add(3); list.Add(4); list.Add(4); list.Add(4); foreach (int value in list) { Console.WriteLine("Before: {0}", value); } // Get distinct elements and convert into a list again. List<int> distinct = list.Distinct().ToList(); foreach (int value in distinct) { Console.WriteLine("After: {0}", value); } } } Output Before: 1 Before: 2 Before: 3 Before: 3 Before: 4 Before: 4 Before: 4 After: 1 After: 2 After: 3 After: 4

求兩點的距離

CGFloat GetPointDistance (CGPoint p0, CGPoint p1) {     CGFloat xDiff = p0.x - p1.x;     CGFloat yDiff = p0.y - p1.y;          return sqrtf ((xDiff * xDiff) + (yDiff * yDiff)); }

在iphone中如何讓某個uiview順著某個圓心轉

首先定義,把角度轉換成弧度 #define RADIANS( degrees ) ( degrees * M_PI / 180 )  [NSTimer scheduledTimerWithTimeInterval:0.01f                                     target:self                                   selector:@selector(testeeee)                                   userInfo:nil                                    repeats:YES] ; -(void)  testeeee{  if (angle > 360) {         angle = 0;     }              angle++;     //    NSLog(@"%f",angle);     int r = 100;     //    testView.f...

單點讓UIView同時縮放與旋轉

UITouch *touch1 = [sortedTouches objectAtIndex:0]; //觸控移動的點         CGPoint beginPoint1 = *(CGPoint *)CFDictionaryGetValue(touchBeginPoints, touch1);         CGPoint currentPoint1 = [touch1 locationInView:self.superview];         double layerX = self.center.x;//指定UIView的中心點         double layerY = self.center.y;//指定UIView的中心點         double x1 = beginPoint1.x - layerX;         double y1 = beginPoint1.y - layerY;         double x2 = layerX - layerX;         double y2 = layerY - layerY;         double x3 = currentPoint1.x - layerX;         double y3 = currentPoint1.y - layerY;         double x4 = layerX - layerX;         double y4 = layerY - layerY;                  do...

Objective-C的數學運算函式

在寫程式的時候,常常需要用到一些數學運算,這邊整理一下常用的。 pow(x,y) : x 的 y 次方 sqrt(x) : x的平方根 floor(x) : 小於 x 的最大整數 ceil(x) : 大於 x 的最小整數 exp(x) : e 的 x 次方 log(x) : 以 e 為底 x 的對數值 log10(x) : 以10為底 x 的對數值 abs(x) : 整數 x 的絕對值 fabs(x) : 浮點數 x 的絕對值 srand(x), rand() : 產生亂數

c# 學習筆記 - 將英文字串中,每個單字第一個字母變大寫

將一串英文字,每個單字第一個字母轉換成大寫 using System; using System.Collections.Generic; using System.Web; /// <summary> /// Summary description for Tools /// </summary> public class Tools { public Tools() { // // TODO: Add constructor logic here // }     public static string UppercaseWords(string value)     {         char[] array = value.ToCharArray();         // Handle the first letter in the string.         if (array.Length >= 1)         {             if (char.IsLower(array[0]))             {                 array[0] = char.ToUpper(array[0]);             }         }         // Scan through the letters, checking for spaces.         // ... Uppercase the lowercase le...

將GLPaint的PaintView與底圖一起存成image

在xib檔中,有一個drawview,放在這個drawview下方再加上一個UIimageView,並把他的名字定為backgroundView。接下來把這個View設定IBOutlet到AppController上。 接下來我們可以設定一個save的funcation。 -(void)saveImage{//使用這個方法就可以把圖片全部合併起來,並寫入到Album裡面了。  UIImageWriteToSavedPhotosAlbum([self imageRepresentationForBackground], self, @selector(image:didFinishSavingWithError:contextInfo:), nil); } -(UIImage*) imageRepresentationForBackground {     UIImage* blendedImage=nil;     UIImageView* imageView = [[UIImageView alloc] initWithFrame:backgroundView.bounds];     imageView.image= backgroundView.image;     imageView.contentMode=backgroundView.contentMode;     UIImageView* subView   = [[UIImageView alloc] initWithImage:[drawingView imageRepresentation]];     [imageView addSubview:subView];     UIGraphicsBeginImageContext(imageView.frame.size);     [imageView.layer renderInContext:UIGraphic...

iOS 拍照以及從相簿取出照片

最近在開發camera and album 相關功能的app。將一些用法以及代碼筆記下來。 LCSViewController.h and LCSViewController.m #import @interface LCSViewController:UIViewController @property(nonatomic,retain)IBOutlet UIImageView *imageView; @property(nonatomic,retain)IBOutlet UIBarButtonItem *saveImageBotton; -(IBAction)showCameraAction:(id)sender;//拍照 -(IBAction)saveImageAction:(id)sender;//將照片存到相簿 -(IBAction)pickPhotoFromLibery:(id)sender;//從相簿取出照片 @end #import "LCSViewController.h" @interface LCSViewController () @end @implementation LCSViewController @synthesize imageView; @synthesize saveImageBotton; #pragma mark - Show camera -(IBAction)showCameraAction:(id)sender { UIImagePickerController *imagePickController=[[UIImagePickerController alloc]init]; //You can use isSourceTypeAvailable to check imagePickController.sourceType=UIImagePickerControllerSourceTypeCamera; imagePickController.delegate=self; imagePickController.allowsEditing=YES; imagePickController.showsCamer...

使用權重控制隨機選取

圖片
水果  |  Apple  | Orange | Banana | Mango ------------------------------------------ 權重  |   70    |   10   |   10   |   10 從這個表中,我們分派了各個權重,我們希望Apple最常被抽中,所以將權重設定較高的值。 因此我們從這個表來實現作法。 fruits = [[NSArray alloc] initWithObjects:@"apple",@"orange",@"banana",@"mango", nil]; //定義水果 weights = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:70],[NSNumber numberWithInt:10],[NSNumber numberWithInt:10],[NSNumber numberWithInt:10], nil]; //定義權重  int currentfruit=0;     NSMutableArray *weighedfruits = [[NSMutableArray alloc] init];         while (currentfruit<[fruits count]){ //step through each fruit[] element        for (int i = 0; i < [[weights objectAtIndex:currentfruit] intValue]; i++) {                  [weighedfruits addObject: [fruits objectAtIndex:currentfruit]]; ...

iOS 產生隨機亂數1~100

-(IBAction)generateNumber:(id)sender { int number = (arc4random()%100)+1; //Generates Number from 1 to 100. NSString *string = [NSString stringWithFormat:@"%i", number]; label.text = string }

NSTableView SelectedRow

圖片
原本一直以為NSTableView跟UITableView的作法會全部一樣,就一直試著要做選取某一列 但是就一直沒有辦法正確的把值選到,今天晚上光找這個解法就找了很久,後來終於有試 出來了,原本這個選取的動作跟iphone/ipad比起來簡易了許多,難怪這個資料不怎麼好找。 一開始我們要先把TableView建立一個IBoutlet到h檔中,這樣我們就可以去對tableview操作。 接下來就如下圖,我們把tableview建立連結到File'sOwner,我們事先已經建了一個方法叫做 - (IBAction)tableViewSelected:(id)sender; 把這兩個接口連結後,我們就可以對tableview點選去做一些事情了。 以下是使用方法的參考 - (IBAction)tableViewSelected:(id)sender {       NSLog(@"the user clicked row %ld",[tableViewMenu selectedRow ]); // selectedRow 這個方法就是指選到tableView的某一row了。 } // end tableViewSelected

CoreData 筆記 (2)

當開發Mac App並使用Coredata的話,要對某一筆資料做更新,可以使用下列的方法。 事先在xib檔中把arraycontroller建立一個iboutlet的連線到h檔中,這時候我們就可以直接 使用arraycontroller中的資料了。 - (IBAction)RestLearnRecord:(id)sender {// 重置資料          //將arraycontroller的陣列取出     NSMutableArray *ctarray = [CategoryArray arrangedObjects];     //選取某一個索引上的物件,並給於對應的型態  Category *inData = [ctarray objectAtIndex:SelectIndexs];     //直接對此物件做操作     [inData setRemember:[NSNumber numberWithInt:100]];     [inData setTotally:[NSNumber numberWithInt:900]];         NSError *error = nil;       //修改完資料後,要執行存檔的動作,這時候畫面上的資料就會被更新了     if (![[self managedObjectContext] save:&error]) { //此行一定要執行         NSLog(@"Failed to save - error: %@", [error localizedDescription]);     }else {         [tableViewMenu reloadData];     }    ...

在OpenGL的環境下把畫面存圖片 Code Snippet

在glpaint裡要把畫面存成照片需要加入以下的程式碼。 void ProviderReleaseData ( void *info, const void *data, size_t size ) { free((void*)data); } -(UIImage*) upsideDownImageRepresenation{ int imageWidth = CGRectGetWidth([self bounds]); int imageHeight = CGRectGetHeight([self bounds]); //image buffer for export NSInteger myDataLength = imageWidth* imageHeight * 4; // allocate array and read pixels into it. GLubyte *tempImagebuffer = (GLubyte *) malloc(myDataLength);          glReadPixels(0, 0, imageWidth, imageHeight, GL_RGBA, GL_UNSIGNED_BYTE, tempImagebuffer); // make data provider with data. CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, tempImagebuffer, myDataLength, ProviderReleaseData); // prep the ingredients int bitsPerComponent = 8; int bitsPerPixel = 32; int bytesPerRow = 4 * imageWidth; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInf...

Core Data 學習筆記

圖片
做到現終於了解Coredata是怎樣的操作了,之前一直以為會跟ios上的操作不一樣,但是後來 看完了Apple官方文件,發現到Apple其實真得很不錯,從Mac到ios上的用法都大致相同,很 多地方其實可以用ios的一些原理,反推回去,不過還是有些許的部份是不同的。 在Mac上的Coredata,是標榜著" write less code ",這可是這Coredata當時的發展目標,在Mac 上確實真的看到了,他真的要寫的code很少,幾乎是不太要寫到什麼代碼。很快就可以完成 一個資料庫的加入、移除、修改,這三大基本動作,就連關聯式資料庫的一些維護動作,他 也全都在背後全做好了。 到目前的階段大致上需要的功能都有了,接下來可以專心的想如何開發出Mac App了。 以下是基本操作的代碼 在這個測試檔中,使用的Entity Class就叫做"Entity",使用的Model也是叫做Entity,裡面只有 一個欄位就叫做"name",標記為紅色的部份就是需要自已替換上的部份。 - (IBAction)fetchRequest:(id)sender {//取出所有資料          NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];     NSEntityDescription *entity = [NSEntityDescription entityForName:@" Entity " inManagedObjectContext:[self managedObjectContext]];     [fetchRequest setEntity:entity];          NSError *error = nil;     NSArray *fetchedObjects = [ [self managedObjectContext] executeFetchRequest:fetchRequest error:...