TextBox that remembers it's value [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a problem. I am making an app on Windows 8 which has a textbox. I want the textbox to remember it's value when I close the application.

There are many possible solutions on this,
One is by saving the value in the database or
by saving it on Settings.

You have several options:
Properties.Settings (and on form closing use Save)
Windows registry
Use storage file
But you'll have to do a little work by yourself.

For possible solutions, see other answers. Here I give you pseudo-code for storage file.
Create a callback that is called whenever application launches and one for then it closes.
When it launches, load your file, get all of it's text including \n in one string, and set that as text.
When it closes, get the text in the box including \n and write it to the file.
It is quite simple. I would prefer the storage file method here because:
Properties.Settings is slightly overkill, though it's a viable alternative.
Windows Registry will work but it won't be cross platform (you may not care about that but it's always a good idea to make sure your app can be ported, in case it's successful)

You can use the approach to save a value to a File.
Here is a very ugly exa,ple to get you started:
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Double val;
val = LoadDoubleValue();
Console.WriteLine("Previous Value was: " + val);
Console.Write("Please enter Value for next startup: ");
string input = Console.ReadLine();
if (Double.TryParse(input, out val))
{
SaveDoubleValue(val);
}
}
static Double LoadDoubleValue()
{
Double ret = 0;
if(File.Exists("save.dat")){
if (!Double.TryParse(File.ReadAllLines("save.dat")[0], out ret))
{
ret = 15;
}
}else{
ret = 15;
}
return ret;
}
static void SaveDoubleValue(Double val)
{
File.WriteAllLines("save.dat", new string[] { val.ToString() });
}
}
}

Related

