|
wasimsharp wrote: if (Char.IsLetter(e.KeyChar))
{
e.Handled;
}
1. That wont compile.
2. Are you checking for digits or letters?
|
|
|
|
|
No
if (!Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
wasim khan
|
|
|
|
|
int.TryParse in the key event handler is your friend.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Greeeg wrote: int.TryParse in the key event handler is your friend.
Friends dont let friends overflow! What happens if the person enters 25 digits?
|
|
|
|
|
Then I should take the time to read the question more precisely
A regex like \d{,7} for at most, or \d{7} for exactly 7 digits will do the job as well.
But I'd use a NumericUpDown control for this task.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
sacr83,
If you only want to allow numbers, why not use a NumericUpDown control...
MSDN[^]
Regards,
Gareth.
(FKA gareth111)
|
|
|
|
|
Just use this
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)<br />
{<br />
if (!char.IsDigit(e.KeyChar))<br />
{<br />
e.Handled = true;<br />
MessageBox.Show("Integer numbers only");<br />
} <br />
}
|
|
|
|
|
if you want to get an error as you type you can use KeyDown events then get your last entered character from your textbox, if last character is not what you suggest to input throw an errror
|
|
|
|
|
Hello!
I´ve made a customError-class inheriting from BaseValidator which I use to add error to Page.Validators from code-behind.
public class CustomError : BaseValidator
At first I inherited from IValidator , but I had to be able to set ValidationGroup and so I switched to BaseValidator . Problem is now the PropertiesValid-property of my class always throws an object reference-error. I´m giving ControlToValidate a correct value, so thats not whats causing this. Does anyone have an idea why this error occurs and how I prevent it?
The error looks like this:
[NullReferenceException: Objectreference has not been set to an instance of an object.]
[NullReferenceException: Objectreference has not been set to an instance of an object.]
System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName) +23
System.Web.UI.WebControls.BaseValidator.ControlPropertiesValid() +37
System.Web.UI.WebControls.BaseValidator.Validate() +58
|
|
|
|
|
|
hi freinds
now a days i am working to create the System updater.
can i use dll which is downloaded with update. and then my updater just
call the dll and it install the update. just like (DNP .NetPanel)
if yes then plez give me some help that how can i do this.
wasim khan
|
|
|
|
|
The way this usually works is a bootstrapper that runs, closes your app, replaces it with a new version and you're done.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
hi Christian Graus
i am creating System Configurator as i saw Dotnetpanel they have a configurator which download the Update and in the uodate they have setup.dll. which run when we install the update how i can do this.
as i remove the dll before installing the update its give error message while installing the update. i think that all the method that is used to install the new update is inside that dll can u tell me how it work
wasim khan
|
|
|
|
|
Hi! I have a question. I am building a APP and I am working with xml documents, I want to delete the file from the local disk after I will put the data I need in the DB, how should I do that?? Thanks ahead
|
|
|
|
|
File.Delete
Giorgi Dalakishvili
#region signature
my articles
#endregion
|
|
|
|
|
File.Delete(pathoftheFile)
wasim khan
|
|
|
|
|
this is the exception whan I am getting when I am debugging, and trying to remove the file:
The process cannot access the file 'C:\test.xml' because it is being used by another process.
|
|
|
|
|
it's ok, I fixed it, I had forgotten a XmlReader open, thx for the help and have a nice day
|
|
|
|
|
k thats good and one think remember always share your knowledege becoz some buddy like you and me need it...........................
wasim khan
|
|
|
|
|
It means that your application has not closed the stream after reading from the file.
Giorgi Dalakishvili
#region signature
my articles
#endregion
|
|
|
|
|
|
Hi everyone, I'm supposed to make a program that finds all the magic squares of size 6, but there are 36! possible combinations of squares to check, so my programs runs forever, perhaps with a million servers I could actually find a solution, so here is my question: Anyone knows a way of running my C# application in multiple computers at the same time?
Here is my code, perhaps anyone can improve it.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Threading;
namespace MagicSquare
{
public partial class Form1 : Form
{
Thread bgWorker = null;
public Form1()
{
InitializeComponent();
}
struct StateInfo
{
public int Size { get; set; }
public bool PanMagic { get; set; }
}
private void threadMethod(object state)
{
MagicSquare msq = new MagicSquare(((StateInfo)state).Size, ((StateInfo)state).PanMagic);
msq.FindEnd += new EventHandler(msq_FindEnd);
msq.FindStart += new EventHandler(msq_FindStart);
msq.MagicSquareFound += new MagicSquare.MagicSquareEventHandler(msq_MagicSquareFound);
msq.FindMagicSquares();
}
void msq_MagicSquareFound(object sender, MagicSquare.MagicSquareEventArgs e)
{
StringBuilder temp = new StringBuilder(textBox1.Text);
temp.Append("---START---\r\n");
for (int i = 0; i < e.Matrix.GetLength(0); i++)
{
for (int j = 0; j < e.Matrix.GetLength(1); j++)
temp.Append(string.Format("{0}{1}", e.Matrix[j, i], j + 1 == e.Matrix.GetLength(0) ? string.Empty : ", "));
temp.Append("\r\n");
}
temp.Append("---END---\r\n");
textBox1.Invoke(new ParameterizedThreadStart(delegate(object state)
{
textBox1.Text = (string)state;
}), temp.ToString());
}
void msq_FindStart(object sender, EventArgs e)
{
working = true;
textBox1.Invoke(new ParameterizedThreadStart(delegate(object state)
{
textBox1.Text = (string)state;
}), string.Empty);
}
void msq_FindEnd(object sender, EventArgs e)
{
working = false;
button1.Invoke(new ParameterizedThreadStart(delegate(object state)
{
button1.Text = (string)state;
}), "Start");
progressBar1.Invoke(new ThreadStart(delegate()
{
progressBar1.Visible = false;
}));
}
private bool working = false;
private void button1_Click(object sender, EventArgs e)
{
if (working) //lo tumbo
{
bgWorker.Abort();
((Control)sender).Text = "Start";
progressBar1.Visible = false;
}
else//lo prendo
{
((Control)sender).Text = "Stop";
progressBar1.Visible = true;
bgWorker = new Thread(threadMethod);
bgWorker.Start(new StateInfo { Size = Convert.ToInt32(numericUpDown1.Value), PanMagic=checkBox1.Checked});
}
working = !working;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
class MagicSquare
{
public class MagicSquareEventArgs : EventArgs
{
public int[,] Matrix { get; set; }
}
public delegate void MagicSquareEventHandler(object sender, MagicSquareEventArgs e);
public event EventHandler FindStart;
public event EventHandler FindEnd;
public event MagicSquareEventHandler MagicSquareFound;
public bool PanMagic { get; private set; }
public int Size { get; private set; }
public const int BOTTOM = 1;
public readonly int TOP;
public int N
{
get
{
return Size * ((Size * Size) + 1) / 2;
}
}
public MagicSquare(int _size, bool _panMagic)
{
Size = _size;
TOP = Size * Size;
PanMagic = _panMagic;
}
private bool isPerfectSquareLine(int number)
{
//double sqrt = Math.Sqrt(number);
//return sqrt == (int)sqrt;
return N == number;
}
private bool isMagicSquare(int[,] square)
{
int MATRIX_SIZE = square.GetLength(0);
int tempSum = 0;
#region |
for (int i = 0; i < MATRIX_SIZE; i++)
{
tempSum = 0;
for (int j = 0; j < MATRIX_SIZE; j++)
tempSum += square[i, j];
if (!isPerfectSquareLine(tempSum))
return false;
}
#endregion
#region -
for (int i = 0; i < MATRIX_SIZE; i++)
{
tempSum = 0;
for (int j = 0; j < MATRIX_SIZE; j++)
tempSum += square[j, i];
if (!isPerfectSquareLine(tempSum))
return false;
}
#endregion
if (PanMagic)
{
#region \
tempSum = 0;
for (int i = 0; i < MATRIX_SIZE; i++)
tempSum += square[i, i];
if (!isPerfectSquareLine(tempSum))
return false;
#endregion
#region /
tempSum = 0;
for (int i = 0; i < MATRIX_SIZE; i++)
tempSum += square[i, MATRIX_SIZE - 1 - i];
if (!isPerfectSquareLine(tempSum))
return false;
#endregion
}
return true;
}
private int[,] blankMatrix()
{
int[,] result = new int[Size, Size];
for (int i = 0; i < Size; i++)
for (int j = 0; j < Size; j++)
result[i, j] = BOTTOM - 1;
return result;
}
private int[,] cloneMatrix(int[,] matrix)
{
int[,] result = new int[matrix.GetLength(0), matrix.GetLength(1)];
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
result[i, j] = matrix[i, j];
return result;
}
private bool containsNumber(int n, int[,] matrix)
{
foreach (int i in matrix)
if (i == n) return true;
return false;
}
private bool isFull(int[,] matrix)
{
foreach (int i in matrix)
if (i < BOTTOM) return false;
return true;
}
private List<int[,]> result = null;
private int[,] matrix = null;
public void FindMagicSquares()
{
if (FindStart != null)
FindStart.Invoke(this, new EventArgs());
result = new List<int[,]>();
matrix = blankMatrix();
for (int i = 0; i < Size; i++)
for (int j = 0; j < Size; j++)
findMagicSquares(BOTTOM, i, j);
matrix = null;
if (FindEnd != null)
FindEnd.Invoke(this, new EventArgs());
}
private void findMagicSquares(int n, int i, int j)
{
matrix[i, j] = n++;
if (isFull(matrix))
{
if (isMagicSquare(matrix))
{
result.Add(cloneMatrix(matrix));
if (MagicSquareFound != null)
MagicSquareFound.Invoke(this, new MagicSquareEventArgs { Matrix = result.Last() });
}
}
else
{
if (!containsNumber(n, matrix))
{
for (int _i = 0; _i < Size; _i++)
for (int _j = 0; _j < Size; _j++)
if (matrix[_i, _j] < BOTTOM)
findMagicSquares(n, _i, _j);
}
}
matrix[i, j] = BOTTOM - 1;
}
public List<int[,]> Result { get { return result; } }
}
}
|
|
|
|
|
You calculate the sums of all rows, columns and diagonals to check if a given array is a magic square, right?
You don't have to do it - you can:
(1) stop the calculations right after you get two different sums (two sums at minimum are needed to determine that a given array is not a magic square),
(2) or even better, use the formula SUM = n*(n^2+1)/2 and calculate the sums only to the point when you get one that does not equal SUM (even one sum may prove that a given array is not a magic square).
This should speed up things, although I don't know if this will be enough.
Please use the code block tags when pasting code into your message. And consider pasting only the important pieces.
|
|
|
|
|
hey man thanks for your answer. but I do check for n*(n^2+1)/2, this is the N property of my class, and the method isMagicSquare returns false if there is a single row or column wich sum is different from N. I made a multithreaded approach in a newer to solve the problem, but it still takes too long. but thanks anyway and sorry for the long code. I thought it will be smaller
|
|
|
|
|
Camilo Sánchez Herrera wrote: this is the N property of my class, and the method isMagicSquare returns false if there is a single row or column wich sum is different from N
Sorry, I didn't notice that.
I cannot understand how you generate the squares to check, but, if you're not already doing it, maybe you could check every generated row and only generate a next one if the sum of the previous one equals N.
BTW, is it a school/university assignment?
|
|
|
|
|