This is the problem. Both situations exist(select the note or not select the note at the first time). I tried these two solutions, and found that each solution will only solve one problem. Maybe I should consider to find out if the note is selected at the beginning, and choose to use different code.
Thank you!