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).
Related
Making a program that converts Alphanumeric phone numbers to a regular phone number (ex.123-TRAVELS = 123-872-8357), but trying to figure some (probably simple to fix) errors to display the results under the CheckButton_Click variable. I'm giving myself a headache right now trying to figure this out. These are the current errors for the pasted code:
public static string PhoneToNumber(string str)
{
// Array version of value
var phoneNumberArr = str.ToCharArray();
for (int i = 0; i < str.Length; i++)
{
phoneNumberArr[i] = PhoneCharToNumber(phoneNumberArr[i]);
}
return new string(phoneNumberArr);
}
public static char PhoneCharToNumber(char value)
{
switch (value)
{
case 'A':
case 'B':
case 'C':
return '2';
case 'D':
case 'E':
case 'F':
return '3';
case 'G':
case 'H':
case 'I':
return '4';
case 'J':
case 'K':
case 'L':
return '5';
case 'M':
case 'N':
case 'O':
return '6';
case 'P':
case 'Q':
case 'R':
return '7';
case 'S':
case 'T':
case 'U':
return '8';
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
return '9';
default: return value;
}
}
private void CheckButton_Click(object sender, EventArgs e)
{
string str = inputTextBox.Text;
char value = PhoneCharToNumber(value);
resultsLabel.Text = value;
}
private void ExitButton_Click(object sender, EventArgs e)
{
// Closes the form
this.Close();
}
}
The error is here: char value = PhoneCharToNumber(value);
where value argument for PhoneCharToNumber hasn't been declared nor assigned.
I think you wanted to use string value = PhoneToNumber(str); instead.
Infact your user enters number inside inputTextBox; you have to convert that text to a valid number and then place the result to resultsLabel.
private void CheckButton_Click(object sender, EventArgs e)
{
string str = inputTextBox.Text;
string value = PhoneToNumber(str);
resultsLabel.Text = value;
}
In the CheckButton_Click method, you pass value as parameter. Value is undefined at that moment, you should pass str (which is a bad name) like this. Beside that PhoneCharToNumber returns a string not a char:
private void CheckButton_Click(object sender, EventArgs e)
{
string textToProcess = inputTextBox.Text;
string processedText = PhoneToNumber(textToCheck );
resultsLabel.Text = processedText ;
}
I'm creating n 8th ball and I'm trying to randomly generate one of the eight phrases into a textbox once a button is tapped and can't get my head around how to get my phrases in a textbox when a button is tapped.
private void Button1_Tapped(object sender, TappedRoutedEventArgs e)
{
Random num = new Random();
int a = num.Next(9);
switch (a)
{
case 0:
Console.;
break;
case 1:
Console.WriteLine.TextBox("Yes");
break;
case 2:
Console.WriteLine.TextBox("No");
break;
case 3:
Console.WriteLine.TextBox("Maybe");
break;
case 4:
Console.WriteLine.TextBox("You could say that");
break;
case 5:
Console.WriteLine.TextBox("Most certain");
break;
case 6:
Console.WriteLine.TextBox("Dont even try");
break;
case 7:
Console.WriteLine.TextBox("Full steam ahead");
break;
}
}
Console.WriteLine() normally writes to the Console output in a Console App. So it seems you are mixing something up...
Your textbox in the wpf app needs to have a variable name e.g. theTextBox and then you assign the string to the .Text property.
private void Button1_Tapped(object sender, TappedRoutedEventArgs e)
{
Random num = new Random();
int a = num.Next(9);
switch (a)
{
case 0:
theTextBox.Text = "";
break;
case 1:
theTextBox.Text = "Yes";
break;
case 2:
theTextBox.Text = "No";
break;
case 3:
theTextBox.Text = "Maybe";
break;
case 4:
theTextBox.Text = "You could say that";
break;
case 5:
theTextBox.Text = "Most certain";
break;
case 6:
theTextBox.Text = "Dont even try";
break;
case 7:
theTextBox.Text = "Full steam ahead";
break;
}
}
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..
}
}
<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
i need to validate textbox1 as 2 alphabets and 2integer only i.e) ab11
how i can set it my text box only accept 2 chars and 2 integers.
Please help me...
if:
string myString = textbox1.Text;
Then to validate:
If (Regex.IsMatch(myString, "^[A-Za-z]{2}[0-9]{2}$")))
{
return true;
}
else
{
return false;
}
private void textBox2_Validating(object sender, CancelEventArgs e)
{
var cn = textBox2.Text.Where(c => char.IsLetter(c)).Count();
var cd = textBox2.Text.Where(c => char.IsNumber(c)).Count();
if (cn >= 2 && cd >= 2)
{
//Success, Do Stuff
}
else
{
e.Cancel = true;
}
}
This should work.
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox tb=sender as TextBox;
string text=tb.Text;
switch (text.Length)
{
case 1:
if (!char.IsLetter(text[0]))
tb.Text = "";
break;
case 2:
if (!char.IsLetter(text[1]))
tb.Text = text.Remove(1);
break;
case 3:
if (!char.IsNumber(text[2]))
tb.Text = text.Remove(2);
break;
case 4:
if (!char.IsNumber(text[3]))
tb.Text = text.Remove(3);
break;
default:
if(text.Length>4)
tb.Text = text.Substring(0, 4);
break;
}
textBox1.Select(tb.Text.Length, 0);
}
string str = textBox1.Text;
if (Regex.IsMatch(str, #"^(([A-Z]|[a-z])([A-Z]|[a-z])\d\d)$"))
{
MessageBox.Show("Valid");
}
Reference: Regex Class