###前提・実現したいこと
現在「数値をtextbox二つに打ち込み、ボタンを押すことで上のtextboxの数値から下のtextboxの数値が引かれ、labelに引かれた後の数値が表示される」計算機のようなものを作成しており、grid内に作成したひとまとまりの計算機部分をウィンドウ下部のbuttonを押すことによって横並びに増やせるようにしたいです。
・現在実行時のイメージ画像
・目標とする状態
また、将来的にはこのタブを複製したいと考えており、
このサイトを基にUsercontrolでタブの中身を作っています。
###発生している問題
調べ方が悪いのか、grid内のコントロールをbuttonを押すイベントで複製する方法がわかりません。
###該当のソースコード
MainWindow.xaml.cs
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Windows; 7using System.Windows.Controls; 8using System.Windows.Data; 9using System.Windows.Documents; 10using System.Windows.Input; 11using System.Windows.Media; 12using System.Windows.Media.Imaging; 13using System.Windows.Navigation; 14using System.Windows.Shapes; 15 16namespace WpfApplication1 17{ 18 /// <summary> 19 /// MainWindow.xaml の相互作用ロジック 20 /// </summary> 21 public partial class MainWindow : Window 22 { 23 public MainWindow() 24 { 25 InitializeComponent(); 26 } 27 private void newtab_Click(object sender, RoutedEventArgs e) 28 { 29 } 30 } 31}
UserControl1.xaml.cs
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Windows; 7using System.Windows.Controls; 8using System.Windows.Data; 9using System.Windows.Documents; 10using System.Windows.Input; 11using System.Windows.Media; 12using System.Windows.Media.Imaging; 13using System.Windows.Navigation; 14using System.Windows.Shapes; 15using System.Text.RegularExpressions; 16 17namespace WpfApplication1 18{ 19 /// <summary> 20 /// UserControl1.xaml の相互作用ロジック 21 /// </summary> 22 public partial class UserControl1 : UserControl 23 { 24 public UserControl1() 25 { 26 InitializeComponent(); 27 } 28 29 int HP, damage; 30 Regex notIntReg = new Regex(@"[^0-9]"); 31 private void text1_TextChanged(object sender, TextChangedEventArgs e) 32 { 33 text1.Text = notIntReg.Replace(text1.Text, ""); 34 int.TryParse(text1.Text,out HP); 35 } 36 37 private void text2_TextChanged(object sender, TextChangedEventArgs e) 38 { 39 text2.Text = notIntReg.Replace(text2.Text, ""); 40 int.TryParse(text2.Text, out damage); 41 } 42 43 private void Button_Click(object sender, RoutedEventArgs e) 44 { 45 HP = HP - damage; 46 NOWHP.Content = HP; 47 } 48 } 49}
###補足情報(言語/FW/ツール等のバージョンなど)
使用言語はC#で、WPFを使用しています。
VisualStudio Express 2013を使って作成しています。
読み取りづらい箇所等あるかもしれませんが何卒よろしくお願いいたします。
###変更1
UserControl1.xaml.cs
1namespace WpfApplication1 2{ 3 /// <summary> 4 /// UserControl1.xaml の相互作用ロジック 5 /// </summary> 6 public partial class UserControl1 : UserControl 7 { 8 public UserControl1() 9 { 10 InitializeComponent(); 11 } 12 //変数の移動 13 Regex notIntReg = new Regex(@"[^0-9]"); 14 private void text1_TextChanged(object sender, TextChangedEventArgs e) 15 { 16 text1.Text = notIntReg.Replace(text1.Text, ""); 17 //移動 18 } 19 20 private void text2_TextChanged(object sender, TextChangedEventArgs e) 21 { 22 text2.Text = notIntReg.Replace(text2.Text, ""); 23 //移動 24 } 25 26 private void Button_Click(object sender, RoutedEventArgs e) //移動先 27 { 28 int damage; 29 int NHP = Convert.ToInt32(NOWHP.Content); 30 int.TryParse(text2.Text, out damage); 31 NHP = NHP - damage; 32 NOWHP.Content = NHP; 33 } 34 } 35}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/28 17:59