How to call a C# Class from a C# Form - c#

This is my class:
using System;
using System.Collections.Generic;
using System.Text;
namespace Num2Wrd
{
public class NumberToEnglish
{
public String changeNumericToWords(double numb)
{
String num = numb.ToString();
return changeToWords(num, false);
}
public String changeCurrencyToWords(String numb)
{
return changeToWords(numb, true);
}
public String changeNumericToWords(String numb)
{
return changeToWords(numb, false);
}
public String changeCurrencyToWords(double numb)
{
return changeToWords(numb.ToString(), true);
}
private String changeToWords(String numb, bool isCurrency)
{
String val = "", wholeNo = numb, points = "", andStr = "", pointStr = "";
String endStr = (isCurrency) ? ("Only") : ("");
try
{
int decimalPlace = numb.IndexOf(".");
if (decimalPlace > 0)
{
wholeNo = numb.Substring(0, decimalPlace);
points = numb.Substring(decimalPlace + 1);
if (Convert.ToInt32(points) > 0)
{
andStr = (isCurrency) ? ("and") : ("point");// just to separate whole numbers from points/Rupees
endStr = (isCurrency) ? ("Rupees " + endStr) : ("");
pointStr = translateRupees(points);
}
}
val = String.Format("{0} {1}{2} {3}", translateWholeNumber(wholeNo).Trim(), andStr, pointStr, endStr);
}
catch
{
;
}
return val;
}
private String translateWholeNumber(String number)
{
string word = "";
try
{
bool beginsZero = false;//tests for 0XX
bool isDone = false;//test if already translated
double dblAmt = (Convert.ToDouble(number));
//if ((dblAmt > 0) && number.StartsWith("0"))
if (dblAmt > 0)
{//test for zero or digit zero in a nuemric
beginsZero = number.StartsWith("0");
int numDigits = number.Length;
int pos = 0;//store digit grouping
String place = "";//digit grouping name:hundres,thousand,etc...
switch (numDigits)
{
case 1://ones' range
word = ones(number);
isDone = true;
break;
case 2://tens' range
word = tens(number);
isDone = true;
break;
case 3://hundreds' range
pos = (numDigits % 3) + 1;
place = " Hundred ";
break;
case 4://thousands' range
case 5:
case 6:
pos = (numDigits % 4) + 1;
place = " Thousand ";
break;
case 7://millions' range
case 8:
case 9:
pos = (numDigits % 7) + 1;
place = " Million ";
break;
case 10://Billions's range
pos = (numDigits % 10) + 1;
place = " Billion ";
break;
//add extra case options for anything above Billion...
default:
isDone = true;
break;
}
if (!isDone)
{//if transalation is not done, continue...(Recursion comes in now!!)
word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos));
//check for trailing zeros
if (beginsZero) word = " and " + word.Trim();
}
//ignore digit grouping names
if (word.Trim().Equals(place.Trim())) word = "";
}
}
catch
{
;
}
return word.Trim();
}
private String tens(String digit)
{
int digt = Convert.ToInt32(digit);
String name = null;
switch (digt)
{
case 10:
name = "Ten";
break;
case 11:
name = "Eleven";
break;
case 12:
name = "Twelve";
break;
case 13:
name = "Thirteen";
break;
case 14:
name = "Fourteen";
break;
case 15:
name = "Fifteen";
break;
case 16:
name = "Sixteen";
break;
case 17:
name = "Seventeen";
break;
case 18:
name = "Eighteen";
break;
case 19:
name = "Nineteen";
break;
case 20:
name = "Twenty";
break;
case 30:
name = "Thirty";
break;
case 40:
name = "Fourty";
break;
case 50:
name = "Fifty";
break;
case 60:
name = "Sixty";
break;
case 70:
name = "Seventy";
break;
case 80:
name = "Eighty";
break;
case 90:
name = "Ninety";
break;
default:
if (digt > 0)
{
name = tens(digit.Substring(0, 1) + "0") + " " + ones(digit.Substring(1));
}
break;
}
return name;
}
private String ones(String digit)
{
int digt = Convert.ToInt32(digit);
String name = "";
switch (digt)
{
case 1:
name = "One";
break;
case 2:
name = "Two";
break;
case 3:
name = "Three";
break;
case 4:
name = "Four";
break;
case 5:
name = "Five";
break;
case 6:
name = "Six";
break;
case 7:
name = "Seven";
break;
case 8:
name = "Eight";
break;
case 9:
name = "Nine";
break;
}
return name;
}
private String translateRupees(String Rupees)
{
String cts = "", digit = "", engOne = "";
for (int i = 0; i < Rupees.Length; i++)
{
digit = Rupees[i].ToString();
if (digit.Equals("0"))
{
engOne = "Zero";
}
else
{
engOne = ones(digit);
}
cts += " " + engOne;
}
return cts;
}
}
}
Form contains two Textboxes (textBox1 and textBox2) and a Button(button1).
I want to type an amount in numbers in textBox1 and click on the button. The amount entered in numbers in textBox1 has to be converted to text and appear in textbox2. Functions to convert are in above C# class file. I am a new student. Can anyone help me in solving this problem.