When not entering any value, my program crashes [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a problem in my windows form in c#. The program is simple;
there are 3 textboxes and then it sums their values. however, when i click on the sum button without entering any values in the other 3 textboxes, the program crashes.
How can i make these textboxes accept only positive numbers and zeros?
this is what i did
private void button1_Click(object sender, EventArgs e)
{
double FirstNumb = Convert.ToDouble(txtFirstValue.Text);
double SecondNumb = Convert.ToDouble(txtSecondValue.Text);
double ThirdNumb = Convert.ToDouble(txtThirdValue.Text);
double m;
m = FirstNumb + SecondNumb + ThirdNumb;
listBox1.Items.Add(m);
}
Try to resolve using int.TryParse. This handles string as well.
Demo Reference
You are likely trying to convert the textboxes textual content to int. Unfortunately, you cannot convert an empty string to a number. Try setting the textboxes default content to "0".
You'll want to check that the string value from the text box is not null or empty, or otherwise invalid. Change this line:
double FirstNumb = Convert.ToDouble(txtFirstValue.Text);
to this
double FirstNumb = 0;
double.TryParse(txtFirstValue.Text, out FirstNumb);
FirstNumb will remain 0 if the parsing fails. Note that TryParse returns a bool, true if the parsing was successful and false otherwise. You can also take action on that as well, perhaps showing a MessageBox asking the user to fill a value and exiting the summation method.
See the documentation for more information: http://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.100).aspx
EDIT: If you want to enforce only positive values, after the try parse you'll have to check that FirstNumb >= 0 and use a MessageBox to alert the user why, and abort the summation method.

simple function to look if a integer in c# is a prime? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
is there a function or a simple class which let me check if a integer in c# is a prime or not?
No.
You'll need to write your own.
The basic algorithm for one single integer is to divide it by all integers up until it's square root and ensure that there is always a remainder.
To check more than one number, optimized algorithms such as the Sieve of Eratosthenes are available.
There is no reason to have a standard prime checking function.
This is an expensive computation which can be made in different ways, each one having different impacts on memory and cpu.
And it really depends on the size of the numbers and the sequence of number whose primality you'll have to test, as there are usually intermediate values to store.
Said otherwise : the solution you'll chose will have to be adapted to your program and your exact need. I suggest you start by looking at wikipedia (credit to Chris Shain for the link) and by existing libraries like this one for small integers (credits to Google for the link).
Here's the one I use for all my project euler problems.
private static bool IsPrime(long number) {
if (number <= 1) return false;
for (long i = 2; i <= Math.Sqrt(number); i++) {
if (number % i == 0)
return false;
}
return true;
}

MasterMind in C# Console Application [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an assignment from my school in which we have to make a MasterMind game in C# in a console application.
So far I managed to do the border (with help from friends), an introductory tune (beep) when the game starts, and the user input to insert the numbers.
The problem is when the user ends the game, the game doesn't stop to accept inputs from the user and obviously crashes.
I also have an error in the high score method "use of unassigned local variable".
score = ptsguesses * ptsTime;
Where are ptsguesses and ptsTime initialized? Nowhere, obviously.
You probably want to set ptsguesses and ptsTime before calculating the score.
the use of unassigned value is probably this one:
static void highscore()
{
{
byte ptsguesses,ptsTime, userGuesses, timeTaken;
int score; <<------
change it to int score = 0;
also, ptsguesses,ptstime,userguesses and timetaken have never been initialised.
you might want to try to pass those arguments to your highscore() method.
something like
static void highscore(byte ptsguesses, byte ptsTime, byte userGuesses, byte timeTaken)
you'd have to call the highscore() method then, and pass the actual values to the method. that way if u try to run highscore(), you will have something to actually calculate.

Error on maximum number of Files [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
is there any way to throw new exception on maxmium number of files in a directory ?
No, there is none:
you can catch it with a more generic
System.IO.IOException
And read the (underlying) message you could throw a self written Exception from that point.
UPDATE
I just learned . that you use the
bool isfull = info.GetFiles().LongLength == 4.294.967.295
property. Unfortunately it will eat all your memory.
Therefore using
DirectoryInfo.EnumerateFiles().Count()
perhaps in a chunked manner could be a better approach
fyi:
65,534 for FAT32
4,294,967,295 FOR NTFS
(source)
I don't know specifically about C#, but I know Java doesn't have a specific exception or error to throw for a full drive. I suggest simply making your own exception class and using that. I suggest calling it a FullDirectoryError or DirectoryOverflowError, assuming it's serious enough to be called an error.
Something like this?
public static void checkFileNumber(string directoryToCheck, int maxNumber )
{
if ( Directory.Exists( directoryToCheck ) )
{
if ( Directory.GetFiles( directoryToCheck ).Length > maxNumber )
{
throw new Exception("Too many files in " + directoryToCheck);
}
}
}
Be sure to use System.IO; :-)
Throwing exceptions is an expensive action; when you can avoid it, please do so. Try to program defensively if it is possible. But when throwing one, you have to supply a matching catch on a specific exception.
Like Casper Kleijne said, you could catch on IOException but why in the first place would you want to catch anyway ? What is the compensating action in the catch ?
Please supply some more info what you want to achieve with it.

How to create serial number generator? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I program a Serial Number Generator that generates serial numbers for some existing software?
You don't state any specific requirements but you can use a GUID.
Guid mySerialNumber = Guid.NewGuid();
I have used this CodeProject Article before when I've needed to do the same thing for projects I have worked on recently. I found it very detailed and while I didn't use everything in the sample I did get a lot out of it. (I have no affiliation with CodeProject or the author)
Additionally there is a library which gives you all this functionality complete with license file replacement and heaps of other features, but its not free, and unfortunately I couldn't remember the link. I'm sure a bit of Googgling and you'll find it.
public static string GetSerialKeyAlphaNumaric(SNKeyLength keyLength)
{
string newSerialNumber = "";
string SerialNumber=Guid.NewGuid().ToString("N").Substring(0, (int)keyLength).ToUpper();
for(int iCount=0; iCount < (int) keyLength ;iCount +=4)
newSerialNumber = newSerialNumber + SerialNumber.Substring(iCount, 4) + "-";
newSerialNumber = newSerialNumber.Substring(0, newSerialNumber.Length - 1);
return newSerialNumber;
}
Try a number you can test using Luhn's algorithm. That way, you can make it long an inscrutable, yet still easily confirmed. The article contains a link to a C# implementation.

Categories

Resources