zoukankan      html  css  js  c++  java
  • MFC edit control 用法

    MFC edit control 用法(总结)

    MFC里面的EDIT Control控件的用法是怎么样的,1.怎么样才能赋值给EDIT Control控件并在EDIT Control控件显示出来;2.怎么取得EDIT Control控件的值并传递给一个变量?希望各位大侠帮帮忙!
    //获得EDIT CEdit* pBoxOne; pBoxOne = (CEdit*) GetDlgItem(IDC_EDIT1); //付值 pBoxOne->SetWindowText( _T"FOO" ); //取值 CString str; pBoxOne->GetWindowText(str);

    GetDlgItem(IDC_EDIT1)->SetWindowText( _T"FOO" ); 也可以
    //取值 CString str; GetDlgItem(IDC_EDIT1)->GetWindowText(str);

    EditControl是在MFC对话框中最频繁的使用的控件之一

    VC++2005提供EditControl的属性和控件事件操作简单方便

    1只允许输入数字 如果设置EditControl中只能输入数字,在VC6.0中需要编写一个派生类来达到目的,而在VC++2005下只需要在属性对话框中将Number的属性值设

    为True就可以了.

    2获取EditControl的内容 两种方法 第一种,利用MFC应用程序向导生成一个基于对话框的应用程序,从资源视图中选择该Dialog窗体,利用右侧的工具箱,向Dialog内添加一个

    EditControl项,声明控件变量的类别为Value,变量类型为CString,变量名为m_sEdit_Content.

    CString m_sEdit_Content; CString s; UpdateData(true); s=m_sEdit_Content.GetString(); MessageBox(s,_T("获取编辑框的内容"),MB_OK); s.ReleaseBuffer();

    这样就取得了编辑框的内容

    UpdateData(true);这句代码很重要,它的作用是将输入的数据装入EditControl对应的变量m_sEdit_Content中. 由于MFC应用程序向导默认是使用Unicode库,所以MessageBox中的字符串需要用_T(),否则会出现const char[]转换LPCTSTR错误,如果不使用

    Unicode库就不需要_T().

    第二种方法 声明控件变量的类别为Control,变量类型为CEdit,变量名为m_Edit_Content. 代码如下(Unicode) CString s; s.GetBufferSetLength(1024); m_Edit_Content.GetWindowTextW(s.GetBuffer(),s.GetLength()); MessageBox(s,_T("获取文本框的内容"),MB_OK); s.ReleaseBuffer();

    如果不是Unicode下获取编辑框内容的函数就是GetWindowTextA

    3将EditControl中的内容转化为整数

    在限制编辑框只能数字之后,要将字符串转化为整数 声明控件变量的类别为Value,变量类型为CString,变量名为m_sEdit_Content. CString s; UpdateData(true); s=m_sEdit_Content.GetString(); int n=_tstoi(s); s.ReleaseBuffer(); n就是所需要的整数 在VC2005下字符串转换成整数需要_tstoi这个函数

    4限制编辑框的输入长度 声明控件变量的类别为Control,变量类型为CEdit,变量名为m_Edit_Content. 在对话框初始化的地方写m_Edit_Content.SetLimitText(1); 编辑框就只能输入一个字符了.

  • 相关阅读:
    几种常用的曲线
    0188. Best Time to Buy and Sell Stock IV (H)
    0074. Search a 2D Matrix (M)
    0189. Rotate Array (E)
    0148. Sort List (M)
    0859. Buddy Strings (E)
    0316. Remove Duplicate Letters (M)
    0452. Minimum Number of Arrows to Burst Balloons (M)
    0449. Serialize and Deserialize BST (M)
    0704. Binary Search (E)
  • 原文地址:https://www.cnblogs.com/gosteps/p/2987451.html
Copyright © 2011-2022 走看看