You have to create an object for 'NumberToEnglish' Class and use it in Form1.cs this way
public partial class Form1 : Form
{
NumberToEnglish neObj = new NumberToEnglish();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = neObj.changeCurrencyToWords(Convert.ToDouble(textBox1.Text));
}
}

public partial class Form1 : Form
{
NumberToEnglish Obj = new NumberToEnglish();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = Obj.changeCurrencyToWords(textBox1.Text);//As your method accept a string..
}
}

Related

OpenQA.Selenium.ElementNotInteractableException : Cannot click on element

public PriceChangeRequestStatus SelectPcrFrom(String[] fromdate)
{
wrapper.Click(imgPcrFrom);
String year, day, month;
day = fromdate[0];
month = fromdate[1];
year = fromdate[2];
//for (int index = 0; index <= 1; index++)
wrapper.Click(lnkPcrFrom);
wrapper.Click(lnkPcrFrom);
Thread.Sleep(4000);
driver.FindElement(By.XPath("//div[contains(text(),'" + year + "')]")).Click();
Thread.Sleep(4000);
driver.FindElement(By.XPath("//div[contains(text(),'" + findMonth(month).Substring(0, 3) + "')]")).Click();
Thread.Sleep(2000);
driver.FindElement(By.XPath("//div[contains(#title,'" + findMonth(month) + " " + day + ", " + year + "')]")).Click();
Thread.Sleep(2000);
return this;
}
public PriceChangeRequestStatus SelectPcrTo(String[] todate)
{
wrapper.Click(imgPcrTo);
wrapper.Click(lnkPcrTo);
wrapper.Click(lnkPcrTo);
String year, day, month;
day = todate[0];
month = todate[1];
year = todate[2];
Thread.Sleep(4000);
// By Sunayana
//IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
//js.ExecuteScript("document.getElementsByClassName('ajax__calendar_year')");
// End
//// IReadOnlyList<IWebElement> yearClick = wrapper.GetElements("xpath:=.//div[#class='ajax__calendar_year']");
//IList<IWebElement> yearClick = driver.FindElements(By.XPath(".//div[#class='ajax__calendar_year']"));
//Console.WriteLine(yearClick.Count);
//for (int index = 1; index-1 <= yearClick.Count; index++)
//{
// int i=0;
// Console.WriteLine(index);
// i=index;
// if (yearClick[i].GetAttribute("innerHTML").Equals(year))
// {
// yearClick[i].Click();
// Console.WriteLine("I am here");
// }
// else {
// Console.WriteLine("Sorry");
// }
//}
Thread.Sleep(4000);
//var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
//try
//{
// wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[contains(text(),'" + year + "')]"))).Click();
//}
//catch (WebDriverTimeoutException)
//{
// Console.WriteLine("Logout button was not visible!");
//}
IWebElement clickyear = driver.FindElement(By.XPath("//div[contains(text(),'" +year+ "')]"));
clickyear.Click();
//Actions action = new Actions(driver);
//action.MoveToElement(clickyear).Click().Build().Perform();
Thread.Sleep(4000);
driver.FindElement(By.XPath("//div[contains(text(),'" + findMonth(month).Substring(0, 3) + "')]")).Click();
Thread.Sleep(2000);
driver.FindElement(By.XPath("//div[contains(#title,'" + findMonth(month) + " " + day + ", " + year + "')]")).Click();
Thread.Sleep(2000);
return this;
}
public String findMonth(String month)
{
String CorrectMonth = "";
switch (month) {
case "01":
CorrectMonth = "January";
break;
case "02":
CorrectMonth = "February";
break;
case "03":
CorrectMonth = "March";
break;
case "04":
CorrectMonth = "April";
break;
case "05":
CorrectMonth = "May";
break;
case "06":
CorrectMonth = "June";
break;
case "07":
CorrectMonth = "July";
break;
case "08":
CorrectMonth = "August";
break;
case "09":
CorrectMonth = "September";
break;
case "10":
CorrectMonth = "October";
break;
case "11":
CorrectMonth = "November";
break;
case "12":
CorrectMonth = "December";
break;
}
return CorrectMonth;
}
Edited the code and tried running the script. The logic written for SELECTPCRFROM is working fine but the same logic is not working for SELECTPCRTO. tried many others solutions but still SELECTPCRTO is nor working as expected Please help me on this. The executed code is displaying error message provided in the title
Most of the datepickers gets identified by xpath but when you try perform an operation on them then they would give you an error. Instead try going the javascript way.
Java -
document.evaluate(PATH, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
Similary you can use it in C#
Refer the answer here - link

Display Numbers to words in textbox or lable in asp.net,C#?

<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox><br />
<br />
<asp:TextBox ID="TextBox2" runat="server" Height="100px" TextMode="MultiLine" Width="600px"></asp:TextBox>
</div>
</form>
And my code behind code is:
public String changeToWords(String numb)
{
String val = "", wholeNo = numb, points = "", andStr = "", pointStr = "";
String endStr = ("");
try
{
int decimalPlace = numb.IndexOf(".");
if (decimalPlace > 0)
{
wholeNo = numb.Substring(0, decimalPlace);
points = numb.Substring(decimalPlace + 1);
if (Convert.ToInt32(points) > 0)
{
andStr = ("point");// just to separate whole numbers from points/Rupees
}
}
val = String.Format("{0} {1}{2} {3}", translateWholeNumber(wholeNo).Trim(), andStr, pointStr, endStr);
}
catch
{
;
}
return val;
}
private String translateWholeNumber(String number)
{
string word = "";
try
{
bool beginsZero = false;//tests for 0XX
bool isDone = false;//test if already translated
double dblAmt = (Convert.ToDouble(number));
//if ((dblAmt > 0) && number.StartsWith("0"))
if (dblAmt > 0)
{//test for zero or digit zero in a nuemric
beginsZero = number.StartsWith("0");
int numDigits = number.Length;
int pos = 0;//store digit grouping
String place = "";//digit grouping name:hundres,thousand,etc...
switch (numDigits)
{
case 1://ones' range
word = ones(number);
isDone = true;
break;
case 2://tens' range
word = tens(number);
isDone = true;
break;
case 3://hundreds' range
pos = (numDigits % 3) + 1;
place = " Hundred ";
break;
case 4://thousands' range
case 5:
case 6:
pos = (numDigits % 4) + 1;
place = " Thousand ";
break;
case 7://millions' range
case 8:
case 9:
pos = (numDigits % 7) + 1;
place = " Million ";
break;
case 10://Billions's range
pos = (numDigits % 10) + 1;
place = " Billion ";
break;
//add extra case options for anything above Billion...
default:
isDone = true;
break;
}
if (!isDone)
{//if transalation is not done, continue...(Recursion comes in now!!)
word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos));
//check for trailing zeros
if (beginsZero) word = " and " + word.Trim();
}
//ignore digit grouping names
if (word.Trim().Equals(place.Trim())) word = "";
}
}
catch
{
;
}
return word.Trim();
}
private String tens(String digit)
{
int digt = Convert.ToInt32(digit);
String name = null;
switch (digt)
{
case 10:
name = "Ten";
break;
case 11:
name = "Eleven";
break;
case 12:
name = "Twelve";
break;
case 13:
name = "Thirteen";
break;
case 14:
name = "Fourteen";
break;
case 15:
name = "Fifteen";
break;
case 16:
name = "Sixteen";
break;
case 17:
name = "Seventeen";
break;
case 18:
name = "Eighteen";
break;
case 19:
name = "Nineteen";
break;
case 20:
name = "Twenty";
break;
case 30:
name = "Thirty";
break;
case 40:
name = "Fourty";
break;
case 50:
name = "Fifty";
break;
case 60:
name = "Sixty";
break;
case 70:
name = "Seventy";
break;
case 80:
name = "Eighty";
break;
case 90:
name = "Ninety";
break;
default:
if (digt > 0)
{
name = tens(digit.Substring(0, 1) + "0") + " " + ones(digit.Substring(1));
}
break;
}
return name;
}
private String ones(String digit)
{
int digt = Convert.ToInt32(digit);
String name = "";
switch (digt)
{
case 1:
name = "One";
break;
case 2:
name = "Two";
break;
case 3:
name = "Three";
break;
case 4:
name = "Four";
break;
case 5:
name = "Five";
break;
case 6:
name = "Six";
break;
case 7:
name = "Seven";
break;
case 8:
name = "Eight";
break;
case 9:
name = "Nine";
break;
}
return name;
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox2.Text = changeToWords(TextBox1.Text);
}
it is working properly but what i want is when i am typeing in textbox it automatically shows in another textbox and I want to show numbers to words in lakhs also.... please help me

