changing textlabel in form1 using different class - c#

having a bit of a problem changing the label text when a button is pressed. my code looks like this. i searched a bit in this forum and tried this solution in form1 i have this code.
public string info
{
get
{
return label11.Text;
}
set
{
label11.Text = value;
}
}
in class search i have this
public void fighting()
{
character tom = new character();
Form1 f = new Form1();
Random explore = new Random();
int exploration = explore.Next(0, 3);
if (character.location == "Forest" || character.location == "Dungeon")
{
switch (exploration)
{
case 0:
f.info = "You didnt find anything";
f.Refresh();
f.herodamage = exploration.ToString();
break;
...
the button has this
public void button8_Click(object sender, EventArgs e)
{
Search find = new Search();
find.fighting();
}
what am i doing wrong? everytime i press the button the text wont change but it works because it does change a buttons text.

Fighting method is creating a new instance of Form1, different from your UI.
While it is updating label1.Text, it refers to different Form1.
My suggestion is for refactoring of your code to make responsibilities more clear.
Something like
var info = find.fighting(); // Where fighting returns string, instead of creating new Form1 and setting value, just return the value.
this.info = info;
If you still insist to update UI from search method, which is not correct way to approach this problem, you can pass around Form1 from the parameter.
Search find = new Search(this); // Store it as instance of Search class.
find.fighting(); // fighting should never create new instance of Form1 (reason of problem you are facing)

Instead of creating a new instance of Form1 you need to get a reference to the already existing instance of this form.
One simple way to do this is to use the Application.OpenForms property:
character tom = new character();
Form1 f = Application.OpenForms.OfType<Form1>().FirstOrDefault();
Random explore = new Random();
...

Related

C# Using function from another file

Ok, so, I have functions.cs file where i stored following function
public static void Global_Reset()
{
Form1 blok = new Form1();
blok.userControl1.Visible = false;
blok.userControl2.Visible = false;
blok.userControl3.Visible = false;
if (Properties.Settings.Default.client == 1)
{
blok.userControl1.Visible = true;
MessageBox.Show("First");
}
else if (Properties.Settings.Default.client == 2)
{
blok.userControl2.Visible = true;
MessageBox.Show("Second");
}
else if (Properties.Settings.Default.client == 3)
{
blok.userControl2.Visible = true;
MessageBox.Show("Third");
}
else
{
MessageBox.Show("Error");
}
}
When I call fun.Global_Reset() in Form1 or in any user control that is used in Form1, visibility never changes but I got messages (First, Second, Third). Is there any way to solve this?
I tried to use this directly in Form1 and it used to work, but when I call it from userControl that was used in Form1 it's not working again.
This is the form you're modifying:
Form1 blok = new Form1();
It only exists during the scope of that method (function), and it is never shown to the user. It's not the form instance you're seeing on the screen.
As an analogy... Imagine the form you're seeing is a car. What you're doing is getting another car that looks just like it and putting something in the trunk of that car, then noticing that what you put in the trunk of the second car can't be found in the trunk of the first car.
Instead of creating a new form instance, pass the existing instance to this method:
public static void Global_Reset(Form1 blok)
{
// don't create an instance here, just use the blok that was passed
}
Then pass that instance from your form, for example:
fun.Global_Reset(this);

Accessing a Variable inForm1 from another class

I have been searching for some time now and can't really find a good solution to my problem.
I got a class (lets call it TaskbarNotifier) and my Form1
In TaskbarNotifier I got this:
private Form1 form = new Form1();
And later in that class I got this:
this.form.NotifiersCount = this.form.NotifiersCount - 1;
switch (notifier)
{
case 0:
this.form.NewsShown = false;
break;
case 1:
this.form.ThreadsSown = false;
break;
}
now my problem is that when I do this I get a StackOverflow error at:
private Form1 form = new Form1();
Id there another better way to access variables in Form1 from another class?
Many thanks for reading :)
----EDIT----
Okay so in Form1 constructor I got this:
private ContextMenuStrip contextMenu = new ContextMenuStrip();
private ToolStripMenuItem mnuItemExit = new ToolStripMenuItem();
private ToolStripMenuItem mnuItemShow = new ToolStripMenuItem();
Which is where the stackoverflow actually starts.
But by removing:
private Form1 form = new Form1();
from TaskbarNotifier it don't throw any errors.
Clearly, you are creating a TaskbarNotifier somewhere in the construction process of Form1.
That could happen if:
You explicitly wrote var test = new TaskbarNotifier() in the constructor
You dropped a control of that type onto the designer
In either case, Form1 will construct a TaskbarNotifier which then constructs a Form1 and, well, this happens a whole bunch of times and you get a Stack Overflow.
Make sure that the TaskbarNotifier is not created as part of Form1 creation and the error will go away.

C# Take combobox item from one form and add its name as text to another

Ok so I'm attempting to create a simple game. In a nutshell it's a resource management game where the player will attempt to manage a thieves guild. In regards to running missions I've created a Thief class, a new instance of which is created when a new thief is recruited. I have coded within the thief class the ability to gain experience and level up.
Here's my specific problem:
I want the player to be able to select which thief/thieves to send on a mission. I have thought about it and figured that opening a new form and populating it with checkboxes is the easiest way to allow this. These checkboxes will be related to a List<thief> of thieves, the player then checks the thieves s/he wants to send and these are then stored in another List<thief> and passed on to the run mission function.
I've built a separate project with the intention of testing and playing around with this before putting it into the main program. The test project consists of two forms: The first (frmMain) with a textbox to hold the selected options and a button to open the second form (frmSelect). Currently I can open and populate the second form (frmSelect) but when I try to add the checked options to the textbox I simply...well can't.
So far I have tried directly accessing the textbox by typing frmMain.txtOptionsDisplay in the cs file of frmSelect but it causes the following error:
An object reference is required for the non-static field, method or
property
I tried to create a new form in frmSelect and make it equal to the active instance of frmMain with: Form frmTemp = frmMain.ActiveForm; and then alter the textbox using frmTemp as a go-between but that produced the error:
'System.Windows.Forms.Form' does not contain a definition for
'txtOptionsDisplay'.
Having searched both google and stackoverflow forums I've encountered answers that I either have never heard of (Threading) or answers that I kind've recognise but can't interpret the code pasted to make it relevant to my problem (delegates).
Any advice or pointers would be fantastic.
EDIT:
frmMain code:
public frmMain()
{
InitializeComponent();
selections.Add("Option 1");
selections.Add("Option 2");
}
private void btnClick_Click(object sender, EventArgs e)
{
frmSelectOptions.Show();
int length = selections.Count();
for (int i = 0; i < length; i++)
{
CheckBox box = new CheckBox();
box.Text = selections[i];
box.AutoSize = true;
box.Location = new Point(50, 50*(i+1));
frmSelectOptions.grpControls.Controls.Add(box);
}
}
public void updateText(string option)
{
txtOptionsDisplay.Text += option;
}
}
frmSelect code:
public List<CheckBox> selectedOptions = new List<CheckBox>();
Form frmTemp = frmMain.ActiveForm;
public frmSelect()
{
InitializeComponent();
}
private void btnSelect_Click(object sender, EventArgs e)
{
foreach (CheckBox box in grpControls.Controls)
{
if (box.Checked == true)
selectedOptions.Add(box);
}
this.Hide();
}
}
I hope this formats correctly... I'm kinda new and don't know how to indent. Oh look there's a preview...
Does this help?
Your problem is that controls defined within a form by default receive the private access identifier. Hence you could just add a property along the lines of
public ControlType ProxyProperty {
get {
return txtOptionsDisplay;
}
}
Besides from that you should think about wether what you're trying is actually a good solution. Manipulating forms from one to another will become a huge clusterfuck in terms of maintenance later on.
I'd suggest using the Singleton pattern for your frmMain. This will help safeguard you from accidentally launching another instance of frmMain and at the same time, will give you access to frmMain's objects. From there, you can either write accessors to Get your txtOptionsDisplay or you can make it public. Below is an example:
public class frmMain
{
private static frmMain Instance = null;
private static object LockObj = new object();
public static frmMain GetMain()
{
// Thread-safe singleton
lock(LockObj)
{
if(Instance == null)
Instance = new frmMain();
return Instance;
}
}
public string GetOptionsDisplayText()
{
return txtOptionsDisplay.Text;
}
}
public class frmSelect
{
private void frmSelect_Load(object sender, EventArgs e)
{
// Set whatever text you want to frmMain's txtOptionsDisplay text
txtDisplay.Text = frmMain.GetMain().GetOptionsDisplayText();
}
}
If you do go this route, don't forget to update Program.cs to use frmMain's singleton.
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new frmMain()); - Old method
Application.Run(frmMain.GetMain());
}

