登陆界面(login Interface)

来自中国的朋友,欢迎您在本版面使用中文讨论问题。请注意,如果您想得到不懂中文的人的帮助,请同时提供英文译文。
Help and discussions in Chinese.
Post Reply
liqi98136
Posts: 53
Joined: Sun Jan 17, 2010 11:37 pm

登陆界面(login Interface)

Post by liqi98136 »

字符串的比较问题,有谁有更好的方法。
On comparison of string, who have a better way???
strcmp(str1,str2)
str1 > str2 return >0
str1==str2 return 0
str1 < str2 return <0
 当s1<s2时,返回值<0
 当s1=s2时,返回值=0
 当s1>s2时,返回值>0
能不能直接比较?
How to directly compare?
str3="abc"
if(str3=="abc") { ...}

Code: Select all

import "ecere"

class Form1 : Window
{
   text = "登陆";
   background = activeBorder;
   borderStyle = sizable;
   hasMaximize = true;
   hasMinimize = true;
   hasClose = true;
   size = { 256, 240 };
   anchor = { horz = -163, vert = -111 };

   Label label1 { this, text = "用户名:", position = { 64, 72 } };
   Label label2 { this, text = "密码:", position = { 64, 112 } };
   EditBox editBox1 { this, text = "editBox1", position = { 120, 72 }, contents = "abc" };
   EditBox editBox2 { this, text = "editBox2", position = { 120, 112 }, contents = "123" };
   Button button1
   {
      this, text = "登陆", position = { 88, 160 };

      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         
         if (strcmp(editBox1.contents,"")==0)
            {
              MessageBox{text="错误", contents="请输入用户名", type=ok}.Modal();
            }
         else
         {
            if(strcmp(editBox1.contents,"abc")==0 && strcmp(editBox2.contents,"123")==0)
            {
              MessageBox{text="信息", contents="你是合法用户,欢迎进入", type=okCancel}.Modal(); 
            }
            else
            {
              MessageBox{text="错误", contents="用户名和密码错误", type=ok}.Modal(); 
            }
         }
         return true;
      }
   };
   Button button2 
   {      
      this, text = "取消", position = { 160, 160 };

      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         Destroy(0);
         return true;
      }
   };

   bool OnCreate(void)
   {
      editBox1.contents="";
      editBox2.contents="";
      return true;
   }
}

Form1 form1 {};
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: 登陆界面(login Interface)

Post by jerome »

Hi liqi,

strcmp is quite standard in C/eC.
I usually write it:

if(!strcmp(str1, str2)) { PrintLn("Strings are the same!"); }

Having a negative, zero, or positive value is very useful in sorting situations.
eC also has an 'OnCompare' method accessible on all data types, including on 'String' (Which simply maps to 'char *' at the moment). You could invoke it as such;

Code: Select all

String str1 = "String1";
String str2 = "String2";
 
if(!str1.OnCompare(str2)) { PrintLn("Strings are the same!"); }
strcmp is just as good though. At one point in the future, we will have a more feature rich String class which might have an == operator that works like that 'if(str3=="abc")'. The meaning of that in C/eC however is somewhat ambiguous and will need to be clarified.

Cheers,

Jerome
Post Reply