I have a Class and a Form. This is the code I have in my form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Tamagotchi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label3.Text = "doplntamboredom";
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
label6.Text = GetMood();
}
}
}
That last part (label6.Text = GetMood()) is making error:
The name 'GetMood' does not exist in the current context.
And this is the code I have in my class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tamagotchi
{
class Mazlicek
{
private int hunger = 1;
private int boredom = 1;
private string mood;
public string GetMood()
{
return mood;
}
private void Moods(int hunger, int boredom, string strHunger, string strBoredom)
{
if ((boredom + hunger) > 15)
{
mood = "Angry!";
}
else if ((boredom + hunger) > 10 && (boredom + hunger) < 15)
{
mood = "Frustrated..";
}
else if ((boredom + hunger) > 5 && (boredom + hunger) < 11)
{
mood = "Fine.";
}
else
{
mood = "Happy!";
}
}
}
}
I have no idea why there is this problem. I'm making it with Visual Studio 2015 and it is all in one project.
You probably mean to have an instance of Mazlicek in your form. In that way you can call methods on it.
So, start by instantiating the variable mazlicek to an instance of type Mazlicek:
public partial class Form1 : Form
{
private Mazlicek mazlicek = new Mazlicek();
...
}
Now we have an instantiated object, you can can call methods on it and set properties to remember its state. You have to reference it by its variable name mazlicek:
private void button3_Click(object sender, EventArgs e)
{
label6.Text = mazlicek.GetMood();
}
Use
label6.Text = Mazlicek.GetMood();
and change it to
static class Mazlicek
and
private static string mood;
and
public static string GetMood()
{
return mood;
}
Related
Hey I got Code Written in Class Form1 And Form2. I want to call the method openkindForm() from Form2. I tried every soloution I found. I got this one at the moment which is not working it gives me a System.NullReferenceException. I do not know why it isnt working. I tried it so long but whatever I do it will not workout somehow. I would be thankfull for an answer.
Kind regards
First Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FBDP00
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
submenupanel.Visible = false;
}
private void funktionenSM_Click(object sender, EventArgs e)
{
switch (submenupanel.Visible)
{
case true:
submenupanel.Visible = false;
break;
case false:
submenupanel.Visible = true;
break;
}
}
public void neuepruefungSm_Click(object sender, EventArgs e)
{
submenupanel.Visible = false;
openkindForm(new Form2());
}
public Form activeForm = null;
public void openkindForm(Form childForm)
{
if (activeForm != null)
{
activeForm.Close();
}
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
backgroundPanel.Controls.Add(childForm);
childForm.Show();
}
}
}
Class 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static FBDP00.Form1;
namespace FBDP00
{
public partial class Form2 : Form
{
public Form1 testform;
public Form2()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void funktionenSM_Click(object sender, EventArgs e)
{
}
public void baukontrolleb_Click(object sender, EventArgs e)
{
testform.openkindForm(new Form3());
}
}
}
In Form2 you have defined a variable for Form1 (testform), but you don't set this anywhere. So this is why you get a Null reference error when you try to use it, because it is null!
So when you create your Form2 then you need to set this value. In your case, maybe in the constructor like this.
public Form1 testform;
private Form2(Form1 f1)
{
InitializeComponent();
testform = f1;
}
Then call it like this:
public void neuepruefungSm_Click(object sender, EventArgs e)
{
submenupanel.Visible = false;
openkindForm(new Form2(this));
}
However, looking at your openkindForm method, it seems to me that this really has nothing to do with Form1, and shares no variables, so should not be in this class.
You should either make this static (together with the activeForm variable), or make it a Singleton class instead. But certainly this should be a separate class.
When trying to write something to check whether a number is odd or even (C#), Visual Studio 2010 displays the error message I've mentioned above...
This is the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void chk_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(num.Text);
if (x%2 == 0)
{
chkd.Text = "Even";
}
else
{
chkd.Text = "Odd";
}
}
}
private void num_TextChanged(object sender, EventArgs e)
{
}
}
num_TextChanged is a method that is outside your class. The only things that can appear inside a namespace scope are, per the error message, a class, delegate, enum, interface or struct.
I expect you meant this to be inside the Form1 class, so you should move it:
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void chk_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(num.Text);
if (x%2 == 0)
{
chkd.Text = "Even";
}
else
{
chkd.Text = "Odd";
}
}
private void num_TextChanged(object sender, EventArgs e)
{
}
}
}
Add one more
}
which close /end namespace of form class
So I am working on a project for school that in a guessing game for a randomly generated number between 1-100. The only trouble I am having at the moment is that every time the user enters a new number the generated number also changes. I tried putting the code to generate the number in the form loader but then I can't access it later in the program. Here's what I have so far.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void guessButton_Click(object sender, EventArgs e)
{
int userGuess;
userGuess = int.Parse(guessText.Text);
Random rand = new Random();
int number = rand.Next(1, 100);
label2.Text = "" + number;
if (userGuess > number)
{
resultLabel.Text = "Your guess is too high";
}
else if (userGuess < number)
{
resultLabel.Text = "Your guess is too low.";
}
else if (userGuess == number)
{
resultLabel.Text = "That is correct!";
}
guessText.Clear();
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
As it currently stands, the Random number is overwritten every time the guessButton_Click function is run.
You are declaring Random inside the guessButton_Click function, that is called every time the guess button is clicked (that's also a memory leak!).
To fix, declare it as a global variable, in the namespace:
Edit: The below code compiles properly, and works perfectly.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
int number = new Random().Next(1, 100);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void guessButton_Click(object sender, EventArgs e)
{
int userGuess;
userGuess = int.Parse(guessText.Text);
label2.Text = "" + number;
if (userGuess > number)
{
resultLabel.Text = "Your guess is too high";
}
else if (userGuess < number)
{
resultLabel.Text = "Your guess is too low.";
}
else if (userGuess == number)
{
resultLabel.Text = "That is correct!";
}
guessText.Clear();
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
You are getting a new random number every button click try the following:
public partial class Form1 : Form
{
Random rand;
int number;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
rand = new Random();
number = rand.Next(1, 100);
}
private void guessButton_Click(object sender, EventArgs e)
{
int userGuess;
userGuess = int.Parse(guessText.Text);
label2.Text = "" + number;
if (userGuess > number)
{
resultLabel.Text = "Your guess is too high";
}
else if (userGuess < number)
{
resultLabel.Text = "Your guess is too low.";
}
else if (userGuess == number)
{
resultLabel.Text = "That is correct!";
}
guessText.Clear();
}
}
i create a simple message box to get user input and set the result into webbrowser of previous form.
this is my MsgInput source code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
public partial class MsgInput : Form
{
private readonly Main mainForm;
public string input_type;
string script;
public MsgInput()
{
this.mainForm = mainForm;
InitializeComponent();
}
private void MsgInput_Load(object sender, EventArgs e)
{
if (input_type == "echo")
{
richTextBox1.Text = "Echo : void echo ( string $arg1 [, string $... ] )";
}
}
private void btnOk_Click(object sender, EventArgs e)
{
if (input_type == "echo")
{
script = mainForm.webBrowser1.DocumentText;
if (chkNewLine.Checked == true)
{
script += "\n";
}
script += "echo " + txtInput.Text;
mainForm.webBrowser1.DocumentText = script;
this.Close();
}
}
}
and i not add anything in first form just set the webbrowser modifiers to public.
when i debug. null return when i try to submit an text
Main Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace EasyPHP
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void echoToolStripMenuItem_Click(object sender, EventArgs e)
{
var msg = new MsgInput();
msg.input_type = "echo";
msg.Show();
}
private void Main_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = "<pre>";
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
}
}
I am assuming you are getting a null reference exception. it will be useful if you post the main form code. From what i understood, you should be passing in the main form reference and that's why you are getting a null reference.
In your code, change the constructor as below (ParentForm is whatever is the class name of your parent form)
public MsgInput(ParentForm mainForm)
{
this.mainForm = mainForm;
InitializeComponent();
}
and in the main form
MsgInput frm = new MsgInput(this);
frm.input_type = "echo";
frm.Show(this);
Else please share the full code and we can quickly help
I'm essentially adding to a list; iterating those names through a listbox using a for..loop.
However, it's not showing any names once I've entered a name/age; click add person and show people; nothing displays whatsoever.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GUI_TEST_2
{
public partial class Form1 : Form
{
List<Person_List> people = new List<Person_List>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void addPerson_Click(object sender, EventArgs e)
{
string name = addName.Text;
int age = Convert.ToInt32(addAge.Text);
for (int i = 0; i < people.Count(); i++)
{
people[i].addPersonToList(name, age);
}
}
private void showPeople_Click(object sender, EventArgs e)
{
for (int i = 0; i < people.Count(); i++)
{
string name = people[i].showPeople();
peopleListBox.Items.Add("Name: " + name);
}
}
private void peopleListBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GUI_TEST_2
{
class Person_List
{
public List<Person> listOfPeople = new List<Person>();
public Person_List()
{
}
public void addPersonToList(string name, int age)
{
listOfPeople.Add(new Person(name, age));
}
public string showPeople()
{
string name = "";
for (int i = 0; i < listOfPeople.Count(); i++)
{
name = listOfPeople[i].Name;
}
return name;
}
}
}
In the addPerson_Click event you loop over your list people but this list does not contain anything then you never call addPersonToList.
Try with somehting like :
Person_List people = new Person_List();
private void addPerson_Click(object sender, EventArgs e)
{
string name = addName.Text;
int age = Convert.ToInt32(addAge.Text);
people.addPersonToList(name, age);
}
You gotta add the ppl to the listbox after adding them to the listOfPeople-list. Addin them to the listOfPeople doesn't mean that they automaticly get added to the listbox. There is no two-way binding as far as I know.
How I would implement
showPeople(){
listBox1.Items.Clear();
foreach (var ppl in listOfPeople)
{
listBox1.Items.Add(ppl)
}
}