键盘的表格标签(文本编号)控制 [英] Form Label (text number) control from keyboard

查看:53
本文介绍了键盘的表格标签(文本编号)控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Visual Studio和Forms的新手。 我正在使用VS 2008并观看视频,了解如何更改标签文本(在本例中为整数"0"),将每个
按钮点击数量增加1(++)正如微软网站上的视频所述:   


我已编写自己的程序来执行该功能,并且工作正常。


这适用于Label8:


WHEN你点击标签上的数字一个接一个:


#pragma
endregion


       private


System :: Void label1_Click(System :: Object ^  sender,System :: EventArgs ^  e){


< span style ="font-size:10.0pt; font-family:'Courier New'">  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  


       ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;   int i = System :: Int32 :: Parse(label1-> Text);


<跨度风格= "字体大小:10.0pt;字体家庭: '信使新'">&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;   i ++;


      ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;   label1-> Text = i.ToString();


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }


现在我想要一个VS2008 Windows Form C ++程序显示一个标签(Label1),其零(0)为'text。


我需要在程序中添加什么代码,这样如果我按下键盘上的"a",Label1框中的零变为1(或x ++)?   如果我按下键盘上的"z",Label1框中的数字会减少1(x - )。 
这是我到目前为止所提出的:



// Label8a.cpp:主项目文件。




< p style ="margin-bottom:0in; margin-bottom:.0001pt; line-height:normal; text-autospace:none">
#include
" stdafx.h"



#include
"Form1.h"



#include
< windows.h>



#include < iostream>



#include
< conio.h>



int x = 0;





using
命名空间 Label8a;





[STAThreadAttribute]



int main( array < System :: String ^> ^ args)



{



     
//在创建任何控件之前启用Windows XP视觉效果



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;应用:: EnableVisualStyles();



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;应用:: SetCompatibleTextRenderingDefault(的);



     
//创建主窗口并运行它



      Application :: Run( gcnew Form1());



 



for (;;)



       {



int x = System :: Int32 :: Parse(label1-> Text);



  char ch;



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; CH = _getche();&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的 //
转换输入字符到ASCII



 



   
if
(( int ) (ch)== 97)        
//如果键盘是"a"罢工那么



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {X ++;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;    
//将1添加到x



   
if
(( int ) (ch)== 122)        
//如果键盘是"z",那么



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {X - ;}&NBSP;&NBSP;&NBSP ;         
//从x减去1



      
if (x <= 0){x = 0; }         //将x限制为正数



   
if
(x> = 39){x = 39;}     ;      //将x限制为最多39个



label1-> Text = x.ToString();  
//在label1中显示新文本



 



&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;睡眠(300);}&NBSP;
//反弹延迟



&NBSP;的
返回 0;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;      
//如果要停止计划,请控制C


}}


缺少的命令/语法是什么? 或者我是否需要使用线程来增加label1中的文本? 我花了40多个小时谷歌搜索没有结果的可能解决方案。


输出错误:


1> ------构建开始:项目:Label8a,配置:调试Win32 ------


1>编译......


1> Label8a.cpp


1> .\Label8a.cpp(23):错误C2065:label1的':未声明的标识符


<跨度风格= "字体大小:8.0pt; font-family:'Courier New'"> 1>。\ Label8a.cpp(23):错误C2227:左边的' - > Text'必须指向class / struct / union / generic type


1>        type is''unknown-type''


1>。\ Label8a.cpp(33):错误C2065:'label1':未声明的标识符


1>。\ Label8a.cpp(33):错误C2227:左边的' - > Text'必须指向class / struct / union / generic type


1>        type is''unknown-type''


1>。\ Label8a.cpp(36):错误C2059:语法错误:'}'


1>。\ Label8a.cpp(36):错误C2143:语法错误:缺少';'之前'}'


1>。\ Label8a.cpp(36):错误C2059:语法错误:'}'


1>生成日志保存在"文件:// C:C ++ Programs\Label8a\Label8a\Debug\BuildLog.htm"


1> Label8a - 7个错误,0个警告


==========构建:0成功,1失败,0最新,0跳过==========

解决方案

