|
Hello everyone,
Having a difficult time adding code to reject non numeric user inputs into text boxes. Can someone please view the following code and give me a path forward? Anything I've tried has resulted in errors.
<
private void btnCalculate_Click(object sender, EventArgs e)
{
double dblPresentValue = 0;
double dblSalvageValue = 0;
int intYearsOfDepreciation = 0;
int intCounter = 0;
string strDisplay = "";
txtDisplay.Text = "";
dblPresentValue = Convert.ToDouble(txtPresentValue.Text);
dblSalvageValue = Convert.ToDouble(txtSalvageValue.Text);
intYearsOfDepreciation = Convert.ToInt32(txtYearsOfDepreciation.Text);
if (dblPresentValue < 1 | dblPresentValue > 1000000)
{
MessageBox.Show("Present value must be between $1 and $1,000,000");
txtPresentValue.Select();
txtPresentValue.SelectAll();
}
else if (dblSalvageValue > dblPresentValue)
{
MessageBox.Show("Salvage value cannot be greater than Present Value");
txtSalvageValue.Select();
txtSalvageValue.SelectAll();
}
else if (intYearsOfDepreciation < 0 | intYearsOfDepreciation > 25)
{
MessageBox.Show("Years of Depreciation must be in 1 - 25 range");
txtYearsOfDepreciation.Select();
txtYearsOfDepreciation.SelectAll();
}
else
{
strDisplay = "Year Asset Value\r\n------- ---------------";
double period = (dblPresentValue - dblSalvageValue)/intYearsOfDepreciation;
int curYear = DateTime.Now.Year;
while (intCounter < intYearsOfDepreciation)
{
strDisplay += "\r\n"+ curYear.ToString() + ": " + dblPresentValue.ToString("C2");
intCounter = intCounter + 1;
dblPresentValue -= period;
curYear++;
}
txtDisplay.Text = strDisplay;
}
}
|
|
|
|
|
First off, stop using Convert functions on user input - they are designed to throw an exception if the value is not numeric, and that means when your user misstypes your app crashes.
Use the TryParse methods instead, and exit the function when it fails:
double presentValue;
if (!double.TryParse(txtPresentValue.Text, out presentValue))
{
MessageBox.Show("Present value must be a numeric value");
return;
} Then you can add your validations, and again return if they do not succeed.
A better way is to use a NumericUpDown control instead of a textbox as the user can't enter invalid numbers into them at all...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
This worked great, appreciate the help!
|
|
|
|
|
You're welcome!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hello,
I have a problem with my application at the time of printing. When I print with the print button of the report viewer in the viewer my printing works fine. And when I run printing without previewing the report limit is limited to where the rectangle is limited. I specify well I use a thermal printer in roll paper
Here is my code
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Reporting.WinForms;
namespace POS_System_Inventory
{
public static class LocalReportExtensions
{
public static void PrintToPrinter(this LocalReport report)
{
PageSettings pageSettings = new PageSettings();
pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize;
pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape;
pageSettings.Margins = report.GetDefaultPageSettings().Margins;
Print(report, pageSettings);
}
public static void Print(this LocalReport report, PageSettings pageSettings)
{
string deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
<PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
<MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
<MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
<MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
<MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
</DeviceInfo>";
Warning[] warnings;
var streams = new List<Stream>();
var pageIndex = 0;
report.Render("Image", deviceInfo,
(name, fileNameExtension, encoding, mimeType, willSeek) =>
{
MemoryStream stream = new MemoryStream();
streams.Add(stream);
return stream;
}, out warnings);
foreach (Stream stream in streams)
stream.Position = 0;
if (streams == null || streams.Count == 0)
throw new Exception("Aucun flux à imprimer.");
using (PrintDocument printDocument = new PrintDocument())
{
printDocument.DefaultPageSettings = pageSettings;
if (!printDocument.PrinterSettings.IsValid)
throw new Exception("Impossible de trouver l'imprimante par défaut.");
else
{
printDocument.PrintPage += (sender, e) =>
{
Metafile pageImage = new Metafile(streams[pageIndex]);
Rectangle adjustedRect = new Rectangle(e.PageBounds.Left - (int)e.PageSettings.HardMarginX, e.PageBounds.Top - (int)e.PageSettings.HardMarginY, e.PageBounds.Width, e.PageBounds.Height);
e.Graphics.FillRectangle(Brushes.White, adjustedRect);
e.Graphics.DrawImage(pageImage, adjustedRect);
pageIndex++;
e.HasMorePages = (pageIndex < streams.Count);
e.Graphics.DrawRectangle(Pens.Red, adjustedRect);
};
printDocument.EndPrint += (Sender, e) =>
{
if (streams != null)
{
foreach (Stream stream in streams)
stream.Close();
streams = null;
}
};
printDocument.Print();
}
}
}
}
}
modified 22-Jun-21 8:24am.
|
|
|
|
|
Repost: this is the same problem you asked about 3 days ago: Printe page limit - C# Discussion Boards[^]
Don't repost the same question repeatedly - it just wastes time and effort!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Looking at your code with a very limited understanding of the domain, I am wondering if
Member 14192216 wrote:
string deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
<PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
<MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
<MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
<MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
<MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
</DeviceInfo>"; is meant to be an interpolated string. If so, try changing the @"..." to $@"..." so that the expressions inside {...} sections are evaluated. It may be that the uninterpolated values are being treated as invalid number, which are treated a undefined items so the defaults for the geometry are used.
|
|
|
|
|
You're confusing "logical" printing and "physical" printing.
There is no evidence that the "thermal printer" understands what you're sending it. (thermal printers can have their own "language")
You start by sending "one line" / chaeacter to the printer. When that works, you'll have some confidence you're on the right track; otherwise, you're just grasping.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
|
I only found "commercial" "high performance" charting software. For open-source, it's not a priority.
And then you need to choose Windows Forms and / or WPF, etc.
If it is static or dynamic (I needed dynamic for a client). With static you could use a (WPF/UWP) ViewBox for scrolling.
For about $1000, you get to do less thinking about it.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
@honeythecodewitch
Check out honeythecodewitch's work on IOT controller graphics: [^].
I'd say she is the right person on CP to advise you.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
The C# part should give you a clue that the "graph/diagram" IS NOT running on an IOT; but obviously not.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
If you were familiar with the range of HTC's projects, and her competencies ...
You would have to find another way to troll.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
More than you apparently; HTC is using C++.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Any idea why the standard:
Form2.Show()
wouldn't be working for me? I'm trying to link a menu button to another form.
|
|
|
|
|
It doesn't like you?
Seriously. How could anybody guess why it's "not working" when you have provided no details whatsoever?
And "not working" is not a proper description of a problem.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I am attempting to link a form to a menu button. The button is an "about" for the app, named FormAbout.
<
private void menAbout_Click(object sender, EventArgs e)
{
FormAbout formabout = new FormAbout();
FormAbout.Show();
}
this is my latest attempt. Still brings up an error:
Severity Code Description Project File Line Suppression State
Error CS0120 An object reference is required for the non-static field, method, or property 'Control.Show()' StraightlineDepreciationCalculator C:\Users\koval\source\repos\StraightlineDepreciationCalculator\StraightlineDepreciationCalculator\Straightline Depreciation Calculator.cs 208 Active
|
|
|
|
|
Good. the answer is easy.
You are calling Show on the wrong name. You are calling it on FormAbout when you need to be calling it on formabout.
FormAbout is the type name. formabout is the instance name.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Thanks, I knew it had to be something easy. Spent way to much time on that, lol. Doing my best as a beginner.
|
|
|
|
|
If it starts with a capital letter, you're probably accessing a class name instead of an (local) object instance.
(For the Troll, that assumes one has naming standards).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
modified 23-Jun-21 12:21pm.
|
|
|
|
|
Thank you for clearing that up for me.
|
|
|
|
|
Hey guys,
As a beginner I'm going through exercises to try an learn the basics of C#. This exercise calls for a straightline depreciation to be calculated with user entering inputs for present value, salvage value, and years of depreciation. I'm stuck at this point and don't know how I should proceed -- if anyone can review and provide me with some direction it would be appreciated!
<
namespace StraightlineDepreciationCalculator
{
public partial class frmStraightlinedDepreciationCalculator : Form
{
public frmStraightlinedDepreciationCalculator()
{
InitializeComponent();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtPresentValue.Text = "";
txtSalvageValue.Text = "";
txtYearsOfDepreciation.Text = "";
txtDisplay.Text = "";
txtPresentValue.SelectAll();
txtPresentValue.Select();
}
private void btnExit_Click(object sender, EventArgs e)
{
DialogResult dlgresult;
dlgresult = MessageBox.Show("Do you want to exit the application?", "Close the form", MessageBoxButtons.YesNo);
if (dlgresult == DialogResult.Yes)
{
System.Environment.Exit(1);
}
else
{
txtPresentValue.Select();
}
}
private void btnCalculate_Click(object sender, EventArgs e)
{
double dblPresentValue = 0;
double dblSalvageValue = 0;
int intYearsOfDepreciation = 0;
int intCounter = 0;
string strDisplay = "";
txtDisplay.Text = "";
dblPresentValue = Convert.ToDouble(txtPresentValue.Text);
dblSalvageValue = Convert.ToDouble(txtSalvageValue.Text);
intYearsOfDepreciation = Convert.ToInt32(txtYearsOfDepreciation.Text);
if (dblPresentValue < 1 | dblPresentValue > 1000000)
{
MessageBox.Show("Present value must be between $1 and $1,000,000");
txtPresentValue.Select();
txtPresentValue.SelectAll();
}
else if (dblSalvageValue > dblPresentValue)
{
MessageBox.Show("Salvage value cannot be greater than Present Value");
txtSalvageValue.Select();
txtSalvageValue.SelectAll();
}
else if (intYearsOfDepreciation < 0 | intYearsOfDepreciation > 25)
{
MessageBox.Show("Years of Depreciation must be in 1 - 25 range");
txtYearsOfDepreciation.Select();
txtYearsOfDepreciation.SelectAll();
}
else
{
strDisplay = "Year Asset Value/r/n------- ---------------";
}
}
}
}
|
|
|
|
|
What do you need help with?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
getting a proper straightline depreciation calculation + display. I'm stuck, not sure where to go from here.
|
|
|
|
|
Well, do you know what "straight line depreciation" is?
How do you work it out manually?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|