tcsoccerman Posted May 22, 2007 Report Posted May 22, 2007 I already have a topic in mgb, but not many people reply there or just not many people go there often. I'll start a new one here. First question: How would i go about having a label which represents integers, not strings? I want to post statistics, not text. Second: How do you save a non .txt file, (in fact a custom made .tm file). that leads to the question how do you make new extensions? I hope more will reply here Btw, i am using the SharpDevelop compiler/debugger.
Samapico Posted May 22, 2007 Report Posted May 22, 2007 How would i go about having a label which represents integers, not strings? I want to post statistics, not text.int result = 4; string mystring = result.ToString(); How do you save a non .txt file, (in fact a custom made .tm file). that leads to the question how do you make new extensions?You have to open a new file as 'write binary', so you'll be saving bytes in there, instead of characters. I don't know the exact methods to use, but it should look pretty much the same as for text, and you can find this easily on any C# help site like MSDN.So you could directly write an array of integers, for example. As for new extensions... it just depends on how you name your new file. However, you have to be careful of how you put your data there, so it must always be readable the same way it was written. For example, if the quan!@#$%^&*y of data is not fixed, you should write an integer specifying how many items there are to read.But basically you can put whatever you want.
tcsoccerman Posted May 22, 2007 Author Report Posted May 22, 2007 Ok, ty much for replying!!. The thing is that i need to do these for the lable/integer thing.(list of things): first i need to retrieve what the value of the label is in int style.second i need to take that #, and add 1 to it.third i need to output the updated #. a little different then what you suggested. i tried this and it turned into a mess of unnasigned variables, and unknown variables. ty very much!
sil Posted May 22, 2007 Report Posted May 22, 2007 For the second question you could do something like:FileInfo fi = new FileInfo(@"c:\sample.tm");StreamWriter sw = fi.AppendText(); or fi.CreateText(); You could also p!@#$%^&* FileInfo a variable.string myPath = @"c:\sample.tm";I use the @ so it treats it as a literal string, otherwise the \ will be treated as an escape character and you will have to do "c:\\sample.tm";It does not really matter what the extension is as long as you are expecting it to behave in a certain way. I am !@#$%^&*uming you are going to treat it like a text file and just read and write to it. For the first question. Are you looking to just post information in the label? Or are you looking to do some mathematical operations on the text in the label and then post the result?
tcsoccerman Posted May 22, 2007 Author Report Posted May 22, 2007 (edited) Do mathmatical operations. And the file needs to be able to handle integers as well as strings. Edited May 22, 2007 by tcsoccerman
Samapico Posted May 22, 2007 Report Posted May 22, 2007 oh... to read an int from a string, you can do something like...string mystring = "2"; int myvalue = int.parse(mystring);However, it would be better to store your value in an integer, and THEN update the label with that value, instead of always referring back to the label when you need to read the value. A label is meant to display an information, not store it. (But yes it would work)
tcsoccerman Posted May 22, 2007 Author Report Posted May 22, 2007 I'd love to not refer back to it, but it's the only way to keep track, i think...i'll try not referring to the label, but just a "behind the scenes in".
sil Posted May 22, 2007 Report Posted May 22, 2007 (edited) okay, lets say your label is called label1 int result = Int32.Parse(label1.Text) + 1; //could do whatever operation you want since it is an int nowlabel1.Text = result.ToString(); edit: i should have known you'd beat me to it =P edit: like Samapico said, you could just create an integer variable, do the operations on that int, and just have the label show that value instead of getting the value fro the label. Edited May 22, 2007 by sil
Samapico Posted May 22, 2007 Report Posted May 22, 2007 You can have a private global variable that keeps track of the value:class myClass { private int myValue = 0; //int initialized to 0, can be seen from the whole class //[... other code... ] private void IncrementValue() { myValue++; myLabel.Text = myValue.ToString(); } }
tcsoccerman Posted May 22, 2007 Author Report Posted May 22, 2007 (edited) I got this error: ALL labels/references exist. The program compiles correctly and creates the program, but while USING(not compiling) it gives the above error. Exception System.FormatException was thrown in debuggee: Input string was not in a correct format. StringToNumber() ParseInt32() Parse() ButtoncreatenewgameClick() - c:\Do!@#$%^&*ents and Settings\Scott\My Do!@#$%^&*ents\SharpDevelop Projects\Team Tracker\MainForm.cs:98,4 OnClick() OnClick() OnMouseUp() WmMouseUp() WndProc() WndProc() WndProc() OnMessage() WndProc() DebuggableCallback() System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManage r.FPushMessageLoop() RunMessageLoopInner() RunMessageLoop() Run() Main() - c:\Do!@#$%^&*ents and Settings\Scott\My Do!@#$%^&*ents\SharpDevelop Projects\Team Tracker\MainForm.cs:28,4 Under this code:EDIT:btw, "teamyouscore" and "teamopponetscore" are the little txtboxes with a up/down arrow that resemble #'s. I guess there called "numericupdown"'s. also, the two radio buttons are to choose if it's a home or away game.hw means homewinsht means hometieetc.... ALL labels/references exist. The program compiles correctly and creates the program, but while USING(not compiling) it gives the above error. void ButtoncreatenewgameClick(object sender, EventArgs e) { int INThw=Int32.Parse(lblhw.Text); int INTht=Int32.Parse(lblht.Text); int INThl=Int32.Parse(lblhl.Text); int INTaw=Int32.Parse(lblaw.Text); int INTat=Int32.Parse(lblat.Text); int INTal=Int32.Parse(lblal.Text); int INTow=INThw+INTaw; int INTol=INThl+INTal; int INTot=INTht+INTat; int INTgoals; int INTpts=(INTow*2)+INTot; MessageBox.Show("Would you like to continue?","Continue?",MessageBoxButtons.YesNo,MessageBoxIcon.Information); if (radiobuttonhome.Checked) { if (teamyouscore.Value > teamopponetscore.Value) { ++INThw; } else if(teamyouscore.Value == teamopponetscore.Value) { ++INTht; } else { ++INThl; } } else { if (teamyouscore.Value > teamopponetscore.Value) { ++INTaw; } else if(teamyouscore.Value == teamopponetscore.Value) { ++INTat; } else { ++INTal; } } //INTgoals=+teamyouscore.Value; lblow.Text=INTow.ToString(); lblol.Text=INTol.ToString(); lblot.Text=INTot.ToString(); lblhw.Text=INThw.ToString(); lblht.Text=INTht.ToString(); lblhl.Text=INThl.ToString(); lblaw.Text=INTaw.ToString(); lblat.Text=INTat.ToString(); lblal.Text=INTal.ToString(); lbloverallrecord.Text = "Overall Record:"; lblawayrecord.Text = "Away Record:"; lblhomerecord.Text = "Home Record:"; } Edited May 22, 2007 by tcsoccerman
rootbear75 Posted May 22, 2007 Report Posted May 22, 2007 (edited) omg... is this the soccer-type mod you're making?i hope you don't plan on using this Edited May 22, 2007 by rootbear75
Samapico Posted May 22, 2007 Report Posted May 22, 2007 uh ok... first thing I noticed you might want to check...when you write: int INTow=INThw+INTaw; You assign it a value... when you increment INThw later, it won't change the value of INTow... I'm not sure if that's what you want... and the error you get is probably because there are non numeric characters in the textboxes...According to MSDN, when this occurs, it throws a FormatException public static int Parse (string s)FormatException s does not consist solely of an optional negative sign followed by a sequence of digits ranging from 0 to 9.So you'd have to use a try catch block to handle invalid values. Either one block for each .Parse() method and handle each value individually, or one single block for every values that would give an error like 'One or more invalid values'//One block for each int INThw, INTht, INThl, INTaw,INTat, INTal; //You must declare your variables outside the block, or they would only be defined for that block try { INThw=Int32.Parse(lblhw.Text); } catch (FormatException) { INThw = 0; //Or whatever value you want it to have in case of an invalid value } //Repeat for each//With a single try/catch block int INThw, INTht, INThl, INTaw,INTat, INTal; //You must declare your variables outside the block, or they would only be defined for that block try { INThw=Int32.Parse(lblhw.Text); INTht=Int32.Parse(lblht.Text); INThl=Int32.Parse(lblhl.Text); INTaw=Int32.Parse(lblaw.Text); INTat=Int32.Parse(lblat.Text); INTal=Int32.Parse(lblal.Text); } catch (FormatException e) { //handle error, could be a messagebox followed by "return;" for example, to simply get out of the method //you can use e.Message to get the actual error message, if you want to put it in your messagebox } In addition, you could also control what the user can type in the textbox directly, using the Textbox_change event... Everytime the content of the textbox would be changed, you'd remove all characters from it, except "0123456789" and maybe '-' if you might need negative numbersThere are many ways to do that... Not sure if the string class has a built-in function for that
tcsoccerman Posted May 23, 2007 Author Report Posted May 23, 2007 (edited) YYYyalk;sdj;faksdjldskjflsdkjf;lsdjfa;dslkjYYYYYYYYYYYyAAAAAAAAAAAAAAYYYYYYYYYYYYYYYYYYY I'm stupid but idc!!!!!!!the problem was that i never made the "lblhw.Text", "lblht.Text", etc. i never made those text = "0", so then it wasn't even a string and such. Now it works perfectly though!!!!!!!!!!!!. OMGGGGGGGGGGG ROFL I"M STUPID!!!! W.E. THANKY YOU MUCH THOUGH FOR THE HELP. I'LL SHOW THE WORKING CODE HERE FOR ANY C# DEV. C# ROCKS AND EVERYONE HERE SHOULD CODE IT!!!. First declare what each label's text is, making sure it is not blank, nor a word(obviously)lblow.Text = "0"; lblol.Text = "0"; lblot.Text = "0"; etc..... Then manage the button click event. to make it work, i made sure that the label !@#$%^&*ignment went in the "if" statement, which could have made it work. either that or !@#$%^&*igning a # to each label.I still feel stupid. void ButtoncreatenewgameClick(object sender, EventArgs e) { int INThw=Int32.Parse(lblhw.Text); int INTht=Int32.Parse(lblht.Text); int INThl=Int32.Parse(lblhl.Text); int INTaw=Int32.Parse(lblaw.Text); int INTat=Int32.Parse(lblat.Text); int INTal=Int32.Parse(lblal.Text); int INTow=Int32.Parse(lblow.Text); int INTol=Int32.Parse(lblol.Text); int INTot=Int32.Parse(lblot.Text); int INTgoals; int INTpts=(INTow*2)+INTot; MessageBox.Show("Would you like to continue?","Continue?",MessageBoxButtons.YesNo,MessageBoxIcon.Information); if (radiobuttonhome.Checked) { if (teamyouscore.Value > teamopponetscore.Value) { ++INThw; ++INTow; lblhw.Text=INTow.ToString(); } else if(teamyouscore.Value == teamopponetscore.Value) { ++INTht; ++INTot; lblht.Text=INTht.ToString(); } else { ++INThl;//:( sad; ++INTol; lblhl.Text=INThl.ToString(); } } else { if (teamyouscore.Value > teamopponetscore.Value) { ++INTaw; ++INTow; lblaw.Text=INTaw.ToString(); } else if(teamyouscore.Value == teamopponetscore.Value) { ++INTat; ++INTot; lblat.Text=INTat.ToString(); } else { ++INTal;//:( sad; ++INTol; lblal.Text=INTal.ToString(); } } //INTgoals=+teamyouscore.Value; lblow.Text=INTow.ToString(); lblol.Text=INTol.ToString(); lblot.Text=INTot.ToString(); lbloverallrecord.Text = "Overall Record:"; lblawayrecord.Text = "Away Record:"; lblhomerecord.Text = "Home Record:"; } Hope this helps everyone Edit: Now to read sil's post and learn how to save all this . Edited May 23, 2007 by tcsoccerman
tcsoccerman Posted May 23, 2007 Author Report Posted May 23, 2007 New Problem/Question: Ok, this question is related to passing variables through multiple forms. Right now i do that by putting parameters in "public MainForm(...)" and then calling the function in another form..etc. Is there another way to do that so i don't have like 30 parameters in "public Mainform(). Not only will it look better, it will be easier to have a function for each other form that relates to MainForm. Ty!
»D1st0rt Posted May 23, 2007 Report Posted May 23, 2007 If you're just looking for basic file output, I write text out to logs in the modular chatnet core I wrote in C#, it may be of some help to you http://svn.slopeout.com/projects/chatbot/r.../Core/Logger.cs
tcsoccerman Posted May 23, 2007 Author Report Posted May 23, 2007 I know how to write to text files, but i need to write "int" 's as well.(numbers). I'll look at it anyways though
Samapico Posted May 23, 2007 Report Posted May 23, 2007 New Problem/Question: Ok, this question is related to passing variables through multiple forms. Right now i do that by putting parameters in "public MainForm(...)" and then calling the function in another form..etc. Is there another way to do that so i don't have like 30 parameters in "public Mainform(). Not only will it look better, it will be easier to have a function for each other form that relates to MainForm. Ty!There are many ways to do this... first of, you could use some sort of parent/child structure...When creating the new form2(), you'd p!@#$%^&* 'this' as parameter, that would refer to the current form (parent)//in mainform (parent) //All the values you need to access from the child form should be public in mainform public int valueToUse1; public int valueToUse2; public string stringToUse; ... [...] //P!@#$%^&* a reference to the current class (mainform, in this case) when constructing your new form Form childform = new form2(this); [...] //in form2 (child) private object parentform; public form2(object parent) { parentform = parent; } public void somemethod() { //Now you can refer to any public members of the mainform... can be either variables, or methods if (parent.valueToUse1 == parent.valueToUse2) parent.somePublicMethod(); else { int sampleInt = parent.calculateSomething(); //dostuff or whatever } } Another thing you can do is have all the values you need to p!@#$%^&* on be part of a struct (or class), and you could just p!@#$%^&* that object to your form, which might simplify some things. But it's less pretty imo I'm not an OO programming beast yet, so there might be a better way to do it... But anyway, usually you'd want to avoid having too much algorythm stuff going on in your form code itself, and you'd have other classes taking care of that... and in your case, both forms would use the same instance of that class... And the form would just send form input to the class and get whatever results you need from it.So yeah I think that would be even better...
tcsoccerman Posted May 23, 2007 Author Report Posted May 23, 2007 (edited) I'm really sorry, but i don't get most of that. i learn best though with examples, so if you could have:MainForm()AddTeam() //this is the child form and there is a variable in AddTeam() (child form) called "teamnametxtbox.Text" that i want to p!@#$%^&* to the mainform(MainForm) and make it = "lblteamname.Text". That would be pimpin. Sampapico rocks. Oh, btw, i attached the program in topic "Team Tracker Program, version 1.0". I learned how to save . Edited May 23, 2007 by tcsoccerman
rootbear75 Posted May 23, 2007 Report Posted May 23, 2007 //in mainform (parent) //All the values you need to access from the child form should be public in mainform public int valueToUse1; public int valueToUse2; public string stringToUse; ... [...] //P!@#$%^&* a reference to the current class (mainform, in this case) when constructing your new form Form childform = new form2(this); [...] //in form2 (child) private object parentform; public form2(object parent) { parentform = parent; } public void somemethod() { //Now you can refer to any public members of the mainform... can be either variables, or methods if (parent.valueToUse1 == parent.valueToUse2) parent.somePublicMethod(); else { int sampleInt = parent.calculateSomething(); //dostuff or whatever } }isnt that java? not C#
tcsoccerman Posted May 23, 2007 Author Report Posted May 23, 2007 No that's c#, btw root, this isn't the mod for soccer in my zone. I don't want to be owner anymore lol, i'm to caught up in c# programming.
Samapico Posted May 24, 2007 Report Posted May 24, 2007 I think c# looks alot like java... and it can't be java, cause I don't know java at all
»D1st0rt Posted May 24, 2007 Report Posted May 24, 2007 You do realize you can put numbers in text files right?
sil Posted May 24, 2007 Report Posted May 24, 2007 I think c# looks alot like java... and it can't be java, cause I don't know java at all C# is very very similar to java. Heck most of the calls to methods have only one difference: the case of the first letter. There are, however, other differences.
»doc flabby Posted May 24, 2007 Report Posted May 24, 2007 C# is very very similar to java. Heck most of the calls to methods have only one difference: the case of the first letter.Its incredibly similar in many ways.I once ported a program from java to c# and i only had to rename the toString to ToString calls to get it to work.This is a rarity but normally about 90% of the code will match
sil Posted May 24, 2007 Report Posted May 24, 2007 C# is very very similar to java. Heck most of the calls to methods have only one difference: the case of the first letter.Its incredibly similar in many ways.I once ported a program from java to c# and i only had to rename the toString to ToString calls to get it to work.This is a rarity but normally about 90% of the code will match Yeah, but I have noticed some calls that exist in C# that do not exist in Java and visa versa (don't ask me to name them, I don't remember). Or at least, it was not as simple as changing the case of the first letter =) But it is funny, they made C# to be so freaking similar to Java, and then they made J# which is actually a clone of Java (or so I've been told, never really messed with it). Why make essentially two copies of the same language?
Recommended Posts