準備
(なし)
デザイン
- フォーム (Form1) にボタン (button1) を配置します。
- フォーム (Form1) にリストボックス (listBox1) を配置します。
サンプルコード (C#)
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int v1 = 100;
listBox1.Items.Add(v1);
string s1 = v1.ToString();
listBox1.Items.Add(s1);
int v2 = int.Parse(s1);
listBox1.Items.Add(v2);
}
}
}
解説
数値から文字列へ、そして文字列から数値へ変換します。数値から文字列への変換は ToString メソッドを使用します。文字列から数値への変換はそれぞれの型の Parse メソッドを使用します。
結果
動作確認環境
Visual Studio 2022 Professional (.NET 7 C#11)
ログ
初版:2016.04.18 Visual Studio 2015 Professional (C# 6.0)