Generate Sequential Passwords in C#

I have been trying to generate passwords sequentially in C# (aaaa, aaab, aaac, ... abcd, abce) for a hash cracker (for white-hat purposes). However, I'm not sure how to do that.
Right now I have a char array, the last element of which is being incremented by a switch:
switch (character) {
case ('0'):
character = '1';
break;
case ('1'):
character = '2';
break;
case ('2'):
character = '3';
break;
case ('3'):
character = '4';
break;
case ('4'):
character = '5';
break;
case ('5'):
character = '6';
break;
case ('6'):
character = '7';
break;
case ('7'):
character = '8';
break;
case ('8'):
character = '9';
break;
case ('9'):
character = 'a';
break;
case ('a'):
character = 'b';
break;
case ('b'):
character = 'c';
break;
case ('c'):
character = 'd';
break;
case ('d'):
character = 'e';
break;
case ('e'):
character = 'f';
break;
case ('f'):
character = 'g';
break;
case ('g'):
character = 'h';
break;
case ('h'):
character = 'i';
break;
case ('i'):
character = 'j';
break;
case ('j'):
character = 'k';
break;
case ('k'):
character = 'l';
break;
case ('l'):
character = 'm';
break;
case ('m'):
character = 'n';
break;
case ('n'):
character = 'o';
break;
case ('o'):
character = 'p';
break;
case ('p'):
character = 'q';
break;
case ('q'):
character = 'r';
break;
case ('r'):
character = 's';
break;
case ('s'):
character = 't';
break;
case ('t'):
character = 'u';
break;
case ('u'):
character = 'v';
break;
case ('v'):
character = 'w';
break;
case ('w'):
character = 'x';
break;
case ('x'):
character = 'y';
break;
case ('y'):
character = 'z';
break;
case ('z'):
character = 'A';
break;
case ('A'):
character = 'B';
break;
case ('B'):
character = 'C';
break;
case ('C'):
character = 'D';
break;
case ('D'):
character = 'E';
break;
case ('E'):
character = 'F';
break;
case ('F'):
character = 'G';
break;
case ('G'):
character = 'H';
break;
case ('H'):
character = 'I';
break;
case ('I'):
character = 'J';
break;
case ('J'):
character = 'K';
break;
case ('K'):
character = 'L';
break;
case ('L'):
character = 'M';
break;
case ('M'):
character = 'N';
break;
case ('N'):
character = 'O';
break;
case ('O'):
character = 'P';
break;
case ('P'):
character = 'Q';
break;
case ('Q'):
character = 'R';
break;
case ('R'):
character = 'S';
break;
case ('S'):
character = 'T';
break;
case ('T'):
character = 'U';
break;
case ('U'):
character = 'V';
break;
case ('V'):
character = 'W';
break;
case ('W'):
character = 'X';
break;
case ('X'):
character = 'Y';
break;
case ('Y'):
character = 'Z';
break;
case ('Z'):
character = '#';
break;
case ('#'):
character = '%';
break;
case ('%'):
character = '/';
break;
case ('/'):
character = '\\';
break;
case ('\\'):
character = '\'';
break;
case ('\''):
character = '!';
break;
case ('!'):
character = '$';
break;
case ('$'):
character = '#';
break;
case ('#'):
character = '^';
break;
case ('^'):
character = '?';
break;
case ('?'):
character = ':';
break;
case (':'):
character = ',';
break;
case (','):
character = '(';
break;
case ('('):
character = ')';
break;
case (')'):
character = '[';
break;
case ('['):
character = ']';
break;
case (']'):
character = '{';
break;
case ('{'):
character = '}';
break;
case ('}'):
character = '-';
break;
case ('-'):
character = '_';
break;
case ('_'):
character = '+';
break;
case ('+'):
character = '=';
break;
case ('='):
character = '0';
break;
prev = true;
default:
character = '0';
break;
}
However, when that element reaches 0 again, I need to increment the previous digit of the password and if that digit also reaches 0, I need to increment the previous digit from that one, and so on.
Also, since this is a hash cracker, it needs to be fast. Any suggestions?
My first answer wouldn't give you quite what you want. This will:
Create a string that defines your alphabet:
const string Alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
Then initialize an int array with all -1, as long as your longest password. This one will do up to 8 characters:
int[] pwArray = new int[] {-1, -1, -1, -1, -1, -1, -1, -1};
Then run a loop:
for (int i = 0; i < Whatever; ++i)
{
string password = ToPasswordString(pwArray, Alphabet);
// do something with the password
}
string ToPasswordString(int[] pass, string alphabet)
{
for (int i = pass.Length-1; i > 0; --i)
{
pass[i]++;
if (pass[i] < alphabet.Length)
{
break;
}
pass[i] = 0;
}
var sb = new StringBuilder();
for (int i = 0; i < pass.Length; ++i)
{
if (pass[i] >= 0)
sb.Append(alphabet[pass[i]]);
}
return sb.ToString();
}
The key here is that we don't modify password characters. Rather, we modify indexes into the character array.
Granted, this won't be as fast as some other methods (passwords per second), but your program's speed is limited by how fast the site you're hacking can respond to login tries. The amount of time you spend generating passwords is irrelevant when compared to that.
Instead of a complicated and extremely large switch statement why not just directly map int values to char.
for (int i = 0; i < (int)Char.MaxValue; i++) {
char c = (char)i;
...
}
This would allow you to build up brute force password generator pretty easily.
You could use ascii code, for sample:
public char GetNextChar(char c)
{
return (char)(((int)c) + 1);
}
Would be a nice extension, for sample:
public static class Extensions
{
public static char GetNextChar(this char c)
{
return (char)(((int)c) + 1);
}
}
and using it:
character = character.GetNextChar();
Something like this should work.
char[] currPassword = new char[] { (char)65, (char)65, (char)65, (char)65, (char)65 }; //65 = A
const int Z = 90;
const int A = 65;
void Main()
{
while(true)
{
string curr = new string(currPassword);
Next();
}
}
public void Next()
{
int currIndex = currPassword.Length -1;
bool done = false;
do
{
currPassword[currIndex]++;
if(currPassword[currIndex] > Z && currIndex >= 0)
{
currPassword[currIndex] = (char)A;
currIndex--;
}
else
{
done = true;
}
}while(!done);
}
LINQLIB contains methods to compute all permutations or combinations. You could use it instead although the order might not be exactly as you describe in your question.
Another different approach:
static void Main(string[] args)
{
string charSet = "0123456789!##$%^&*";
string password = "20#";
StringBuilder start = new StringBuilder("0");
int j = 0;
int z = 0;
while (start.ToString() != password)
{
start[z] = charSet[j++];
Console.WriteLine(start);
if (j == charSet.Length)
{
if (start.ToString().Where(c => c == charSet[charSet.Length - 1]).Count() == start.ToString().Length)
{
start.Append("0");
for (int t = 0; t < start.Length; t++)
{
start[t] = '0';
}
z++;
}
else
{
for (int t = start.Length - 2; t >= 0; t--)
{
if (charSet.IndexOf(start[t]) == charSet.Length - 1)
{
start[t] = '0';
}
else
{
start[t] = charSet[charSet.IndexOf(start[t]) + 1];
break;
}
}
}
j = 0;
}
}
}
Just treat each of your password entries as numbers, its base being the number of characters present in the initial Dictionary. Whenever you reach a maximum in a given position, you 'carry over' to the next 'digit'.
This small class will return all possible combinations, given:
an initial character dictionary
a minimum password size
a maximum password size
Just keep in mind that sequences like that get EXTREMELY large very fast, depending on the initial dictionary size. For example, the following code with default values (minChar=2,MaxChar=4) generates 866495 entries.
public class Sequencer
{
public string characterDictionary = "0123456789";
//public string characterDictionary = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=[];',./`~!##$%^&*()_+{}|:\"<>?|\\";
public int minCharCount = 2;
public int maxCharCount = 4;
private List<string> _sequence;
public List<string> GetSequence()
{
_sequence = new List<string>();
for (int i = minCharCount; i < (maxCharCount + 1); i++)
RenderCombinations(i);
return _sequence;
}
private void RenderCombinations(int charCount)
{
int _dictSize = characterDictionary.Length;
int[] _containerMatrix = new int[charCount];
char[] _splitDict = characterDictionary.ToCharArray();
bool _maxReached = false;
do
{
string _currentCombination = "";
for (int i = 0; i < charCount; i++)
_currentCombination += _splitDict[_containerMatrix[i]];
_sequence.Add(_currentCombination);
// Let the shifting begin!
bool _mustCarry = false;
int _carryIndex = 0;
do
{
_mustCarry = false;
if (_carryIndex == _containerMatrix.Length)
{
_maxReached = true;
break;
}
_containerMatrix[_carryIndex]++;
if (_containerMatrix[_carryIndex] == _dictSize)
{
_mustCarry = true;
_containerMatrix[_carryIndex] = 0;
}
_carryIndex++;
if (_carryIndex > charCount)
{
_mustCarry = false;
_maxReached = true;
}
} while (_mustCarry);
} while (!_maxReached);
}
}