Passing string between two forms

I have a problem in passing a string value between two forms.
First I created public string sanacode which I assign the value passed in form 2
Form 1 code
AnalysisEdit ae = new AnalysisEdit();
int Row = dataGridView1.CurrentRow.Index;
ae.sanacode = dataGridView1[0, Row].Value.ToString();
ae.Show();
Form 2 constructor code
public AnalysisEdit()
{
InitializeComponent();
MessageBox.Show(sanacode,);
}
it shows me nothing
Change you constructor from
public AnalysisEdit()
{
InitializeComponent();
MessageBox.Show(sanacode);
}
to
public AnalysisEdit(string sanacode)
{
InitializeComponent();
MessageBox.Show(sanacode);
}
form call
int Row = dataGridView1.CurrentRow.Index;
AnalysisEdit ae = new AnalysisEdit(dataGridView1[0, Row].Value.ToString());
ae.Show();
The issue is that you're not calling things in the correct order. The form 2 constructor code will be called on line 1 of the form code, or AnalysisEdit ae = new AnalysisEdit(); However, this is before the assignment that takes place on line 3: ae.sanacode = dataGridView1[0, Row].Value.ToString(); So when you show the messagebox in the form 2 constructor, sanacode has not yet been assigned to.
There are two ways to fix this. Firstly, you can pass the value in via the constructor as per #kostas ch.'s answer, or you can override the form's OnShown event in form 2:
protected override void OnShown(EventArgs e)
{
MessageBox.Show(sanacode);
}
I wouldn't put your code
MessageBox.Show(sanacode,);
in the constructor. I would use the "Load"-Event.
If you use the "Load"-Event your MessageBox will Show, when you use
ae.Show();
Like this
private void AnalysisEdit_Load(object sender, EventArgs e)
{
MessageBox.Show(sanacode);
}
Perhaps you'd like some ideas for the various ways to pass data between forms. I have written two blog posts on the subject:
http://geek-goddess-bonnie.blogspot.com/2011/01/passing-data-between-forms.html
http://geek-goddess-bonnie.blogspot.com/2012/12/passing-data-between-forms-redux_31.html
You could temporarily add MessageBox.Show() to your setter:
// In Form2
public sanacode
{
set
{
_sanacode = value;
MessageBox.Show(_sanacode);
}
get
{
return _sanacode;
}
}

