I am trying to update "Player's health." when "Player" attacks a target. Player's health is reduced based on the target's return damage.
My Game class (where the label is):
public partial class Game : Form
{
public static Wizard wizard;
public static Assasin assasin;
public Game()
{
InitializeComponent();
}
private void Battle_Click(object sender, EventArgs e)
{
Battle battle = new Battle();
battle.Show();
}
public void Game_Load(object sender, EventArgs e)
{
NameLabel.Text = HeroMaker.className;
if (HeroMaker.wizardChoosen)
{
wizard = new Wizard(HeroMaker.className);
HealthLabel.Text = wizard.Health.ToString();
DamageLabel.Text = wizard.AttackDamage.ToString();
HealthBar.Maximum = wizard.Health;
HealthBar.Value = wizard.Health;
}
}
}
My Battle class (when attacking is happening) :
public partial class Battle : Form
{
Creature troll = CreaturesFactory.CreateCreature(CreatureType.Troll);
public Battle()
{
InitializeComponent();
}
private void AttackTroll_Click(object sender, EventArgs e)
{
Game.wizard.Health -= troll.ReturnDamage;
//TODO: Update the "HealthLabel value."
}
}
The problem is that when I attack the troll, Player's health is decreasing but its not updating on the label. Thanks in advance.
You just need to update the label:
private void AttackTroll_Click(object sender, EventArgs e)
{
Game.wizard.Health -= troll.ReturnDamage;
//TODO: Update the "HealthLabel value."
HealthLabel.Text = wizard.Health.ToString();
//any other things that need to be updated
}
You can also bind the label value like in this question.
Similarly, you can hook up an event such as OnWizardHealthChange that updates the label value whenever the HP changes. This way, you won't need to remember to add HealthLabel.Text = wizard.Health.ToString(); everywhere the health changes. There's an example of this in the question that I linked.
EDIT:
You can try looking in the code-behind where the label is created to see its access modifier (is it public?)
Or, you can try this:
Label healthLabel = (Label)Application.OpenForms["FormName"].Controls.OfType<Label>().First(x=> x.Name == "LabelName");
Note that I haven't tested it but you should be able to at least get the label using this code and then update the value there. Here is a good discussion on accessing controls (such as your label) in another form.
Related
I have labels with footballer's names inside. I want to get footballer's age after clicking on these labels. I do it by this way:
public partial class Form1 : Form
{
Footballer[] team = { /*team initialization*/};
public Form1()
{
InitializeComponent();
}
private void OnLabel_Click(object sender, EventArgs e)
{
for (int i = 0; i < team.Length; i++)
{
if (team[i].Name == this.Text)
{
MessageBox.Show(team[i].Age.ToString());
break;
}
}
}
}
But there is problem. There might be more than one player with the same name. So I want to bind each label with footballer. How can I do this?
For sake of simplicity suppose that you initialize your labels in this mode....
public Form1()
{
InitializeComponent();
int index = 0;
foreach(Label lbl in this.Controls.OfType<Label>())
{
lbl.Text = team[index].Name;
// Make the Tag property reference the Footballer instance
// used to set the label text with the footballer name
lbl.Tag = team[index];
index++;
}
At this point, when you receive the click event, you just need to retrieve the reference from the Tag property and use it directly
private void OnLabel_Click(object sender, EventArgs e)
{
// No loop needed here
Label current = sender as Label;
Footballer player = current.Tag as Footballer;
if(player != null)
MessageBox.Show(player.Age.ToString());
}
}
You should separate out your business logic from your presentation logic. The name is what you presented, but to keep each player/team unique then assign a unique ID.
This is then assigned to the label but hidden from view, so that when the label is clicked the ID is retrieved and you can then do a lookup based on this.
So with Player class like the following:
public class Player
{
public int ID { get;set; }
public string Name { get;set; }
//etc.
}
Then when assigning a Player to label use Label.Tag which is a general purpose field which you can use for anything your want. (Available on all Controls).
label1.Text = MyPlayer.Name;
label1.Tag = MyPlayer.ID;
If I was you, I would also change your Teams to be a List not an array
List<Footballer> team = new List<Footballer>() { /*team initialization*/};
Then you can look up as follows
private void OnLabel_Click(object sender, EventArgs e)
{
Label clickedLabel = (sender as Label);
int id = Convert.ToInt32(clickedLabel.Tag);
Footballer found = team.Find(x => x.Id == id);
MessageBox.Show(found.Age.ToString());
}
I am working with yFiles.Net component which I use for representing dependencies between objects in some SQL database (graph representation).
I need to create tooltip which will appear when I point cursors on some object. That tooltip not contains just text, it contains images also. I know how to make custom class which extends default Tooltip class and then to override methods for drawing custom tooltips.
What i do not know is how to show tooltip when I point on some object on graph, and how to dispose it when I move cursor from object on graph? Can someone help me with this please?
If I understand you correctly, you want to intercept the
ItemHoverInputMode.HoveredItemChanged Event.
I have not tested this nor worked with yFiles before, but according to documentation this should work:
// 'gc' is of type yWorks.yFiles.UI.GraphControl.
var ihim = new ItemHoverInputMode();
ihim.HoveredItemChanged += YourEvenHandler;
gc.InputModes.Add(ihim);
Then check the element in the event handler and display or hide the tooltip.
private CustomTooltip m_tooltip;
private MouseHoverInputMode m_mouseHoverMode;
private void SetupToolTips(GraphEditorInputMode mode)
{
m_tooltip = new CustomTooltip(m_model.TooltipImages);
ItemHoverInputMode itemHoverMode = new ItemHoverInputMode();
itemHoverMode.HoverItems = GraphItemTypes.Node | GraphItemTypes.Edge;
mode.ItemHoverInputMode = itemHoverMode;
m_mouseHoverMode = new MouseHoverInputMode(m_tooltip, textProvider);
mode.MouseHoverInputMode = m_mouseHoverMode;
mode.ItemHoverInputMode.HoveredItemChanged += new EventHandler<HoveredItemChangedEventArgs>(ToolTipEvent);
}
private void ToolTipEvent(object sender, HoveredItemChangedEventArgs e)
{
m_tooltip.Item = e.Item;
}
private void textProvider(object sender, ToolTipQueryEventArgs e)
{
if (m_tooltip.Item is INode || m_tooltip.Item is IEdge)
{
e.ToolTip = " ";
}
}
public class CustomTooltip : ToolTip
{
private void OnPopup(object sender, PopupEventArgs e)
{
}
private void OnDraw(object sender, DrawToolTipEventArgs e)
{
}
}
I've got code and i know I'm 99% of the way there. C# coding in MS VS2008.
Basically I have a form that has 4 radio buttons and a Continue button. the user clicks one of the radio buttons and clicks continue, and this all works fine.
However, I want to use the value entered by the user (i.e. if they click the first button, I want a variable equal to 1, 2nd button equals 2 and so on). I tried doing this in various points but the only place I can get it to run is in the private void btnOkClick line, which means I can use the values outside this void, which is what I really want.
I've tried playing around with setting some enums and such (commented out in the code below), but I can't quite get it. I know I must be close but my novice-ness is truly showing as I keep reading posts and can't quite grasp it.
In short, I want to be able to have other classes in my VS2008 project be able to reference whatever value the user selected in the initial form.
namespace AmortClient
{
public partial class frmLoadACTFCST : Form
{
public frmLoadACTFCST()
{
InitializeComponent();
//set the parent of the form to the container
//this.MdiParent = parent;
}
//public enum ACTFCST
//{
// ACT = 1,
// FCST = 2,
// PLAN = 3,
// FiveYearPlan2012=4
//}
//private ACTFCST _actfcst = ACTFCST.ACT;
//public ACTFCST actfcst
//{
// get { return _actfcst; }
// set { _actfcst = value; }
//}
private void frmLoadACTFCST_Load(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void btnActual_CheckedChanged(object sender, EventArgs e)
{
}
private void btnForecast_CheckedChanged(object sender, EventArgs e)
{
}
private void btnPlan_CheckedChanged(object sender, EventArgs e)
{
}
private void btn5YrPlan2012_CheckedChanged(object sender, EventArgs e)
{
}
private void btnContinue_Click(object sender, EventArgs e)
{
string ACTFCSTtext = "";
int dataTypeKey = 0;
if (btnActual.Checked)
{
ACTFCSTtext = btnActual.Text;
dataTypeKey = 1;
}
else if (btnForecast.Checked)
{
ACTFCSTtext = btnForecast.Text;
dataTypeKey = 2;
}
else if (btnPlan.Checked)
{
ACTFCSTtext = btnPlan.Text;
dataTypeKey = 3;
}
else if (btn5YrPlan2012.Checked)
{
ACTFCSTtext = btn5YrPlan2012.Text;
dataTypeKey = 4;
}
string msg = "";
msg = ACTFCSTtext + " " + dataTypeKey;
//btn5YrPlan2012
MessageBox.Show(msg);
Close();
}
}
}
Your dataTypeKey and ACTFCSTtext variables need to be declared as instance variables for your Form object if you want to access them from any other methods within your form. If you want to use them with some other form, you can pass them either as constructor arguments, or set some properties of said other form.
So you'd declare them just after the class declaration if you want them to be instance variables. They should still be private, meaning they can only be accessed from within your frmLoadACTFCST class.
public partial class frmLoadACTFCST : Form
{
private string ACTFCSTtext = "";
private int dataTypeKey = 0;
...
EDIT: if you want to access variables from one object in a different object (or static class), your options are as follows...
1) Declare your variables as public instance variables (same as shown above but public; these are known as Properties when you give them getter and setter methods). Your class that needs access to these variables would need to have a reference to the class that owns the variables.
Example:
FormA has a public property named SomeString.
FormB needs to access SomeString.
FormB needs a reference to FormA, and would access the variable as...
formAReference.SomeString
2) Pass the values of the variables as arguments to some method for the class that needs access.
Example:
FormA has a private instance variable named SomeString.
FormB needs access to SomeString.
If FormA instantiates FormB, it can pass the value of SomeString to FormB's constructor...
//From within FormA's code
FormB formB = new FormB(SomeString);
//FormB's constructor
public FormB(string someString)
{
this.someString = someString;
}
Maybe there is a smarter way to do it.
public partial class frmLoadACTFCST : Form
{
public frmLoadACTFCST()
{
InitializeComponent();
actfcst = ACTFCST.ACT;
btnActual.Tag = ACTFCST.ACT;
btnActual.Checked = true;
btnForecast.Tag = ACTFCST.FCST;
btnPlan.Tag = ACTFSCT.PLAN;
btn5YrPlan2012.Tag = ACTFCST.FiveYearPlan2012;
}
public enum ACTFCST
{
ACT = 1,
FCST = 2,
PLAN = 3,
FiveYearPlan2012=4
}
public static ACTFCST actfcst { get; private set; }
private void CheckedChanged(object sender, EventArgs e)
{
// All the buttons uses this Click-event.
actfcst = (sender as Button).Tag as ACTFCST;
}
private void btnContinue_Click(object sender, EventArgs e)
{
MessageBox.Show(actfcst.ToString());
Close();
}
}
The point is that all the buttons calls CheckedChanged when clicked.
Using a static means that others can access the value using something like this:
frmLoadACTFCST.ACTFCST value = frmLoadACTFCST.actfcst;
// Do something based on value.
I hope this helps you in yoyr quest.
If you select a control in design view, the properties window contains an item named "Modifiers". You can make the control public here.
A better way would be to create a new public property on your form that yields the value of the currently selected radio button.
Okay, I have a form called settings that I'm using to set hot key's and other stuff through out my form but the issue is once set you need to restart the app for them to take affect, my code is this:
In main class:
public void setHotkeys()
{
if (Properties.Settings.Default.F4 == true)
{
Pvt.HookedKeys.Add(Keys.F4);
MessageBox.Show("checked F4");
}
if (Properties.Settings.Default.F5 == true)
{
Pvt.HookedKeys.Add(Keys.F5);
MessageBox.Show("checked F5");
}
Pvt.KeyDown += new KeyEventHandler(ghk_startPvt);
Group.KeyDown += new KeyEventHandler(ghk_startGroup);
}
Settings form class for saving / setting the settings:
private void settingsPwn4g3_Load(object sender, EventArgs e)
{
settingsLoad();
}
public void saveFields()
{
Properties.Settings.Default.autoAttach = Autoattach.Checked;
Properties.Settings.Default.F5 = F5.Checked;
Properties.Settings.Default.F4 = F4.Checked;
Properties.Settings.Default.Save();
}
public void settingsLoad()
{
Autoattach.Checked = Properties.Settings.Default.autoAttach;
F5.Checked = Properties.Settings.Default.F5;
F4.Checked = Properties.Settings.Default.F4;
}
private void button1_Click(object sender, EventArgs e)
{
pwn4g3 Pwn4g3 = new pwn4g3();
saveFields();
Pwn4g3.setHotkeys();
}
Not to mention this way of doing it is very annoying for over 200 settings this can take days. Any other ideas? I would like to use a variable for set hotkey's then have something where they can press the hot key and set it, rather then 200 check box's for every key on the keyboard.
I'm completely new to GUI programming and need a little help with a list of pictureboxes.
The idea is that I have a list of pictureboxes. When a user clicks on one I want to (for example) change the BorderStyle property of the one selected to be Fixed3D, but change the remaining collection borders to FixedSingle (or something like that). What's the proper way to do something like this? I guess the bigger picture is how do I get a method of one class to call a method of another without having any information about it?
class myPicture
{
private int _pictureNumber;
private PictureBox _box;
public myPicture(int order)
{
_box = new List<PictureBox>();
_box.Click += new System.EventHandler(box_click);
_pictureNumber = order;
}
public void setBorderStyle(BorderStyle bs)
{
_box.BorderStyle = bs;
}
public void box_click(object sender, EventArgs e)
{
//here I'd like to call the set_borders from myPicturesContainer, but I don't know or have any knowledge of the instantiation
}
}
class myPicturesContainer
{
private List<myPicture> _myPictures;
//constructor and other code omitted, not really needed...
public void set_borders(int i)
{
foreach(myPicture mp in _MyPictures)
mp.setBorderStyle(BorderStyle.FixedSingle);
if(i>0 && _MyPictures.Count>=i)
_MyPictures[i].setBorderStyle(BorderStyle.Fixed3d);
}
}
You will need to create a Clicked event in your myPicture class and raise that event when it is clicked. Then you will need to attach to this event in your myPicturesContainer for each instance of myPicture that you have.
Here is a very simple example of what I mean:
class myPicture
{
public event Action<Int32> Clicked = delegate { };
private int _pictureNumber;
public void box_click(object sender, EventArgs e)
{
this.Clicked(this._pictureNumber);
}
}
class myPicturesContainer
{
private List<myPicture> _myPictures;
public void set_borders(int i)
{
foreach (myPicture mp in _myPictures)
{
mp.Clicked += pictureClick;
}
}
void pictureClick(Int32 pictureId)
{
// This method will be called and the pictureId
// of the clicked picture will be passed in
}
}