Code snippets 1: ListBox

来自中国的朋友,欢迎您在本版面使用中文讨论问题。请注意,如果您想得到不懂中文的人的帮助,请同时提供英文译文。
Help and discussions in Chinese.
Post Reply
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Code snippets 1: ListBox

Post by samsam598 »

Code: Select all

import "ecere"
 
class Form1 : Window
{
   text = "Form1";
   background = activeBorder;
   borderStyle = fixed;
   hasClose = true;
   size = { 688, 256 };
   anchor = { horz = -43, vert = -93 };
 
   Label label1 { this, text = "在左边的ListBox双击一行则将该行文字添加给右边的ListBox", font = { "Tahoma", 9 }, size = { 324, 21 }, position = { 16, 8 } };
   Label label2 { this, text = "在右边的ListBox里双击一行则将该行删除", font = { "Tahoma", 9 }, size = { 308, 21 }, position = { 352, 8 } };
   ListBox listBox2 
   {
      this, text = "listBox2", size = { 316, 172 }, position = { 352, 40 };
 
      bool NotifyDoubleClick(ListBox listBox, int x, int y, Modifiers mods)
      {
         if(listBox.currentRow)
            listBox.DeleteRow(listBox.currentRow);
         return true;
      }
   };
   ListBox listBox1 
   {
      this, text = "listBox1", size = { 324, 172 }, position = { 16, 40 };
 
      bool NotifyDoubleClick(ListBox listBox, int x, int y, Modifiers mods)
      {
         if( listBox.currentRow)
            listBox2.AddString(listBox.currentRow.string);
         return true;
      }
   };
 
   bool OnPostCreate(void)
   {
      int i;
      for(i=0;i<10;i++)
      {
         this.listBox1.AddStringf("Line #%d",i+1);
         this.listBox1.AddStringf("第%d行",i+1);
      }
      return true;
   }
}
 
Form1 form1 {};
 
 
Last edited by samsam598 on Thu Sep 08, 2011 4:30 am, edited 1 time in total.
liqi98136
Posts: 53
Joined: Sun Jan 17, 2010 11:37 pm

Re: Code snippets 1: ListBox

Post by liqi98136 »

顶一个,加油.
D.Bane
Posts: 78
Joined: Thu Jul 29, 2010 2:51 pm

Re: Code snippets 1: ListBox

Post by D.Bane »

Hi Sam.

Hope you do not mind me saying this but you will have less to write in your code...

Code: Select all

 
ListBox listBox2 
 {
    this, text = "listBox2", size = { 316, 172 }, position = { 352, 40 };
 
    bool NotifyDoubleClick(ListBox listBox, int x, int y, Modifiers mods)
    {
       //the call is made inside of the listBox2 and it is referenced here as listBox
       if(listBox.currentRow)
          listBox.DeleteRow(listBox.currentRow);
       return true;
    }
 };
 ListBox listBox1 
 {
    this, text = "listBox1", size = { 324, 172 }, position = { 16, 40 };
 
    bool NotifyDoubleClick(ListBox listBox, int x, int y, Modifiers mods)
    {
       //the call is made inside of the listBox1 and it is referenced here as listBox as well
       // we need to call the listBox2 as it is
       if( listBox.currentRow)
          listBox2.AddString(listBox.currentRow.string);
       return true;
    }
 };
You also do not need to write the "this." part as it will call it just like it is not there... This all of course is not going to generate errors, but it is faster :)

Best Regards, oh, and thank you for the questions on forum, you saved me some trouble :)
D.Bane.
No army is to big, if the heart is beating strong.
There's no bad luck, it's just easier to blame someone else.
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: Code snippets 1: ListBox

Post by samsam598 »

Hi D.Bane,

Thank you!Yes,just noticed that

Code: Select all

bool NotifyDoubleClick(ListBox listBox, int x, int y, Modifiers mods)
it can be referenced as listBox.

Also noted that in many cases the this can be omitted.I used it just to fire the code auto-completion :D

Appreciate your help and you are always welcome.

Regards,
Sam
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Code snippets 1: ListBox

Post by jerome »

Thanks Sam and D.Bane for the samples =)

D.Bane is right the 'listBox' parameter is passed in for convenience, and you can use it instead of 'listBox1' or 'this.listBox1' but the main reason for it is so that you can use the same method for multiple controls, e.g.

Code: Select all

listBox1.NotifyClicked = AnyListBoxClicked;
listBox2.NotifyClicked = AnyListBoxClicked;
(You can experiment with the Methods Sheet's detach and attach buttons)

Then you know which list box sent the notification :D
But it's fine as well to use 'listBox1' (which translates to this.listBox1) if you want to be explicit.

Sam, you can bring up the auto-complete with Ctrl-Space, which lists all identifiers available in the current context, and if you have started typing anything it will auto-complete it. Ctrl-Shift-Space is to bring up the instantiation members and function parameters. The auto-complete is a bit buggy right now, it will be part of the focus of 0.44 pre-release 2 to fix most of the bugs.

Our milestones ahead: https://launchpad.net/ecere/+milestones
linghutf
Posts: 1
Joined: Sun Sep 18, 2011 9:14 am

Re: Code snippets 1: ListBox

Post by linghutf »

控件库里没有?基本控件应该学学Qt,都做出来。
Post Reply