Convert a complicated txt file into a csv file using c#

I have absolutely no idea what to do with this but here is a snippet of the file that I'm trying to convert:
"September
3Beef
Lamb Chops
4Fish
Not Fish
5Mac and Cheese
PB & J"
The csv file is supposed to print the date what comes after in quotes, so the above should look like:
Tuesday September Third 2013 "Beef" "Lamb Chops"
Wednesday September Fourth 2013 "Fish" "Not Fish"
Thursday September Fifth 2013 "Mac and Cheese" "PB&J"
Here is what I have so far:
StreamReader reader = new StreamReader(#"..\..\Lunches.txt");
while (!reader.EndOfStream)
{
string currentLine = reader.ReadLine();
}
StreamWriter writer = new StreamWriter(#"..\..\Lunches.csv");
// date.ToString("ddddd yyyyy mm MMMMMM");
string delimiter = ",";
Here is the code
private void Form1_Load(object sender, EventArgs e)
{
string sayka = "August\n\n" +
"31Beef\n" +
"Lamb Chops\n" +
"24Fish\n" +
"Not Fish\n" +
"15Mac and Cheese\n" +
"PB & J\n";
MessageBox.Show(makeCSV(sayka));
}
string getMonthName(int val)
{
switch (val)
{
case 1: return "JANUARY";
case 2: return "FEBRUARY";
case 3: return "MARCH";
case 4: return "APRIL";
case 5: return "MAY";
case 6: return "JUNE";
case 7: return "JULY";
case 8: return "AUGUST";
case 9: return "SEPTEMBER";
case 10: return "OCTOBER";
case 11: return "NOVEMBER";
case 12: return "DECEMBER";
default: return null;
}
}
string getDayName(int val)
{
switch (val)
{
case 1: return "First";
case 2: return "Second";
case 3: return "Third";
case 4: return "Fourth";
case 5: return "Fifth";
case 6: return "Sixth";
case 7: return "Seventh";
case 8: return "Eighth";
case 9: return "Nineth";
case 10: return "Tenth";
case 11: return "Eleventh";
case 12: return "Twelth";
case 13: return "Thirteenth";
case 14: return "Fouteenth";
case 15: return "Fifteenth";
case 16: return "Sixteenth";
case 17: return "Seventeenth";
case 18: return "Eighteenth";
case 19: return "Nineteenth";
case 20: return "Twentieth";
default: return "";
}
}
string getDayName2(int val)
{
if (val == 30) return "Thirtieth";
else if (val > 30) return "Thirty " + getDayName(val % 30);
else if (val > 20) return "Twenty " + getDayName(val % 20);
else return getDayName(val);
}
string makeCSV(string val)
{
string res = "";
string[] ss = val.Split('\n');
int curMonth = 0;
for (int i = 0; i < ss.Length; i++)
{
if (ss[i].Trim() != "")
{
bool isInt = false;
try
{
int intA = Convert.ToInt32(ss[i][0].ToString());
isInt = true;
}
catch { }
if (isInt)
{
bool isDoubleInt = false;
try
{
int intB = Convert.ToInt32(ss[i][1].ToString());
isDoubleInt = true;
}
catch { }
int date = 0;
if (isDoubleInt) date = Convert.ToInt32(ss[i].Remove(2));
else date = Convert.ToInt32(ss[i][0].ToString());
DateTime dt = new DateTime(DateTime.Now.Year, curMonth, date);
string itemName = "";
if (isDoubleInt) itemName = ss[i].Substring(2);
else itemName = ss[i].Substring(1);
string itemName2 = ss[i + 1];
res += dt.DayOfWeek + " " + getMonthName(dt.Month) + " " + getDayName2(dt.Day) + " \"" + itemName + "\"" + " \"" + itemName2 + "\"\n";
}
else
{
for (int j = 1; j < 13; j++)
if (ss[i].ToUpper().StartsWith(getMonthName(j)))
{
curMonth = j;
break;
}
}
}
}
return res;
}
}
From the filestream either use StreamReader.readToEnd(), get the string and use the function, Or if the file is big then use it line by line..
Rate if this helps..
This is going to be a bit complicated. I left out some things for you to do.
String[] months = { "January", "February", "March", ....};
Date processDate = new Date();
while (!reader.EndOfStream)
{
string currentLine = reader.ReadLine();
// skip this line if blank
if (String.IsNullOrEmpty(currentLine)) continue;
if (months.Contains(currentLine)) {
// we have a new starting month.
// reset the process date
Int32 month = DateTime.ParseExact(currentLine.Trim(), "MMMM", CultureInfo.CurrentCulture).Month;
date = Convert.ToDate(month.ToString() + "/01/2013");
continue;
}
// here's where the real fun begins:
// you have to pull out the first two characters and test if one or both are digits.
// This will give you the day. Put that into your date variable.
Int32 day = 0;
char[] arr = currentLine.ToCharArray(0, currentLine.Length);
if (Char.IsDigit(arr[1])) {
// first two characters are numbers
day = Convert.ToInt32(currentLine.Substring(0,2));
currentLine = currentLine.Remove(0,2);
} else {
// only the first character is a number
day = Convert.ToInt32(currentLine.Substring(0,1));
currentLine = currentLine.Remove(0,1);
}
// set the new date
date = new DateTime(date.Year, date.Month, day, 0, 0, 0);
// If we can assume that ALL lines are broken into two parts then we can do the following:
String secondLine = reader.ReadLine();
currentLine = String.Format("{0} {1}", currentLine, secondLine);
// At this point you have the month, day, and the entire line.
// write it to your lunch stream or store in a StringBuilder
}

Displaying random images at runtime

I am trying to display random images
heres my code
private void Page_Load(object sender, EventArgs e)
{
int num1=0;
Random randNum = new Random();
num1 = randNum.Next(0, 9);
Image1.ImageUrl = DisplayNumber(num1);
Image1.Visible=true;
}
protected string DisplayNumber(int i)
{
string imagepath="";
switch (i)
{
case 0:
imagepath = "~/fordoctor/doctor_login/images/0.GIF";
break;
case 1:
imagepath = "~/fordoctor/doctor_login/images/1.GIF";
break;
case 2:
imagepath = "~/fordoctor/doctor_login/images/2.GIF";
break;
case 3:
imagepath = "~/fordoctor/doctor_login/images/3.GIF";
break;
case 4:
imagepath = "~/fordoctor/doctor_login/images/4.GIF";
break;
case 5:
imagepath = "~/fordoctor/doctor_login/images/5.GIF";
break;
case 6:
imagepath = "~/fordoctor/doctor_login/images/6.GIF";
break;
case 7:
imagepath = "~/fordoctor/doctor_login/images/7.GIF";
break;
case 8:
imagepath = "~/fordoctor/doctor_login/images/8.GIF";
break;
case 9:
imagepath = "~/fordoctor/doctor_login/images/9.GIF";
break;
}
Session["num1"] = imagepath;
return imagepath;
}
but it displays nothing
i have even checked the images using Response.Write(Session["num1"].ToString());
and the images get displayed at the next page
I suggest making your code similar to this:
private void Page_Load( object sender, EventArgs e ) {
string imgUrl = GetRandomImageUrl();
Session["num1"] = imgUrl;
Image1.ImageUrl = imgUrl;
Image1.Visible = true;
}
protected string GetRandomImageUrl() {
Random r = new Random();
return String.Format( "~/fordoctor/doctor_login/images/{0}.gif", r.Next( 0, 9 ) );
}
Check the case of your GIF, should be lowercase (usually).

Categories