在您的原始代码中 - 您点击标签以增加标签的文本 - 您订阅了标签的点击事件。好吧,要在表单上捕获键盘输入,你需要一个类似的方法。


订阅键盘事件(很可能是KeyPress)。你将能够确定哪个从事件args中按下了键(e.KeyChar,如果它是KeyPress事件),你可以根据它更改标签的文本。




哦,我差点忘了。你需要将表单的KeyPreview属性设置为true,否则按键将注册表单上具有焦点的任何控件,而不是表单本身。

I am new to Visual Studio and Forms.  I am using VS 2008 and have watched the video on how to change the label text (in this case an integer "0") to increase the number by 1 (++) with each button click as noted the video at a microsoft web site:   

I have written my own program to do that function and it works fine.

This works for Label8:

WHEN YOU CLICK ON THE LABEL THE NUMBER GOES UP BY ONE:

#pragma endregion

      private:

System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {

             

                         int i = System::Int32::Parse(label1->Text);

                         i++;

                         label1->Text= i.ToString();

      }

Now I want a VS2008 Windows Form C++ program to display one Label (Label1) with a zero (0) as its’ text.

What code do I have to add to the program so that if I depress an "a" on the keyboard the zero in the Label1 box changes to a 1 (or x++)?   And if I depress a "z" on the keyboard the number in the Label1 box decreases by 1 (x--).  This is what I have come up with so far:

// Label8a.cpp : main project file.

#include "stdafx.h"

#include "Form1.h"

#include <windows.h>

#include<iostream>

#include <conio.h>

int x = 0;

using namespace Label8a;

[STAThreadAttribute]

int main(array<System::String ^> ^args)

{

      // Enabling Windows XP visual effects before any controls are created

      Application::EnableVisualStyles();

      Application::SetCompatibleTextRenderingDefault(false);

      // Create the main window and run it

      Application::Run(gcnew Form1());

 

for (;;)

       {

int x = System::Int32::Parse(label1->Text);

 char ch;

         ch=_getche();           

                                   // converts input character to ascii

 

    if ((int)(ch) == 97)          // if keyboard is an "a"strike then

              {x ++;}            // add 1 to x

    if ((int)(ch) == 122)         // if keyboard is a "z"strike then

                 {x --;}             // subtract 1 from x

       if (x <=0) {x =0;}         // limits x to positive numbers

    if (x >=39) {x =39;}         // limits x to 39 max.

label1->Text= x.ToString();   // displays new text in label1

 

              Sleep(300);}  // de-bounce delay

  return 0;                // Control C if you want to stop program

}}

What is the missing command/syntax?  Or do I need to use a thread to increment the text in the label1?  I have spent over 40 hours googling possible solutions with no results.

Output errors:

1>------ Build started: Project: Label8a, Configuration: Debug Win32 ------

1>Compiling...

1>Label8a.cpp

1>.\Label8a.cpp(23) : error C2065: 'label1' : undeclared identifier

1>.\Label8a.cpp(23) : error C2227: left of '->Text' must point to class/struct/union/generic type

1>        type is ''unknown-type''

1>.\Label8a.cpp(33) : error C2065: 'label1' : undeclared identifier

1>.\Label8a.cpp(33) : error C2227: left of '->Text' must point to class/struct/union/generic type

1>        type is ''unknown-type''

1>.\Label8a.cpp(36) : error C2059: syntax error : '}'

1>.\Label8a.cpp(36) : error C2143: syntax error : missing ';' before '}'

1>.\Label8a.cpp(36) : error C2059: syntax error : '}'

1>Build log was saved at "file://c:C++ Programs\Label8a\Label8a\Debug\BuildLog.htm"

1>Label8a - 7 error(s), 0 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

解决方案

In your original code - where you click on the label to increment the label's text - you subscribe to the label's click event. Well, to trap keyboard input on the form, you need a similar approach.

Subscribe to a keyboard event (most likely the KeyPress). You will be able to determine which key was pressed from the event args (e.KeyChar if it's the KeyPress event), and you can change the label's text based on that.


Oh, and I almost forgot. You need to set the form's KeyPreview property to true, otherwise the key press will register with whatever control on the form has the focus, instead of with the form itself.


这篇关于键盘的表格标签(文本编号)控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