问题遇到的现象和发生背景
我想实现在用户窗口状态栏显示IP地址的功能,所以使用了以下代码:
private void toolStripStatusLabel3_Click(object sender, EventArgs e)
{
System.Net.IPHostEntry hostIP = System.Net.Dns.GetHostEntry(Environment.MachineName);
this.toolStripStatusLabel2.Text = hostIP.AddressList[0].ToString();
}
但是当这个运行的时候会把我前面显示系统的statuelabel给覆盖掉,还会显示一串乱码,我怀疑IP地址的数据格式错误,前面的属性我设置的都是居中,我有点怀疑不知道是哪里出了问题
问题相关代码,请勿粘贴截图
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;
namespace 哨兵1._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 菜单_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void toolStripStatusLabel2_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
this.toolStripStatusLabel2.Text = "本次登录时间戳为:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
}
private void toolStripStatusLabel3_Click(object sender, EventArgs e)
{
System.Net.IPHostEntry hostIP = System.Net.Dns.GetHostEntry(Environment.MachineName);
this.toolStripStatusLabel2.Text = hostIP.AddressList[0].ToString();
}
}
}
目的
让状态栏正常显示IP地址,不要遮住前面的