1. messagebox.show,中如何把字符串转换为一个byte数组?
string s="ssasfs" ; byte[] by = System.Text.ASCIIEncoding.UTF8.GetBytes(s.ToCharArray()); string st = ""; foreach (byte b in by) { st += b.ToString(); } MessageBox.Show(st); 或者这样也可以. 由于byte是存储数值类型的,所以输出的是数字.
2. vs2010中如何写一个弹出框提示?
1.首先打开桌面安装好的visual studio 20122.打开之后,按ctrl+shift+N快捷键,在弹出的界面选择wpf应用程序。
3.在下面并输入名称和保存路径,点击确定即可。
4.当创建完成以后,在打开的界面,点击左边的工具栏
5.在弹出的下拉菜单,拖动button功能到窗口界面,并在右边content,将其名称修改为请点击
6.然后双击请点击按钮,进入编辑代码界面
7.这时候在代码里面输入内容: MessageBox.Show("这是一个桌面应用程序","标题桌面");
8.这时候点击上面的启动按钮,进行预览操作。
9.根据提示,点击,就会弹出消息框。
3. 怎么在没有net环境运行?
这是不行的,因为winform本身就依托于.net环境.netframework为我们封装了对电脑的各种操作,你所使用的所有c#语法(如MessageBox.Show("123");它会绘制一个窗口然后在显示器上输出“123”)都是.net为我们封装好的,这些语法会综合使用底层的API接口以达到我们想要的功能,如果没有.net环境计算机底层就不能识别我们写的代码,当然就无法运行所以你可以在程序打包发布时加上.net4.5的安装包,这样就可以在所有Windows上运行了如果你是指在非Windows平台运行,那winform是达不到的,因为.netframework是不能跨平台的;但是现在微软新出一个跨平台的开源框架.netcore;此框架可以保证基于此框架的.net程序可以在所有操作系统上运行但是这个框架也不能运行winform程序,你只能用.net语法在.netcore上重新实现你的功能才能跨平台,只是现在.netcore上并没有一个像winform这样好的界面开发方式
4. ss格式字符转为日期型?
string dateTimeString = "2008-09-12 15:14:56.135"; DateTime time = DateTime.ParseExact(dateTimeString, "yyyy-MM-dd HH:mm:ss.fff", null); MessageBox.Show(time.ToString("yyyy-MM-dd HH:mm:ss.fff"));
5. 实现UDP协议的发送与接收?
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.Net.Sockets;
using System.Net;
using System.Threading;
namespace _02UDP通信
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 用于UDP发送的网络服务类
/// </summary>
private UdpClient udpcSend;
/// <summary>
/// 用于UDP接收的网络服务类
/// </summary>
private UdpClient udpcRecv;
private void btnSend_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtSendMsg.Text))
{
messageBox.Show("请先输入待发送内容");
return;
}
// 匿名发送
//udpcSend = new UdpClient(0); // 自动分配本地IPv4地址
// 实名发送
IPEndPoint localIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 25555); // 本机IP,指定的端口号
udpcSend = new UdpClient(localIpep);
Thread thrSend = new Thread(SendMessage);
thrSend.Start(txtSendMsg.Text);
}
/// <summary>
/// 发送信息
/// </summary>
/// <param name="o"></param>
void SendMessage(object o)
{
string message = (string)o;
byte[] sendbytes = Encoding.Unicode.GetBytes(message);
IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8848); // 发送到的IP地址和端口号
udpcSend.Send(sendbytes, sendbytes.Length, remoteIpep);
udpcSend.Close();
ResetTextBox(txtSendMsg);
}
/// <summary>
/// 开关:在监听UDP报文阶段为true,否则为false
/// </summary>
bool IsUdpcRecvStart = false;
/// <summary>
/// 线程:不断监听UDP报文
/// </summary>
Thread thrRecv;
/// <summary>
/// 按钮:接收数据开关
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRecv_Click(object sender, EventArgs e)
{
if (!IsUdpcRecvStart) // 未监听的情况,开始监听
{
IPEndPoint localIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8848); // 本机IP和监听端口号
udpcRecv = new UdpClient(localIpep);
thrRecv = new Thread(ReceiveMessage);
thrRecv.Start();
IsUdpcRecvStart = true;
ShowMessage(txtRecvMsg, "UDP监听器已成功启动");
}
else // 正在监听的情况,终止监听
{
thrRecv.Abort(); // 必须先关闭这个线程,否则会异常
udpcRecv.Close();
IsUdpcRecvStart = false;
ShowMessage(txtRecvMsg, "UDP监听器已成功关闭");
}
}
/// <summary>
/// 接收数据
/// </summary>
/// <param name="o"></param>
void ReceiveMessage(object o)
{
IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
try
{
byte[] bytRecv = udpcRecv.Receive(ref remoteIpep);
string message = Encoding.Unicode.GetString( bytRecv, 0, bytRecv.Length);
ShowMessage(txtRecvMsg,string.Format("{0}[{1}]", remoteIpep, message));
}
catch (Exception ex)
{
ShowMessage(txtRecvMsg, ex.Message);
break;
}
}
}
// 向TextBox中添加文本
delegate void ShowMessageDelegate(TextBox txtbox, string message);
void ShowMessage(TextBox txtbox, string message)
{
if (txtbox.InvokeRequired)
{
ShowMessageDelegate showMessageDelegate = ShowMessage;
txtbox.Invoke(showMessageDelegate, new object[] { txtbox, message });
}
else
{
txtbox.Text += message + "\r\n";
}
}
// 清空指定TextBox中的文本
delegate void ResetTextBoxDelegate(TextBox txtbox);
void ResetTextBox(TextBox txtbox)
{
if (txtbox.InvokeRequired)
{
ResetTextBoxDelegate resetTextBoxDelegate = ResetTextBox;
txtbox.Invoke(resetTextBoxDelegate, new object[] { txtbox });
}
else
{
txtbox.Text = "";
}
}
/// <summary>
/// 关闭程序,强制退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Environment.Exit(0);
}
}
}
6. 编译程序用ct衫每件35元每条裤子120买3个T衫2条裤子需要多少钱打88折后多少钱?
double tShirtPrice = 3.5; double pantsPrice = 120; double fullPrice = 3 * tShirtPrice + 2 * pantsPrice; double discount = fullPrice * 0.88; MessageBox.Show(string.Format(买3个T衫和2条裤子全价 ,88折为 , fullPrice, discount));
7. 代码打开外部程序?
private void button1_Click(object sender, EventArgs e) { try { ProcessStartInfo MyStarInfo = new ProcessStartInfo(); MyStarInfo.FileName="C:\\A.txt"; ////窗口起始状态最大化 MyStarInfo.WindowStyle = ProcessWindowStyle.Maximized; Process MyProcees = new Process(); MyProcees.StartInfo = MyStarInfo; MyProcees.Start(); } catch (Exception ex) { MessageBox.Show(ex.Message, "信息提示!", MessageBoxButtons.OK, MessageBoxIcon.Information); } }