passing values between forms (winforms)

Wierd behaviour when passing values to and from second form.
ParameterForm pf = new ParameterForm(testString);
works
ParameterForm pf = new ParameterForm();
pf.testString="test";
doesn't (testString defined as public string)
maybe i'm missing something? Anyway I'd like to make 2nd variant work properly, as for now - it returns null object reference error.
Thanks for help.
Posting more code here:
calling
Button ParametersButton = new Button();
ParametersButton.Click += delegate
{
ParameterForm pf = new ParameterForm(doc.GetElementById(ParametersButton.Tag.ToString()));
pf.ShowDialog(this);
pf.test = "test";
pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
};
definition and use
public partial class ParameterForm : Form
{
public string test;
public XmlElement node;
public delegate void ParameterSubmitResult(object sender, XmlElement e);
public event ParameterSubmitResult Submit;
public void SubmitButton_Click(object sender, EventArgs e)
{
Submit(this,this.node);
Debug.WriteLine(test);
}
}
result:
Submit - null object reference
test - null object reference
pf.ShowDialog(this); is a blocking call, so pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit); is never reached: switch the order.
Submit(this,this.node); throws a null object reference because no event is assigned to it (see above). Generally, you should always check first: if (Submit != null) Submit(this,this.node);
You should change ``pf.ShowDialog(this);topf.Show(this);` so that your main form isn't disabled while your dialog box is open, if that's what you want, or use the model below (typical for dialog boxes.)
I'm not sure what pf_Submit is supposed to do, so this might not be the best way to go about it in your application, but it's how general "Proceed? Yes/No" questions work.
Button ParametersButton = new Button();
ParametersButton.Click += delegate
{
ParameterForm pf = new ParameterForm(testString);
pf.ShowDialog(this); // Blocks until user submits
// Do whatever pf_Submit did here.
};
public partial class ParameterForm : Form
{
public string test; // Generally, encapsulate these
public XmlElement node; // in properties
public void SubmitButton_Click(object sender, EventArgs e)
{
Debug.WriteLine(test);
this.Close(); // Returns from ShowDialog()
}
}
When you want to use your second variant, you have to use a getString()-Method, where you can put the e.g. "testString". The way you wrote it, "testString" should be a method (and got brackets).
EDIT (a bit more precise):
You could write:
pf.getString(testString);
, if "pf" is an instance of your own class, otherwise you had to look up, whether you can retrieve a String in this class.
the thing was in line order :)
pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
and
pf.Test = "test";
should have been set before
pf.ShowDialog(this);
my mistake thingking that parameter can be passed after 2nd form was displayed
thnx for answers

Categories