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
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.
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;
}
My problem is simple. I want to click a panel in Form1 that will cause label1 in a userControl1, which will be placed upon form2 to change to "Text".
Clicking this panel would also change the background color of said userControl1. I receive the error "'TileInterFaceTest.Usercontrol1.label1' due to its protection level" which frankly baffles me.
Even running the color change code separately it simply doesn't achieve the desired result.
To be clear, I'm quite a novice when it comes to C# and programming. I've been working with Visual Basic until now so the concept of classes, methods and objects are slightly confusing to me.
Here is my code for Form1:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
UserControl1 userControl1 = new UserControl1();
form2.Show();
userControl1.BackColor = System.Drawing.Color.Red;
userControl1.LabelText = "Text";
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Code for UserControl1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class UserControl1 : UserControl
{
public String LabelText
{
get
{
return label1.Text;
}
set
{
label1.Text = value;
}
}
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Load(object sender, EventArgs e)
{
}
}
}
label1 is a private field, which means you cannot access it outside of the class UserControl1.
What you could do is add a public property in the definition of the class UserControl1:
public String LabelText {
get {
return label1.Text;
}
set {
label1.Text = value;
}
}
Then use this property to modify the value of the Text of label1:
private void panel1_Click(object sender, EventArgs e)
{
form2.Show();
userControl1.BackColor = System.Drawing.Color.Red;
userControl1.LabelText = "Text";
}
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
So I have this class called Common.cs in that class I have a function that creates an instance of a form, frmMainGame.cs, which then calls a method that should invoke this new form instance, but for some reason, it does not. So when the form is shown using mainGame.Show(); the form just hangs there and I can't do anything with it.
Here's the code:
frmMainGame.cs
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 System.Net.Sockets;
using System.Threading;
namespace Ghoul_Engine
{
public partial class frmMainGame : Form
{
public delegate void openGame();
public delegate void refreshGame();
public delegate void updateScreen(Bitmap image);
drawing draw;
public bool isTyping = false;
public Graphics g;
public List<String> Chat = new List<String>();
public List<String> chat
{
get { return Chat; }
}
public frmMainGame()
{
draw = new drawing(this);
InitializeComponent();
}
public Size size{
get { return this.Size; }
}
public void showGame()
{
if (InvokeRequired)
{
MessageBox.Show("Invoke");
BeginInvoke(new openGame(showGame), null);
}
else
{
this.Show();
this.Focus();
}
}
public void refresh()
{
if (this.InvokeRequired)
{
this.Invoke(new refreshGame(refresh), null);
}
else
{
this.Refresh();
}
}
private void frmMainGame_Load(object sender, EventArgs e)
{
this.DoubleBuffered = true;
draw.drawText(new string[] { "GHOUL Game Engine Alpha V0.0.1" }, 12, 0, 0, Brushes.Yellow);
clientNetHandler handlers = new clientNetHandler();
}
public void update(Bitmap image)
{
if (InvokeRequired)
{
this.Invoke(new updateScreen(update), new object[] { image });
}
else
{
pbScreen.Image = image;
}
}
private void frmMainGame_Close(object sender, FormClosingEventArgs e)
{
Program.Network.Send("logout", Program.netHandlers.getDataByte(clientNetHandler.cNetType.Logout));
//Program.Network.closeSocket();
Program.mainMenu.showMainMenu();
Program.mainGame = new frmMainGame();
}
private void tmrRefreshScreen_Tick(object sender, EventArgs e)
{
}
private void frmMainGame_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void pbScreen_Click(object sender, EventArgs e)
{
draw.drawText(Chat.ToArray(), 12, 0, 30, Brushes.Green);
}
}
}
Common.Cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Drawing;
namespace Ghoul_Engine
{
class Common
{
public Common()
{
}
public void innnitiateGame(String login)
{
Program.mainMenu.hideMainMenu();
frmMainGame mainGame = new frmMainGame();
mainGame.showGame();
}
public void preExit()
{
Program.Network.closeSocket();
}
}
}
You need a message pump, which is created by passing a form instance to Application.Run():
Application.Run(new frmMainGame());
You probably need to redesign your classes structure a little bit. Instead of creating your main form's instance in Common.cs, I suggest you go ahead with the normal way things flow, i.e. create your main form's instance in program.cs using Application.Run() so that it installs a message pump for you, keep your socket communication classes in a separate file (probably like just they are) and then instantiate them within your main form. If you must have these communication classes available across multiple forms, then go on creating their instance(s) in program.cs and pass them to different forms/other modules as parameters.