I’m trying to make a relatively basic text based RPG in C# in Visual Studio. An I’ve gotten hung up trying to have a method in a class react to a button click on a form. Like to move from one method to another based on the button clicked.
Let me give you a basic layout of the program.
I have a frmMain and a cls_EVENTS. It’ll get more complex, but that’s for a time once I actually get stuff working.
I want the frmMain to be where the player interacts with the program. It has the text screen, buttons, status meters, most of the good fun stuff. Below the text screen are 6 buttons (labeled btn1 – btn6, respectively) that I plan on changing the text to, to reflect the available actions of the current scene/events (inspect that, go right, tell that guy that you and his mother had dinner last night).
the controls to ‘make’ the main menu are in cls_EVENTS, mostly to make it more convenient for a Main Menu button to just simply call it to get the player back to the main menu (rather then just copy-pasting the code from the frmMain method).
public partial class frmMain : Form
{
//creates a static copy of the form that references to this form, I believe.
public static frmMain MainForm { get; private set; }
public frmMain()
{
InitializeComponent();
// sets the static form as a copy of this form named MainForm.
frmMain.MainForm = this;
// calls the MainMenu() method in the cls_EVENTS class.
cls_EVENTS.MainMenu();
}
public void btn1_Click(object sender, EventArgs e)
{
}
//Same thing all the way down to btn6_Click
//And a couple of functions for manipulating the controls on the form...
//(enabling buttons, pushing text to the textbox, etc)
My cls_EVENTS is where I want to actually code the events. Like the MainMenu event that’ll change the text in the textbox, change the text on the button to reflect the current options, and disable the buttons I won’t need.
public class cls_EVENTS
{
/*the Main menu, solely here for 'conveinence' (so I can have a Main Menu button on my form)*/
public static void MainMenu()
{
//clears the text screen
frmMain.MainForm.ScreenTextClear();
//Enables the first button, disables the rest.
frmMain.MainForm.ButtonEnable(true, false, false, false, false, false);
//indent = true, write text to screen.
frmMain.MainForm.ScreenText(true, "Welcome to the alpha of this game.");
//test to make sure it ADDS TO the text rather then overwriting.
frmMain.MainForm.ScreenText(true, "Hi");
//changes the six button texts to the following six strings
frmMain.MainForm.ButtonText("New Game", "Load Game", "About", "---", "---", "---");
//problem area
//when player clicks button one
//start the NewGame() method
//when player clicks button two, after I eventually code such a function.
//start the LoadGame() function
//etc.
}
Public void NewGame()
{
//etc.
}
}
I’ve tried using a loop with a Sleep method in it to check a variable that each button would add a different number to (as long as intSelected was 0, the loop would continue, btn1 would add 1, etc., etc.)
But that caused the program to hang.
Plus, I feel that it would be a messy, unprofessional way of doing what I want to do. And while I may not be a professional, even I have standards.
Does anyone know of a way to pause the programs execution until a button is pressed? or have a better idea on how to get done what I want to get done? I can’t just simply code the functions into the click events, as what these buttons do would be constantly changing.
I will go ahead and say that C# is not a language I am strong in. I was originally doing this in VB.Net, but ran into the same problem, and was lead to believe that C# was a bit more flexible. The only C-language I’m even vaguely familiar with is C++, and my class in that was a least a year-and-a-half ago. So, please forgive ‘stupid’ coding, as I am a little rusty.
After some more experimenting and more Google searching, I think I may have found it.
This seems to be doing what I want to get done. The text loads up, the buttons activate, then clicking on them starts up the appropriate method. Rise-repeat.