-
-
Save ScottLilly/d9a7a94b0e13d81117b6 to your computer and use it in GitHub Desktop.
Lesson 21.3 - Add a button and create its eventhandler in code, without the UI design screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.IO; | |
using Engine; | |
namespace SuperAdventure | |
{ | |
public partial class SuperAdventure : Form | |
{ | |
private const string PLAYER_DATA_FILE_NAME = "PlayerData.xml"; | |
private Player _player; | |
public SuperAdventure() | |
{ | |
InitializeComponent(); | |
if(File.Exists(PLAYER_DATA_FILE_NAME)) | |
{ | |
_player = Player.CreatePlayerFromXmlString(File.ReadAllText(PLAYER_DATA_FILE_NAME)); | |
} | |
else | |
{ | |
_player = Player.CreateDefaultPlayer(); | |
} | |
lblHitPoints.DataBindings.Add("Text", _player, "CurrentHitPoints"); | |
lblGold.DataBindings.Add("Text", _player, "Gold"); | |
lblExperience.DataBindings.Add("Text", _player, "ExperiencePoints"); | |
lblLevel.DataBindings.Add("Text", _player, "Level"); | |
dgvInventory.RowHeadersVisible = false; | |
dgvInventory.AutoGenerateColumns = false; | |
dgvInventory.DataSource = _player.Inventory; | |
dgvInventory.Columns.Add(new DataGridViewTextBoxColumn | |
{ | |
HeaderText = "Name", | |
Width = 197, | |
DataPropertyName = "Description" | |
}); | |
dgvInventory.Columns.Add(new DataGridViewTextBoxColumn | |
{ | |
HeaderText = "Quantity", | |
DataPropertyName = "Quantity" | |
}); | |
dgvQuests.RowHeadersVisible = false; | |
dgvQuests.AutoGenerateColumns = false; | |
dgvQuests.DataSource = _player.Quests; | |
dgvQuests.Columns.Add(new DataGridViewTextBoxColumn | |
{ | |
HeaderText = "Name", | |
Width = 197, | |
DataPropertyName = "Name" | |
}); | |
dgvQuests.Columns.Add(new DataGridViewTextBoxColumn | |
{ | |
HeaderText = "Done?", | |
DataPropertyName = "IsCompleted" | |
}); | |
cboWeapons.DataSource = _player.Weapons; | |
cboWeapons.DisplayMember = "Name"; | |
cboWeapons.ValueMember = "Id"; | |
if(_player.CurrentWeapon != null) | |
{ | |
cboWeapons.SelectedItem = _player.CurrentWeapon; | |
} | |
cboWeapons.SelectedIndexChanged += cboWeapons_SelectedIndexChanged; | |
cboPotions.DataSource = _player.Potions; | |
cboPotions.DisplayMember = "Name"; | |
cboPotions.ValueMember = "Id"; | |
_player.PropertyChanged += PlayerOnPropertyChanged; | |
_player.OnMessage += DisplayMessage; | |
_player.MoveTo(_player.CurrentLocation); | |
} | |
private void DisplayMessage(object sender, MessageEventArgs messageEventArgs) | |
{ | |
rtbMessages.Text += messageEventArgs.Message + Environment.NewLine; | |
if(messageEventArgs.AddExtraNewLine) | |
{ | |
rtbMessages.Text += Environment.NewLine; | |
} | |
rtbMessages.SelectionStart = rtbMessages.Text.Length; | |
rtbMessages.ScrollToCaret(); | |
} | |
private void PlayerOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) | |
{ | |
if(propertyChangedEventArgs.PropertyName == "Weapons") | |
{ | |
cboWeapons.DataSource = _player.Weapons; | |
if(!_player.Weapons.Any()) | |
{ | |
cboWeapons.Visible = false; | |
btnUseWeapon.Visible = false; | |
} | |
} | |
if(propertyChangedEventArgs.PropertyName == "Potions") | |
{ | |
cboPotions.DataSource = _player.Potions; | |
if(!_player.Potions.Any()) | |
{ | |
cboPotions.Visible = false; | |
btnUsePotion.Visible = false; | |
} | |
} | |
if(propertyChangedEventArgs.PropertyName == "CurrentLocation") | |
{ | |
// Show/hide available movement buttons | |
btnNorth.Visible = (_player.CurrentLocation.LocationToNorth != null); | |
btnEast.Visible = (_player.CurrentLocation.LocationToEast != null); | |
btnSouth.Visible = (_player.CurrentLocation.LocationToSouth != null); | |
btnWest.Visible = (_player.CurrentLocation.LocationToWest != null); | |
// Display current location name and description | |
rtbLocation.Text = _player.CurrentLocation.Name + Environment.NewLine; | |
rtbLocation.Text += _player.CurrentLocation.Description + Environment.NewLine; | |
if(_player.CurrentLocation.MonsterLivingHere == null) | |
{ | |
cboWeapons.Visible = false; | |
cboPotions.Visible = false; | |
btnUseWeapon.Visible = false; | |
btnUsePotion.Visible = false; | |
} | |
else | |
{ | |
cboWeapons.Visible = _player.Weapons.Any(); | |
cboPotions.Visible = _player.Potions.Any(); | |
btnUseWeapon.Visible = _player.Weapons.Any(); | |
btnUsePotion.Visible = _player.Potions.Any(); | |
} | |
} | |
} | |
private void btnNorth_Click(object sender, EventArgs e) | |
{ | |
_player.MoveNorth(); | |
} | |
private void btnEast_Click(object sender, EventArgs e) | |
{ | |
_player.MoveEast(); | |
} | |
private void btnSouth_Click(object sender, EventArgs e) | |
{ | |
_player.MoveSouth(); | |
} | |
private void btnWest_Click(object sender, EventArgs e) | |
{ | |
_player.MoveWest(); | |
} | |
private void btnUseWeapon_Click(object sender, EventArgs e) | |
{ | |
// Get the currently selected weapon from the cboWeapons ComboBox | |
Weapon currentWeapon = (Weapon)cboWeapons.SelectedItem; | |
_player.UseWeapon(currentWeapon); | |
} | |
private void btnUsePotion_Click(object sender, EventArgs e) | |
{ | |
// Get the currently selected potion from the combobox | |
HealingPotion potion = (HealingPotion)cboPotions.SelectedItem; | |
_player.UsePotion(potion); | |
} | |
private void SuperAdventure_FormClosing(object sender, FormClosingEventArgs e) | |
{ | |
File.WriteAllText(PLAYER_DATA_FILE_NAME, _player.ToXmlString()); | |
} | |
private void cboWeapons_SelectedIndexChanged(object sender, EventArgs e) | |
{ | |
_player.CurrentWeapon = (Weapon)cboWeapons.SelectedItem; | |
} | |
private void btnTrade_Click(object sender, EventArgs e) | |
{ | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace SuperAdventure | |
{ | |
partial class SuperAdventure | |
{ | |
/// <summary> | |
/// Required designer variable. | |
/// </summary> | |
private System.ComponentModel.IContainer components = null; | |
/// <summary> | |
/// Clean up any resources being used. | |
/// </summary> | |
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> | |
protected override void Dispose(bool disposing) | |
{ | |
if(disposing && (components != null)) | |
{ | |
components.Dispose(); | |
} | |
base.Dispose(disposing); | |
} | |
#region Windows Form Designer generated code | |
/// <summary> | |
/// Required method for Designer support - do not modify | |
/// the contents of this method with the code editor. | |
/// </summary> | |
private void InitializeComponent() | |
{ | |
this.label1 = new System.Windows.Forms.Label(); | |
this.label2 = new System.Windows.Forms.Label(); | |
this.label3 = new System.Windows.Forms.Label(); | |
this.label4 = new System.Windows.Forms.Label(); | |
this.lblHitPoints = new System.Windows.Forms.Label(); | |
this.lblGold = new System.Windows.Forms.Label(); | |
this.lblExperience = new System.Windows.Forms.Label(); | |
this.lblLevel = new System.Windows.Forms.Label(); | |
this.label5 = new System.Windows.Forms.Label(); | |
this.cboWeapons = new System.Windows.Forms.ComboBox(); | |
this.cboPotions = new System.Windows.Forms.ComboBox(); | |
this.btnUseWeapon = new System.Windows.Forms.Button(); | |
this.btnUsePotion = new System.Windows.Forms.Button(); | |
this.btnNorth = new System.Windows.Forms.Button(); | |
this.btnEast = new System.Windows.Forms.Button(); | |
this.btnSouth = new System.Windows.Forms.Button(); | |
this.btnWest = new System.Windows.Forms.Button(); | |
this.rtbLocation = new System.Windows.Forms.RichTextBox(); | |
this.rtbMessages = new System.Windows.Forms.RichTextBox(); | |
this.dgvInventory = new System.Windows.Forms.DataGridView(); | |
this.dgvQuests = new System.Windows.Forms.DataGridView(); | |
this.btnTrade = new System.Windows.Forms.Button(); | |
((System.ComponentModel.ISupportInitialize)(this.dgvInventory)).BeginInit(); | |
((System.ComponentModel.ISupportInitialize)(this.dgvQuests)).BeginInit(); | |
this.SuspendLayout(); | |
// | |
// label1 | |
// | |
this.label1.AutoSize = true; | |
this.label1.Location = new System.Drawing.Point(18, 20); | |
this.label1.Name = "label1"; | |
this.label1.Size = new System.Drawing.Size(55, 13); | |
this.label1.TabIndex = 0; | |
this.label1.Text = "Hit Points:"; | |
// | |
// label2 | |
// | |
this.label2.AutoSize = true; | |
this.label2.Location = new System.Drawing.Point(18, 46); | |
this.label2.Name = "label2"; | |
this.label2.Size = new System.Drawing.Size(32, 13); | |
this.label2.TabIndex = 1; | |
this.label2.Text = "Gold:"; | |
// | |
// label3 | |
// | |
this.label3.AutoSize = true; | |
this.label3.Location = new System.Drawing.Point(18, 74); | |
this.label3.Name = "label3"; | |
this.label3.Size = new System.Drawing.Size(63, 13); | |
this.label3.TabIndex = 2; | |
this.label3.Text = "Experience:"; | |
// | |
// label4 | |
// | |
this.label4.AutoSize = true; | |
this.label4.Location = new System.Drawing.Point(18, 100); | |
this.label4.Name = "label4"; | |
this.label4.Size = new System.Drawing.Size(36, 13); | |
this.label4.TabIndex = 3; | |
this.label4.Text = "Level:"; | |
// | |
// lblHitPoints | |
// | |
this.lblHitPoints.AutoSize = true; | |
this.lblHitPoints.Location = new System.Drawing.Point(110, 19); | |
this.lblHitPoints.Name = "lblHitPoints"; | |
this.lblHitPoints.Size = new System.Drawing.Size(0, 13); | |
this.lblHitPoints.TabIndex = 4; | |
// | |
// lblGold | |
// | |
this.lblGold.AutoSize = true; | |
this.lblGold.Location = new System.Drawing.Point(110, 45); | |
this.lblGold.Name = "lblGold"; | |
this.lblGold.Size = new System.Drawing.Size(0, 13); | |
this.lblGold.TabIndex = 5; | |
// | |
// lblExperience | |
// | |
this.lblExperience.AutoSize = true; | |
this.lblExperience.Location = new System.Drawing.Point(110, 73); | |
this.lblExperience.Name = "lblExperience"; | |
this.lblExperience.Size = new System.Drawing.Size(0, 13); | |
this.lblExperience.TabIndex = 6; | |
// | |
// lblLevel | |
// | |
this.lblLevel.AutoSize = true; | |
this.lblLevel.Location = new System.Drawing.Point(110, 99); | |
this.lblLevel.Name = "lblLevel"; | |
this.lblLevel.Size = new System.Drawing.Size(0, 13); | |
this.lblLevel.TabIndex = 7; | |
// | |
// label5 | |
// | |
this.label5.AutoSize = true; | |
this.label5.Location = new System.Drawing.Point(617, 531); | |
this.label5.Name = "label5"; | |
this.label5.Size = new System.Drawing.Size(69, 13); | |
this.label5.TabIndex = 8; | |
this.label5.Text = "Select action"; | |
// | |
// cboWeapons | |
// | |
this.cboWeapons.FormattingEnabled = true; | |
this.cboWeapons.Location = new System.Drawing.Point(369, 559); | |
this.cboWeapons.Name = "cboWeapons"; | |
this.cboWeapons.Size = new System.Drawing.Size(121, 21); | |
this.cboWeapons.TabIndex = 9; | |
// | |
// cboPotions | |
// | |
this.cboPotions.FormattingEnabled = true; | |
this.cboPotions.Location = new System.Drawing.Point(369, 593); | |
this.cboPotions.Name = "cboPotions"; | |
this.cboPotions.Size = new System.Drawing.Size(121, 21); | |
this.cboPotions.TabIndex = 10; | |
// | |
// btnUseWeapon | |
// | |
this.btnUseWeapon.Location = new System.Drawing.Point(620, 559); | |
this.btnUseWeapon.Name = "btnUseWeapon"; | |
this.btnUseWeapon.Size = new System.Drawing.Size(75, 23); | |
this.btnUseWeapon.TabIndex = 11; | |
this.btnUseWeapon.Text = "Use"; | |
this.btnUseWeapon.UseVisualStyleBackColor = true; | |
this.btnUseWeapon.Click += new System.EventHandler(this.btnUseWeapon_Click); | |
// | |
// btnUsePotion | |
// | |
this.btnUsePotion.Location = new System.Drawing.Point(620, 593); | |
this.btnUsePotion.Name = "btnUsePotion"; | |
this.btnUsePotion.Size = new System.Drawing.Size(75, 23); | |
this.btnUsePotion.TabIndex = 12; | |
this.btnUsePotion.Text = "Use"; | |
this.btnUsePotion.UseVisualStyleBackColor = true; | |
this.btnUsePotion.Click += new System.EventHandler(this.btnUsePotion_Click); | |
// | |
// btnNorth | |
// | |
this.btnNorth.Location = new System.Drawing.Point(493, 433); | |
this.btnNorth.Name = "btnNorth"; | |
this.btnNorth.Size = new System.Drawing.Size(75, 23); | |
this.btnNorth.TabIndex = 13; | |
this.btnNorth.Text = "North"; | |
this.btnNorth.UseVisualStyleBackColor = true; | |
this.btnNorth.Click += new System.EventHandler(this.btnNorth_Click); | |
// | |
// btnEast | |
// | |
this.btnEast.Location = new System.Drawing.Point(573, 457); | |
this.btnEast.Name = "btnEast"; | |
this.btnEast.Size = new System.Drawing.Size(75, 23); | |
this.btnEast.TabIndex = 14; | |
this.btnEast.Text = "East"; | |
this.btnEast.UseVisualStyleBackColor = true; | |
this.btnEast.Click += new System.EventHandler(this.btnEast_Click); | |
// | |
// btnSouth | |
// | |
this.btnSouth.Location = new System.Drawing.Point(493, 487); | |
this.btnSouth.Name = "btnSouth"; | |
this.btnSouth.Size = new System.Drawing.Size(75, 23); | |
this.btnSouth.TabIndex = 15; | |
this.btnSouth.Text = "South"; | |
this.btnSouth.UseVisualStyleBackColor = true; | |
this.btnSouth.Click += new System.EventHandler(this.btnSouth_Click); | |
// | |
// btnWest | |
// | |
this.btnWest.Location = new System.Drawing.Point(412, 457); | |
this.btnWest.Name = "btnWest"; | |
this.btnWest.Size = new System.Drawing.Size(75, 23); | |
this.btnWest.TabIndex = 16; | |
this.btnWest.Text = "West"; | |
this.btnWest.UseVisualStyleBackColor = true; | |
this.btnWest.Click += new System.EventHandler(this.btnWest_Click); | |
// | |
// rtbLocation | |
// | |
this.rtbLocation.Location = new System.Drawing.Point(347, 19); | |
this.rtbLocation.Name = "rtbLocation"; | |
this.rtbLocation.ReadOnly = true; | |
this.rtbLocation.Size = new System.Drawing.Size(360, 105); | |
this.rtbLocation.TabIndex = 17; | |
this.rtbLocation.Text = ""; | |
// | |
// rtbMessages | |
// | |
this.rtbMessages.Location = new System.Drawing.Point(347, 130); | |
this.rtbMessages.Name = "rtbMessages"; | |
this.rtbMessages.ReadOnly = true; | |
this.rtbMessages.Size = new System.Drawing.Size(360, 286); | |
this.rtbMessages.TabIndex = 18; | |
this.rtbMessages.Text = ""; | |
// | |
// dgvInventory | |
// | |
this.dgvInventory.AllowUserToAddRows = false; | |
this.dgvInventory.AllowUserToDeleteRows = false; | |
this.dgvInventory.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; | |
this.dgvInventory.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically; | |
this.dgvInventory.Location = new System.Drawing.Point(16, 130); | |
this.dgvInventory.MultiSelect = false; | |
this.dgvInventory.Name = "dgvInventory"; | |
this.dgvInventory.ReadOnly = true; | |
this.dgvInventory.Size = new System.Drawing.Size(312, 309); | |
this.dgvInventory.TabIndex = 19; | |
// | |
// dgvQuests | |
// | |
this.dgvQuests.AllowUserToAddRows = false; | |
this.dgvQuests.AllowUserToDeleteRows = false; | |
this.dgvQuests.AllowUserToResizeRows = false; | |
this.dgvQuests.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; | |
this.dgvQuests.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically; | |
this.dgvQuests.Location = new System.Drawing.Point(16, 446); | |
this.dgvQuests.MultiSelect = false; | |
this.dgvQuests.Name = "dgvQuests"; | |
this.dgvQuests.ReadOnly = true; | |
this.dgvQuests.Size = new System.Drawing.Size(312, 189); | |
this.dgvQuests.TabIndex = 20; | |
// | |
// btnTrade | |
// | |
this.btnTrade.Location = new System.Drawing.Point(493, 620); | |
this.btnTrade.Name = "btnTrade"; | |
this.btnTrade.Size = new System.Drawing.Size(75, 23); | |
this.btnTrade.TabIndex = 21; | |
this.btnTrade.Text = "Trade"; | |
this.btnTrade.UseVisualStyleBackColor = true; | |
this.btnTrade.Click += new System.EventHandler(this.btnTrade_Click); | |
// | |
// SuperAdventure | |
// | |
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); | |
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; | |
this.ClientSize = new System.Drawing.Size(719, 651); | |
this.Controls.Add(this.btnTrade); | |
this.Controls.Add(this.dgvQuests); | |
this.Controls.Add(this.dgvInventory); | |
this.Controls.Add(this.rtbMessages); | |
this.Controls.Add(this.rtbLocation); | |
this.Controls.Add(this.btnWest); | |
this.Controls.Add(this.btnSouth); | |
this.Controls.Add(this.btnEast); | |
this.Controls.Add(this.btnNorth); | |
this.Controls.Add(this.btnUsePotion); | |
this.Controls.Add(this.btnUseWeapon); | |
this.Controls.Add(this.cboPotions); | |
this.Controls.Add(this.cboWeapons); | |
this.Controls.Add(this.label5); | |
this.Controls.Add(this.lblLevel); | |
this.Controls.Add(this.lblExperience); | |
this.Controls.Add(this.lblGold); | |
this.Controls.Add(this.lblHitPoints); | |
this.Controls.Add(this.label4); | |
this.Controls.Add(this.label3); | |
this.Controls.Add(this.label2); | |
this.Controls.Add(this.label1); | |
this.Name = "SuperAdventure"; | |
this.Text = "My Game"; | |
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.SuperAdventure_FormClosing); | |
((System.ComponentModel.ISupportInitialize)(this.dgvInventory)).EndInit(); | |
((System.ComponentModel.ISupportInitialize)(this.dgvQuests)).EndInit(); | |
this.ResumeLayout(false); | |
this.PerformLayout(); | |
} | |
#endregion | |
private System.Windows.Forms.Label label1; | |
private System.Windows.Forms.Label label2; | |
private System.Windows.Forms.Label label3; | |
private System.Windows.Forms.Label label4; | |
private System.Windows.Forms.Label lblHitPoints; | |
private System.Windows.Forms.Label lblGold; | |
private System.Windows.Forms.Label lblExperience; | |
private System.Windows.Forms.Label lblLevel; | |
private System.Windows.Forms.Label label5; | |
private System.Windows.Forms.ComboBox cboWeapons; | |
private System.Windows.Forms.ComboBox cboPotions; | |
private System.Windows.Forms.Button btnUseWeapon; | |
private System.Windows.Forms.Button btnUsePotion; | |
private System.Windows.Forms.Button btnNorth; | |
private System.Windows.Forms.Button btnEast; | |
private System.Windows.Forms.Button btnSouth; | |
private System.Windows.Forms.Button btnWest; | |
private System.Windows.Forms.RichTextBox rtbLocation; | |
private System.Windows.Forms.RichTextBox rtbMessages; | |
private System.Windows.Forms.DataGridView dgvInventory; | |
private System.Windows.Forms.DataGridView dgvQuests; | |
private System.Windows.Forms.Button btnTrade; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment