System.FormatException - Appeared after I've added a new class - c#

(I'm new to C#). After I've split-up my class (which included they GUI and the calculation of the code) in each of its parts. (One class the GUI, one the program itself), it shows evertime I press on the Button for the calculation: System.FormatException
Following is the first class
class Annuitätenrechner : Annuitätendarlehen
{
public void AnRechner() {
int betrag;
betrag = Convert.ToInt32(betrag2.Text);
betrag = int.Parse(betrag2.Text);
[Irrelevent stuff following...]
}
}
Second class
public partial class Annuitätendarlehen : Window
{
public Annuitätendarlehen()
{
InitializeComponent();
}
private void berechnen2_Click(object sender, RoutedEventArgs e)
{
var AnRe = new Annuitätenrechner();
AnRe.AnRechner();
}
Got no idea why it doesnt work <.<

Rene's comment makes great sense already.
I would like to add my 2 cents:
1) In case you want to break whenever an exception is thrown, you could take a look at VS 2013 and before or enter link description here.
2) You can check the $exception virtual variable in watch window and check its "stack" or inner exceptions for more detail of the error. Looking into the detail of the exception and the error message always helps.
For the question itself, you could also took a look at Int32.Parse or Convert.ToInt32's document and check when "FormatException" will be thrown.
According to the document, betrag2.Text was not a valid integer when the button was triggered.

Related

Error CS1674 trying to open second form

I'm sure, this is a total newbie question, but I'm hanging for hours now...
My main form should have a method that opens a second form with a textbox. Just to have a kind of messagebox with scrollbar. I know there are lots of examples out there, but they are all too extensive for my needs. So here is my code so far:
private void ShowBox (string info)
{
if (info != "")
{
using (var form = new Message())
{
// work to do
}
}
}
public partial class Message : Form
{
public Message()
{
InitializeComponent();
say.Text = "OK";
}
}
Message is my 2nd form. I receive CS1674 (type used in a using statement must be implicitly convertible to 'System.IDisposable') error on the using line.
I already took this as reference Opening and closing form2 from form1 C# but I don't see the difference to my code.
What am I doing wrong?
Message probably resolves as System.Windows.Forms.Message which is a structure.
Ctrl+Click on your usage of new Message() will jump to the declaration.
In namespace System.Windows.Forms there is already a struct named Message. Seems like the compiler confounds your class with that. You should rename your form to something like MessageForm.

how to set up class instance control events?

