Dailycode.info

Short solution for short problems

iOS: Hide keyboard on edit begin before picker view

I wanted to show a picker view in a popup when I click in a text field. But I do not want to show the keyboard. Now this was not a problem when you click directly in the textfield. But when coming from another textfield with the keyboard enabled, the keyboard stayed visible. 

I solved it with a simple call before I show the picker view:

- (IBAction)LocationDidBegin:(id)sender

{

    [self.txtLocation performSelector:@selector(resignFirstResponder) withObject:nil afterDelay:0.2];

    [selfShowLocationPicker];

}

So that did the trick.


iOS: cannot find manuel segue

I had an application with a dashboard with dynamic number of button. Based on the button description, a segue would be triggered. This works fine. But at a moment, a certain push segue was not found. So I triple checked the name, debugged and debugged, but could not find the problem. Also the application did no respond to other segues anymore. 

The problem was that I copy a view controller on the story board, then changed it and created a new manuel segue to it. Were the view controller of which I copied the new VC from was also referring. This was causing the problem. I guess there is somewhere still a reference because it was a copy. 

Now I solved it by simply adding a new view controller to the story board, then copy paste all controls to this and changed the view controller class. Deleted the first copy, then set the manual segue to the new view controller and everything worked. I had to hook up all the outlets and events off course. 


Read and write windows forms textboxes into DTO

Without using any kind of existing binding protocols, these functions will fill the controls with the corresponding fields from a class (DTO in this case).

The most important requirement in this case is that you name the textfields like your DTO properties. you can use a random prefix for the text fields. Contain the text fields in a panel or group box. Then for eg. each group box use a different container. In my case I had several tab pages, for each tab page I use a prefix txt1 up to txt5. So a text field is called txt1CREATED or ...

The first function is to Fill the fields on the form. You provide a DTO class, a container and a number that is equal to the number of prefix characters.

    Private Sub FillDataFieldsOnForm(Of T)(ByVal DTO As T, ByVal collection As Control.ControlCollection, ByVal i As Short)

        Dim myType As Type = GetType(T)

 

        For Each txt In collection

            If (TypeOf txt Is DevExpress.XtraEditors.TextEdit) Then

                Try

                    Dim name As String = DirectCast(txt, DevExpress.XtraEditors.TextEdit).Name.Substring(i)

                    Dim myProp As PropertyInfo = myType.GetProperty(name)

 

                    DirectCast(txt, DevExpress.XtraEditors.TextEdit).EditValue = myProp.GetValue(DTO, Nothing)

                Catch ex As Exception

                    DirectCast(txt, DevExpress.XtraEditors.TextEdit).EditValue = ex.Message

                End Try

            End If

        Next txt

 

    End Sub

Here you can see an implementation of this function:

FillDataFieldsOnForm(kKna1, grpHeader.Controls, 3)

Now when you want to read the controls, its equally easy:

ReadDataFieldsOnForm(Of KNB1)(kKnb1, grpCustomerMasterData, 4)

The function for to do this is:

    Private Sub ReadDataFieldsOnForm(Of T)(ByVal DTO As T, ByVal grp As DevExpress.XtraEditors.GroupControl, ByVal i As Short)

        Dim myType As Type = GetType(T)

 

        For Each txt In grp.Controls

            If (TypeOf txt Is DevExpress.XtraEditors.TextEdit) Then

                Try

                    Dim name As String = DirectCast(txt, DevExpress.XtraEditors.TextEdit).Name.Substring(i)

                    Dim myProp As PropertyInfo = myType.GetProperty(name)

 

                    myProp.SetValue(DTO, DirectCast(txt, DevExpress.XtraEditors.TextEdit).EditValue, Nothing)

                Catch ex As Exception

                End Try

            End If

        Next txt

 

    End Sub


Grabbing the image from AXIS (P1346) Network Camera

Thanks to the wonderful API this camera has, you do not need DLL's or Active X controls. You can just get an image for example by the API.

Here an example to get the image from the camera using c# (this can be used in a service, web service or other applications):

Uri test = new Uri("http://192.0.0.100/jpg/image.jpg");

WebClient wc = new WebClient();

wc.Credentials = new NetworkCredential("MyUser", "MyPassW0rd");

wc.DownloadFile(test, @"C:\TakePicture\picFromWS.jpg");

wc.Dispose(); 

 

 


iOS: Using multiple storyboards within a navigation controller.

I had a big story board file and I wanted to start to split it up in multiple storyboards containing views with shared functionality. First I found out how to open a different storyboard modally. But since the application uses a navigation controller at certain points, I also wanted to keep the navigation when opening some other storyboards. 

So iOS makes it really simple:

First instantiate the storyboard you want to navigate to (bundle can be nil if the story board is in the same bundle):

UIStoryboard * planningSB = [UIStoryboardstoryboardWithName:@"PlanningStoryboard"bundle:nil];

Then instantiate the initial view controller:

UIViewController * planningVC = [planningSB instantiateInitialViewController];

And last, just push this view controller into the view controllers collection of the current navigation controller.

[self.navigationControllerpushViewController:planningVC animated:YES];

Now the View controller of the second storyboard is loaded in the navigation controller. When pushed on the back arrow on the top, it will simply go back to the previous view controller and the second story board will be deallocated when the reference to its initial view controller is released.

Next is another example where I change the transition style:

UIStoryboard * planningSB = [UIStoryboardstoryboardWithName:@"PlanningStoryboard"bundle:nil];

