April 20, 2024, 12:25:45 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Adding a leading zero in output from the Calendar Control

Started by AdrianFox, January 10, 2014, 08:02:56 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

AdrianFox

Is it possible to format the output from the Calendar Control so that it is consistent with the output from the DateTimePicker and date$, both of  which are formatted as '01 January 2014'  (or can be with date$ of course)

I am trying to get consistency as my program has an automatic search which looks for the date clicked on in the Datetimepicker, which gives me the format '01 January 2014'.  Maybe the DateTimePicker format can be varied to give output without the leading zero instead?  But I cannot see from the Control Pak guide how this is done.

A quick search on the web gave me examples which indicated it could be done in principle though the examples were in other languages than Basic dialects.   

Thanks...

Adrian Fox

Bruce Peaslee

Is something like this what you want?


Sub GetDate(dialog dlg, int nCalendar), string

string     sDate
SYSTEMTIME sysTime

SendMessage dlg, MCM_GETCURSEL, 0, sysTime, nCalendar
sDate = _
using("####", sysTime.wYear)  + "-" +_
using("0##",  sysTime.wMonth) + "-" +_
using("0##",  sysTime.wDay)

Return sDate
EndSub
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

AdrianFox

Hi Bruce,

Thanks for your suggestion Bruce  but I'm still not sure exactly how to call this sub. 
Here is the relevant part of the code I am using that might help you explain how it could interact with your suggested sub routine.

Larry helped me to respond correctly to a click on the Calendar Control  by using the constants:

CONST MCN_FIRST=(-750)
CONST MCN_SELECT=(MCN_FIRST+4)

CASE @IDCONTROL
SELECT @CONTROLID

CASE MAIN_CALENDAR1
if @notifycode=MCN_Select
ccGetCurSel main,main_calendar1,m,d,y

gosub monthdefine  'Just a sub to change month integers to month names

SelectedDate =STR$(d)+" "+month$+" "+str$(y) ' gives me a date string without leading 0

setcontroltext main,main_EDIT2,SelectedDate   'displays the date string in a 'hidden' edit box

CONTROLCMD main,main_EDIT2, @EDSETSELECTION, 0, -1  'select the date text

                               'copy selected text from hidden editbox and paste into main edit box at caret position.
controlcmd  main,main_EDIT2,@EDCOPY       
controlcmd main, main_edit1,@edpaste


This all works absolutely fine but the output is a date in the format  '1 January  2014' .  What I am trying to achieve is output in the format 01 January 2014.   The DateTimePicker I use in a child window in the program gives me a date in that format.

Forgive me for not understanding how to go about calling your sub, but is it a completely different way of retrieving the date from the Calendar Control,  or should it be used in conjunction with some of the code I am already using.

When I tried call your sub in a way I thought just might work  MCM_GETCURSEL was rejected on compile as an undefined variable and my attempts at defining it in the include file didn't meet with any success.   

This all probably sounds like the ravings of a complete moron to you,  but I am only very slowly grasping the principles of how to do these things!  A few prods in the right direction  would be helpful.  Thanks for your time.


Sub GetDate(dialog dlg, int nCalendar), string  'Does dialog refer to the main dialog or window I am using? 

string     sDate
SYSTEMTIME sysTime

SendMessage dlg, MCM_GETCURSEL, 0, sysTime, nCalendar
sDate = _
using("####", sysTime.wYear)  + "-" +_
using("0##",  sysTime.wMonth) + "-" +_
using("0##",  sysTime.wDay)

Return sDate
EndSub


Adrian Fox

LarryMc

select @controlid
case main_CALENDAR1
if @notifycode=MCN_SELCHANGE
ccGetCurSel main, main_CALENDAR1, m, d, y 'gets month, day and year from the calendar
month$= monthdefine(m)  'just a routine to convert months to month names!
/* added */
string day$=using(" 0##", d)
SelectedDate=day$+" "+month$+" "+str$(y)
/*end added */
'get the existing text in the editbox
CurrentText=getcontroltext(main,main_edit1)
'add the current text to the date string
CurrentText=CurrentText+SelectedDate
'display the text plus the selected date in the editbox
setcontroltext main,main_EDIT1,CurrentText 
setfocus main,main_edit1
'following should place cursor at end of text in editbox
controlcmd main,main_edit1,@EDSETSELECTION,0,-3
endif
endselect
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Bruce Peaslee

Larry's way is simpler, but to answer your questions:

dlg is the dialog the contains the control and nCalendar is the control's number.

CONST MCM_FIRST = 0x1000
CONST MCM_GETCURSEL =   (MCM_FIRST+ 1)

I use a subroutine because I have more than one calendar and this makes maintenance less tricky. Also, my style is to push stuff away from the handler routines so that they remain lean and easier to read.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

AdrianFox

Many thanks both of you.  I will work through both solutions as a learning exercise and I much appreciate your time.

I have also found a different very simple solution to what I wanted to do which was simply to remove the leading 0 from my Datetimepicker string using right$ etc etc.  I feel stupid I didn't think of that first! 

Have a great weekend.  I hope the 'polar vortex' is now over.  Here we are enjoying balmy spring like temperatures still, which hardly seems fair!   ;)

Adrian Fox