I have a class Viewer that creates two linked FastColoredTextBoxes. I want the two boxes to scroll together horizontally. I have this code:
public class Viewer : Panel
{
public FastColoredTextBox HeaderRow = new FastColoredTextBox();
public FastColoredTextBox Editor = new FastColoredTextBox();
public Viewer(int _Top, int _Left, int _Height, int _Width, bool _HasHeaderRow, Control control)
{
this.Editor.Scroll += new ScrollEventHandler(Editor_Scroll);
}
void Editor_Scroll(object sender, ScrollEventArgs e)
{
if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
{
this.HeaderRow.HorizontalScroll.Value = this.Editor.HorizontalScroll.Value;
}
this.HeaderRow.UpdateScrollbars();
}
}
It doesn't work. I've never tried to do attach events to controls in a class instance before. If I declare the controls in my form and attach a very similar event (minus the .this's) it works fine. Thank you.
i think that for the next time try to tell yourself " what could it be?" and maybe debug a little, like a breackpoint for example. as you probably understood, you had a little mistake in the line
this.HeaderRow.HorizontalScroll.Value = this.HeaderRow.HorizontalScroll.Value;
you meant to write
HeaderRow.HorizontalScroll.Value = Editor.HorizontalScroll.Value;
you just got mixed between the two or something, which happens to all of us. but the first thing i would do is to think and debug it, check the values and let someone look at it. only then post it here.

c# objects modification: a strange behaviour

I'm developing a WPF C# application and I have a strange behaviour in modification of objects. I try to explain it in general way.
Suppose that you have an object of a class described as follows:
public class A
{
int one;
bool two;
List<B> listofBObjects;
}
where B is:
public class B
{
int three;
int four;
}
I pass an instance of A class and an instance of B class from a window to another, only defining two variables of type A and B in the second window and passing them before the Show() method, with the following code, executed into an instance of window FirstWindow:
SecondWindow newWindow = new SecondWindow();
newWindow.instanceOfA = this.instanceOfA; //instanceOfA is of type A
newWindow.instanceOfB = this.instanceOfA.listOfBObjects[0]; //instanceOfB is of type B
newWindow.Show();
If I have to repeat this code twice(that is, opening twice the window), in the first execution everything works as expected, infact if I modify values in instanceOfB variable, I see the modification also in instanceOfA variable. But, in the second execution, the modification in instanceOfB does not affect instanceOfA...
The modifications are done in newWindow. For example:
this.instanceOfB.three++;
this.instanceOfB.four--;
Imagine that you are in the FirstWindow. Click on a button and SecondWindow opens, passing both variables as described above. In SecondWindow, do some modifications, click on OK and SecondWindow closes, returning control to FirstWindow. If I reclick on the same button, I reopen SecondWindow. If I do modifications now, they do not affect both variables.
I try to have a look (in VS2012) at both variables in the console with control expression and I see that, in the first pass of code, both variables changes when code above is executed but, in the second pass of code, only instanceOfB changes...
EDIT:
Following the code that I use to pass parameters to SecondWindow...types are explaind below
IntermediatePosition obj = ((FrameworkElement)sender).DataContext as IntermediatePosition; //IntermediatePosition is Class B
IntermediatePositionsSettingsWindow ips = new IntermediatePositionsSettingsWindow();
ips.currentIntermediatePosition = obj;//this is the instanceOfB
ips.idxOfIpToModify = obj.index;
ips.currentSingleProperty = this.currentPropertyToShow; //this is the instanceOfA object
ips.sideIndex = this.sideIndex;
ips.ShowDialog();
Consider that obj is given by a button selection into a datagrid, in which each row represents an IntermediatePosition object. In the datagrid, there is a column button and, clicking by buttons, IntermediatePositionsSettingsWindow is opened with the proper data
EDIT:
I've performed the folloqing check:
this.currentPropertyToShow.sides[this.sideIndex].intermediatePositionList[i].Ge‌​tHashCode() == obj.GetHashCode()
where i is the index of related IntermediatePosition object. At first usage of IntermediatePositionsSettingsWindow the objects result equals, but in second usage they are different
Why this thing happens?
If it is needed any other clarification, I will edit the question
Thanks
It's difficult to give a proper answer to this, as there is insufficient code to correctly work out the issue. However, if you are databinding, then I believe you need to implement this interface. It is possible that you're issue is simply that you're model is not reflecting the changes to the screen.
I can't reproduce your problem. Here's a simplified representation of your class relation (as I understood from your question). Please let us know if this is correct:
public partial class MainWindow : Window
{
internal A instanceOfA;
internal B instanceOfB;
public MainWindow()
{
InitializeComponent();
instanceOfB = new B() { };
instanceOfA = new A() { listOfBObjects = new List<B>() { instanceOfB } };
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SecondWindow newWindow = new SecondWindow();
newWindow.instanceOfA = this.instanceOfA; //instanceOfA is of type A
newWindow.instanceOfB = this.instanceOfA.listOfBObjects[0]; //instanceOfB is of type B
newWindow.Show();
}
}
public partial class SecondWindow : Window
{
internal A instanceOfA;
internal B instanceOfB;
public SecondWindow()
{
InitializeComponent();
Loaded += SecondWindow_Loaded;
}
void SecondWindow_Loaded(object sender, RoutedEventArgs e)
{
MessageBox
.Show(String.Format("{0}",
this.instanceOfB == this.instanceOfA.listOfBObjects[0]));
this.instanceOfB.three++;
this.instanceOfB.four--;
}
}
Note: this is not an answer, just trying to establish some common ground for further discussions, as comments don't leave you enough freedom for code samples.
Thanks to #pm_2 and #BillZhang comments, I found a row in my code in which this.currentPropertyToShowwas edited. After the returning back at first window, infact, I perform the refresh of the window, but it is not needed to edit this.currentPropertyToShow, so I have commented it and everything works!
Thanks everybody for precious comments and suggestions!

C# make method required

I have a class with a method that when not called causes a crash.
Is there a way to make a compilation failure when the method isn't called?
Edit:
So what I have is basically a class that makes an istance of another class (a form) and it is a mesagebox with a do not show again option. Here's an example of how you'd use it.
public partial class Form1 : Form {
DontShowAgainBox box;
public void AlertYes() {
if (box.form.showagain.Checked)
t1.Text = "You chose yes (checked)!!!";
else
t1.Text = "You chose yes (unchecked)!!!";
}
public void AlertNo() {
if (box.form.showagain.Checked)
t1.Text = "You chose no (checked)!!!";
else
t1.Text = "You chose no (unchecked)!!!";
}
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
box = new DontShowAgainBox("Warning", "Are you sure?");
Action yesaction = new Action(AlertYes);
box.Bind_Yes(yesaction);
Action noaction = new Action(AlertNo);
box.Bind_No(noaction);
box.SetNoButton("Nope");
box.SetYesButton("I'm sure");
box.Show();
}
}
There's another method that you can hide the "No" button with also.
But the yes button is in every instance of the class so it needs to have a function associated with it or else... crash.
Why not just test for this in software instead of crashing?
if (!init_called) {
print error
exit
}
It is not possible for the compiler or linker to say, at build time, "Hey, this method that was supposed to be called wasn't called!" You can't make the compiler issue an error message because client code didn't call a particular method.
What you're asking is not possible.

