473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Formula in Changed Event

I have a InfoPath form that I need to use an event handler to perform a
calculation. The calculation is derived from a excel formula that
calculates the second tuesday of the month when the user enters the month
(numeric) and the year. Where Year and Month are variables from a dropdown
box.

=DATE(Year,Month,1+((2-(2>=WEEKDAY(DATE(Year,Month,1),2)))*7+(2-WEEKDAY(DATE(Year,Month,1),2))))

What is the best way to execute this formula in c#?

Alan

Jan 11 '08 #1
2 1624
Alan wrote:
I have a InfoPath form that I need to use an event handler to perform a
calculation. The calculation is derived from a excel formula that
calculates the second tuesday of the month when the user enters the month
(numeric) and the year. Where Year and Month are variables from a dropdown
box.

=DATE(Year,Month,1+((2-(2>=WEEKDAY(DATE(Year,Month,1),2)))*7+(2-WEEKDAY(DATE(Year,Month,1),2))))

What is the best way to execute this formula in c#?
Well, not too sure of the BEST way, but:

DateTime now = DateTime.Now;

// d becomes the first day of the month
DateTime d = now.AddDays((now.Day * -1) + 1);

// Figure out if it's before or after this.
int diff = DayOfWeek.Tuesday - d.DayOfWeek;

// If it's negative, add 7 to it, otherwise
// just add it straight up.
d = d.AddDays((diff >= 0) ? diff : diff + 7);
That will work. You can trim down the code a bit by utilizing the fact that the
AddDays function returns the changed DateTime.
Chris.
Jan 11 '08 #2
Chris,

Thanks I give this a try.

"Chris Shepherd" wrote:
Alan wrote:
I have a InfoPath form that I need to use an event handler to perform a
calculation. The calculation is derived from a excel formula that
calculates the second tuesday of the month when the user enters the month
(numeric) and the year. Where Year and Month are variables from a dropdown
box.

=DATE(Year,Month,1+((2-(2>=WEEKDAY(DATE(Year,Month,1),2)))*7+(2-WEEKDAY(DATE(Year,Month,1),2))))

What is the best way to execute this formula in c#?

Well, not too sure of the BEST way, but:

DateTime now = DateTime.Now;

// d becomes the first day of the month
DateTime d = now.AddDays((now.Day * -1) + 1);

// Figure out if it's before or after this.
int diff = DayOfWeek.Tuesday - d.DayOfWeek;

// If it's negative, add 7 to it, otherwise
// just add it straight up.
d = d.AddDays((diff >= 0) ? diff : diff + 7);
That will work. You can trim down the code a bit by utilizing the fact that the
AddDays function returns the changed DateTime.
Chris.
Jan 16 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Lance Johnson | last post by:
I would like for when a tab page changes, that my controls on the previous tab page would receive a visiblechanged event or some other event so they are aware of that. We have db locks in some of...
2
by: carmen | last post by:
i have the folowing code Dim y As New frmCobranz Dim MyReport As New RPTCobranz MyReport.DataDefinition.RecordSelectionFormula = "{CobH.RelNbr}= y.txtNoRelac.Text...
0
by: RJN | last post by:
Hi I have a main report and a sub report. I have a formula field on the main report and one on the sub report. I want the formula in the subreport to be evaluated after the formula in the main...
0
by: RJN | last post by:
Hi Sorry for posting this message again. I have a main report and a sub report. I have a formula field on the main report and one on the sub report. I want the formula in the subreport to be...
0
by: rjn | last post by:
Hi I have a main report in which I have inserted a sub report. I have a formula field on the main report and one on the sub report. I want the formula in the subreport to be evaluated after the...
0
by: Orange2p | last post by:
Hi, I imported a table from Paradox to SQL Server and I am using this table for a report generation. That table in paradox contains a field "Entry" which is of type Time. When this table was...
11
by: Brian VanPelt | last post by:
I am not a very experienced VB user, but I was trying to make a form that a user could input a formula for summation. For example, I would like the user to input the beginning and end values of...
3
by: VJ | last post by:
We have a form where there is a like 10-15 fields and it performs a length formula calucation each time field value changes. Using validate event is not a option as validate wont fire on Menu click...
1
by: Freud52 | last post by:
y = 1E-07x6 - 0.0001x5 + 0.0147x4 - 0.635x3 + 12.906x2 - 115.76x + 401.3 this is a formula for a percentile score. the "x" represents th raw score and the number after it is supposed to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.