• C#如何弹出输入框


    在C#中,进行windows窗体应用程序编程的时候,经常需要弹出输入框,输入密码,输入文本之类的。然而,C#中没有直接弹出输入框的语句,MessageBox只能显示一段消息而不能输入。我们需要调用Microsoft.VisualBasic,使用VB中的inputbox,实现弹出输入框的功能。

    1、菜单栏,选择【项目】;然后在弹出的菜单中选择【添加引用】

     

    2、弹出“添加引用”的窗口,找到名称为Microsoft.VisualBasic的组件,选择它并点击【确定】

     

    3、使用命名空间Microsoft.VisualBasic。添加代码:using Microsoft.VisualBasic;

    using Microsoft.VisualBasic;

    4、在窗体中添加一个Button1和textBox1。我们要实现点击button1,用textBox1显示输入的文本的内容。

     

    5、

    调用VB中的InputBox,输入一串字符串。给按钮添加代码:

    string str = Interaction.InputBox("提示信息","标题","文本内容",-1,-1);

    Interaction.InputBox的格式:string  Interaction .InputBox(string Prompt,string title,string  Defaultresponce,int Xpos,int Ypose)

     6、参考代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 using Microsoft.VisualBasic;
    10 
    11 namespace WindowsFormsApplication1
    12 {
    13     public partial class Form1 : Form
    14     {
    15         public Form1()
    16         {
    17             InitializeComponent();
    18         }
    19 
    20         private void Form1_Load(object sender, EventArgs e)
    21         {
    22 
    23         }
    24 
    25         private void button1_Click(object sender, EventArgs e)
    26         {
    27             string str = Interaction.InputBox("提示信息","标题","文本内容",-1,-1);
    28             
    29             textBox1.Text = str;
    30         }
    31     }
    32 }

    7、结果显示:

  • 相关阅读:
    Linux简介
    在VMware上安装Ubuntu软件步骤与遇到的相关问题及解决方案
    深度学习框架之TensorFlow的概念及安装(ubuntu下基于pip的安装,IDE为Pycharm)
    Windows下安装Python及Eclipse中配置PyDev插件
    结构体定义struct和typedef struct
    定义与声明
    error LNK2005:错误改正方法
    OPENCV 笔记
    RANSANC算法
    梯度下降法和牛顿法
  • 原文地址:https://www.cnblogs.com/wangfeihu/p/5696232.html
Copyright © 2020-2023  润新知