How do I get the About box to appear in C#?

I have an About box in my C# project using Microsoft's Visual C# 2008 Express Edition named AboutBox1. I have made it look how I want it in the design view, but how do I make it appear when the About link in the Help menu is clicked?
This codes makes an About box appear, but it looks blank. It's not the one I designed.
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 box = new AboutBox1();
box.ShowDialog();
}
Any thoughts or suggestions would be appreciated. Thanks.
Got it.
The about box is driven off of assembly properties for your project.
Go to Project -> 'ProjectName' Properties -> Assembly Information.
You set all of the information there.
If you try to set the information in the Property Explorer it will simply be over written at run time by what ever is in this window.
Cheers,
Mike
It sounds to me like a borked designer surface... have you hit save and rebuilt it? Perhaps close the IDE, reopen it, and check that your carefully designed form is still pretty?
BTW, when using ShowDialog you should also use using (since it doesn't Dispose() itself when shown with ShowDialog):
using(AboutBox1 box = new AboutBox1()) {
box.ShowDialog(this);
}
Did you remove the method-call to 'InitializeComponent' in the constructor of your AboutBox - form ?
Your constructor should at least look like this:
public partial class AboutBox : Form
{
public AboutBox()
{
InitializeComponent ();
}
}
Where the InitializeComponent method call should be the first line in the constructor.
If it appears but is blank, the problem is in AboutBox1. Show us some of that code.
I faced same problem before but I solved it by removing the statements below the InitializeComponent();
Default code:
partial class AboutBox1 : Form
{
public AboutBox1()
{
InitializeComponent();
this.Text = String.Format("About {0} {0}", AssemblyTitle);
this.labelProductName.Text = AssemblyProduct;
this.labelVersion.Text = String.Format("Version {0} {0}", AssemblyVersion);
this.labelCopyright.Text = AssemblyCopyright;
this.labelCompanyName.Text = AssemblyCompany;
this.textBoxDescription.Text = AssemblyDescription;
}
}
My final code:
partial class AboutBox1 : Form
{
public AboutBox1()
{
InitializeComponent();
}
}
I couldn't find the project / project name/ assembly properties.
But commenting out the lines after InitializeComponent(); worked for me.
This is how mine looks:
public frmAboutBox1()
{
InitializeComponent();
//this.Text = String.Format("About {0}", AssemblyTitle);
//this.labelMyFFEProductName.Text = AssemblyProduct;
//this.labelVersion.Text = String.Format("Version {0}", AssemblyVersion);
//this.labelCopyright.Text = AssemblyCopyright;
//this.labelCompanyName.Text = AssemblyCompany;
//this.textBoxDescription.Text = AssemblyDescription;
}
If you are an amateur like me, to find these lines, click the AboutBox in the project explorer, and hit the View Code button <>.

Categories

Resources