UIViewController * planningVC = [planningSB instantiateInitialViewController];

 

[UIView transitionWithView:self.view duration:0.8 options:UIViewAnimationOptionTransitionCrossDissolve

                        animations:^{

                            [self.navigationController pushViewController:planningVC animated:NO];

                        }

                        completion:NULL];

 

 In my case I had a camera view that could be called from several views, I put this on a new storyboard and this works great. Since the camera view was also pointing to the search order view, I had to put the search order view also on a separate storyboard. Again my main story board got simpler.

So have a main story board, and separate as much as you can onto separate storyboards. I made a general class StoryBoardNavigation where I centralized all functions to call child storyboards.

Here is an example of this:

This function navigates to the storyboard called OrderLookupStoryboard and opens the view in start position.

+(void)NavigateToOrderLookupFrom:(UIViewController*)vc {UIStoryboard* sb =[UIStoryboard storyboardWithName:@"OrderLookupStoryboard" bundle:nil];UIViewController* vcTo =[sb instantiateInitialViewController];[UIView transitionWithView:vc.view duration:0.8 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{[vc.navigationController pushViewController:vcTo animated:NO];} completion:NULL];}

This function passes some parameters to the storyboard.

+(void)NavigateToCameraFrom:(UIViewController*)vc WithSelectedZone:(int)selectedZone {UIStoryboard* sb =[UIStoryboard storyboardWithName:@"CameraStoryboard" bundle:nil];MediaCapture* vcTo =[sb instantiateInitialViewController]; vcTo.selectedZone = selectedZone; vcTo.zoneSet = YES;[UIView transitionWithView:vc.view duration:0.8 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{[vc.navigationController pushViewController:vcTo animated:NO];} completion:NULL];}

This function opens the view in module transition style:

+(void)NavigateToSignatureFrom:(UIViewController*)vc withSignee:(NSString*)Signee{UIStoryboard* sb =[UIStoryboard storyboardWithName:@"SignatureStoryboard" bundle:nil];UIViewController* vcTo =[sb instantiateInitialViewController];((SignatureVC*) vcTo).Signee=Signee;[vcTo setModalTransitionStyle:UIModalTransitionStylePartialCurl];[vc presentViewController:vcTo animated:YES completion:nil]; 

} 

 

 

 

 


Locating the Configuration file of an VSTO (excel) project

I was contacting WCF Webservices in an Excel VSTO project. Deploying this to the customers, they could use VBA as much as they want without exposing code to contact the database.  

My problem was that the message size was the default and therefore not enough. It was already deployed to a customer and he had already made a lot of changes. Re deploying would mean losing these changes. So I was planning on changing the binding on the client and server. There came the challenge. Changing it on the server was no problem, but finding the config file on the client was a BIG problem.

The path was located at: C:\Users\mderaeve\AppData\Local\Apps\2.0\V154RLC4.47N\73GCM0NZ.LX6\poed..vsto_268646e17c0b18d5_0001.0000_9bd2d9b28f73af10

Fortunatelly, there are not that many directories in the Local\Apps\2.0 so you can find the project fast. I found the path by putting this piece of code in the project and show it somewhere in the excel. Then I installed it and the path was printed out.

//Get the assembly information

System.Reflection.Assembly assemblyInfo = System.Reflection.Assembly.GetExecutingAssembly();

                   

//Location is where the assembly is run from

string assemblyLocation = assemblyInfo.Location;

 

//CodeBase is the location of the ClickOnce deployment files

Uri uriCodeBase = new Uri(assemblyInfo.CodeBase);

string ClickOnceLocation = Path.GetDirectoryName(uriCodeBase.LocalPath.ToString());

Excel._Worksheet activeSheet = ((Excel._Worksheet)Application.ActiveSheet);

 

activeSheet.Cells[4, 2] = ClickOnceLocation;


iOS: Performing a segue using a splash screen.

My application was using a dash board, from where the user navigates through the application. Works file until some large forms take more then a second or so to load. Then it might appear that the form is doing nothing. So a simple solution is to show a waiting screen while performing the segue. The only little trick is that you call the perform segue with a delay and before you do that show your waiting screen. Else the thread will wait to output to GUI until the segue is done and you will not see the splash screen.

So here just a little piece of code:

-(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath

{

    @try

    {

        NSString * label = [store.HomeButtonsArray objectAtIndex:indexPath.item];

        //Show the waiting popup (Change it here to match your waiting popup)

        hud = [GeneralFunctionsGetMBProgressHUDForView:self.view];

        hud.detailsLabelText = [[storeTranslate: @"$MOB$Loading"] stringByAppendingString:[@" : "stringByAppendingString:label]];

        AudioServicesPlaySystemSound(0x450);

        

        [self performSelector:@selector(segueWithLabel:) withObject:label afterDelay:0.2];

        //[self performSegueWithIdentifier:[NSString stringWithFormat:@"DashB%@",label] sender:nil];

    }

    @catch (NSException *exception) {

        

    }

    @finally {

        

    }

}

 

-(void) segueWithLabel:(id) label

{

    [selfperformSegueWithIdentifier:[NSStringstringWithFormat:@"DashB%@",label] sender:nil];

}

 
if a collection view item is pressed, the segue starts after 0.2 seconds, but first a waiting popup will be send to the screen.
Then when the view disappears, I hide the waiting popup:
 

-(void) viewDidDisappear:(BOOL)animated

{

    [MBProgressHUDhideAllHUDsForView:self.viewanimated:YES];

}