wallish 0 Newbie Poster

Unfortunately that's not my problem. I know which button is clicked (using my "clickCheck" function), but in order to assign a function to a button I have to take every single button and put it into an if statement (or switch-case statement like you used). If I had 30-50 buttons spread out across four different GUIs (like, say, inventory, player profile, main menu, combat, etc) I would have a very large section of code that would be a bit out of place.

So I would instead like to know if there is a way to "assign" a function to a button so that I could later just call that function without using a click check. Here's an example of how it is now:

if(aMouse.LeftButton == ButtonState.Pressed) {
   // check if FirstButton has been clicked
   if(FirstButton.clickCheck(aMouse.X, aMouse.Y) == true) {
      this.Quit();  // in this case, FirstButton's function is to quit
   }
   // check if SecondButton has been clicked
   if(SecondButton.clickCheck(aMouse.X, aMouse.Y) == true) {
      // something else
   }
   // check if ThirdButton has been clicked...
   if(ThirdButton.clickCheck(...) == true) {
   .
   .
   .
   // and so on for all buttons, of which there could be too many
}

The problem with this (or a switch-case) statement is that I have to define my buttons from within the mouse's event check. If I have a lot of buttons that's a very very long set of if statements and isn't a very elegant way of doing things.

Instead I would like something …

wallish 0 Newbie Poster

Hi.

I'm currently messing around with C# and decided to write a small game. One of the first things I am doing is setting up the game's GUI, which I am designing myself with help from XNA (but not Windows Forms).

I've already made proof-of-concept buttons that can be placed, scaled, given multiple images (default, clicked, hovered, and disabled), as well as a "GUI" element that can hold sets of buttons and handle their enabled/disabled state en-masse (for switching between menus).

My problem is with the function of the buttons themselves. Right now, when the mouse is clicked, it runs through the current GUI's buttons using a function of my Button class called "clickCheck" to see if the mouse has been clicked within a specific button. This works great when I have only a few buttons, but it seems like it will get out of control if I ever get a "game" going as there will be many buttons.

The reason is that in the "foreach" statement, where I run all the clickChecks, I have to put in if statements for every button, for every GUI. So right now it is something like:

if(aButton.clickCheck == true) {
          ...whatever it does...
      }
      if(bButton.clickCheck == true) {
          ...whatever it does...
      }
      if(cButton.clickCheck == true) {
      .
      .
      .
}

So my question is this: is there a way to define a function that each individual button will perform when clicked. This way I could, say, set up …

wallish 0 Newbie Poster

No. Static means you cant make instances, its created at the start, and dies at the end, but like "Colors.White" is available everywhere.

Alright, I guess that is what I need for the mainUser problem, thanks. Now, if I were going to create some sort of user entry program, in which the several fields could be filled in and a new user is created, would there be any way to modify those newly created instances somewhere else in the program, or is that problem complicated enough to warrant a new topic when I come to it?

wallish 0 Newbie Poster

Then I dont understand why you cant have a static class which is availble then throughout all the application.

Sorry if I'm wrong as I am new to C#, but doesn't static mean that the members of the class could never be changed? The purpose of my program here is that I click the button to change the "name" value in the class.

wallish 0 Newbie Poster

Do you ever have more than 1 user variable defined?

Not in this program, no. It's a simple "let's test out C# to see how it's like" program. In the future, were I to create more complicated programs, there might be more than one user. But in this program the one simple thing I need is to be able to use User mainUser from Program.cs in Form1.cs.

wallish 0 Newbie Poster

If user were a static class you could access it from anywhere

I think you misunderstand. I can declare a new User object anywhere, but I simply cannot change the values of a User object that has been declared in another source file. Like this:

User.cs // contains the User class definition
Program.cs // Contains main(). Here is "User mainUser = new User("John Doe")"
   -- I now have a successfully created User called mainUser. It can  
   -- be modified from within Program.cs
Form1.cs // In here is the form source.  In the source is a button's Click event.
   --> button1_Click(object sender, EventArgs e) // the button's Click event.
         --> mainUser.setName(textBox1.Text);  // this is what it should do when clicked.

The result of this setup is a compiler error and the message "The name 'mainUser' does not exists in this context" in Form1.cs.

There just HAS to be a way to do all this. Otherwise every single object ever created would have to be created within the source file for a single Form...

wallish 0 Newbie Poster

I really hate to bump threads, but it just seems like there should be a simple solution to this. Once again the problem is basically that I have two source files: Form1.cs and Program.cs. In Program.cs I have created an instance of object User called mainUser. I would like to modify some of mainUser's values using a button whose source is located within Form1.cs. How can I modify/use mainUser in Form1.cs when mainUser is created in Program.cs? Please note that creating mainUser in Form1.cs doesn't solve this problem because that would essentially mean that all projects would have to be in one source file.

For C++ users: I'm basically looking for a similar method to "extern [object]".

wallish 0 Newbie Poster

Long as you declare the main user as a private varaible (or public if you need to for later) within form so it goes

partiial class Form1 
{
  private User mainuser;
 // more stuff
}

You can access it form any of the form1's procedures, just like you did with c++

The problem lies in that mainUser is declared in the main function, not in the Form function. I'd like manipulate that instance of mainUser. So I guess the question should be is there a way to have a specific instance of an object be available throughout the whole namespace or at least be passed from between "static void Main()" in Program.cs to "public partial class Form1 : Form" in Form1.cs?

A C++ example would be having two source files, main.cpp and users.cpp. In int main() I declare User mainUser = new User(). In users.cpp, since I would like to use mainUser, I'd just use "extern mainUser" to bring it in to the entire source file. Not exactly the best programming habit but the easiest way to describe what I'd like to do here. Hell, I'd even like to just be able to pass mainUser to a function in users.cpp.

wallish 0 Newbie Poster

hi...
Create the object of your user class in button click and manipulate the data of the object as you wish...

That would work fine if the object was solely localised to the button click area. I'd like to have background logic and object manipulation and use the user interface solely as a frontend.

wallish 0 Newbie Poster

Oh, that's User mainUser. Sorry about that.

wallish 0 Newbie Poster

I'm relatively new to C#, having migrated over from C++, so I'm a little confused about the use of variables.

I have a class called User located in the program's main namespace

// In User.cs

namespace TestProgram 
{
     class User
     {
          public string name;
          public int id;
          
          public User()
          {
              name = "John Doe";
              id = 0;
          }
          public void setName(string nName)   // I know these two can be 
          {                                   // turned into C# style
              name = nName;                   // with get and set
          }                                   // but one step at a time...
          public string getName()
          {
              return name;
          }
      }
}

In the main file Program.cs, under the Main() function in the same namespace, I've got

User.mainUser = new User();
// This works fine and produces no errors.
// I can mess with the mainUser object just fine as long as I do so within Main()

Now using VS 2005's form generator I've put in a label, a textBox, and a button. I'd like it to, when the button is clicked, take textBox1.Text's value, change mainUser's name to that value, and then change the label to the name as well. All that is in Form1.cs as generated by VS.

So my problem/question is basically that the button1_Click(...) function, which is located in the partial class Form1 in the same namespace TestProgram, cannot find that declared mainUser and therefore cannot get/set any of its values.

// In Form1.cs
// namespace TestProgram -> public …
wallish 0 Newbie Poster

first, use std::string's find() method to find the first space, then use its substr() method to extract the first word

std::string line = "name    =  John";
std::string tag;

std::string::size_type pos = line.find(' ');
if(pos != std::string::npos)
{
   // first space found.  So extract the first word
   tag = line.substr(0,pos);
}

Now, use code similar to above to find the '=' symbol, and extract everything after that. You will also want to skip all the spaces between '=' and the first non-white-space character.

Oh, now I see what you meant. Thanks again for the help.

wallish 0 Newbie Poster

strings or c-strings? You need to give us more information on how your program currently is written.

Well, I would prefer to use strings and not c-strings, but that isn't important. Also, I did not include any real code because this part of the project is completely independent from the rest. This code will essentially be just a function or whatever that is called in the start to load up different sets of information from pre-set files.

wallish 0 Newbie Poster

it won't matter how many spaces are between the marker and the '=' symbol. After reading the whole line into memory take the first word (which starts with the first space on the line). If its the marker you want search for the '=' symbol and the text you want follows that (ignoring the spaces between '=' and the text).

That sounds just like what I wanted!
Now for follow up questions tough: How do I search through the line? Am I going to use a simple ifstream to skip the whitespaces and store as strings? Am I going to use the entire line string and cut it up somehow? If I do use ifstream, how can I make it handle values with spaces (like if the name was "Bob Jones" instead of just "Bob"). Thanks for your help.

wallish 0 Newbie Poster

Well, I'm working on a simple project but I've hit a snag in my progress so far. I'd like to take a file that has text which will be something similar to:

Placemarker 1       = words/numbers
Placemarker 2       = words/numbers
Placemarker 3       = words/numbers

so you'll have a file like:

Name        = Bob
Job         = Student
Whatever    = stuff

Name        = John
Job         = Student
Whatever    = things

The place markers are just for readabilty of the resulting text file.

So I'd like to know how to use C++ (maybe fstream?) to:
a. check for the placemarker
b. if the marker is the one I want (like "Name") skip to the "= " and take the value after that to be placed in a temp variable for checking.
c. See if the variable (such as "Bob") matches the one I want
d. Then repeat the process so many times line by line (to maybe input into a structure of values) until it hits the blank line and will then stop.

So the user could type in "Bob", the set text file "whatever.txt" would be opened, name lines would be found, then checked for "Bob", then input the data if matched.

I (obviously) don't know enough about C++ I/O and fstream to figure this out, but I am already thinking that a set number of characters (include spaces) between the start of the marker ("|Name") and the end of the = thing …