2014/10/3

10/3第三週 物體來回移動

10/3第三週  物體來回移動

visual C++

首先,先至工具箱建立兩個button、一個Timer

進入button2程式
先在public partial class Form1 : Form裡 輸入int c=0,s;
在到button2輸入移動的條件

然後回到表單點入Timer的程式
將剛剛的條件複製進來,即可執行

Ex:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int c, s = 1;

        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button1.Left > 430)
            {
                s = -1;
            }
            c = c + s;
            button1.Left = 50 * c;

            if (button1.Left < 0)
            {
                s = 1;
            }
            c = c + s;
            button1.Left = 50 * c;


        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (button1.Left > 430)
            {
                s = -1;
            }
            c = c + s;
            button1.Left = 50 * c;

            if (button1.Left < 0)
            {
                s = 1;
            }
            c = c + s;
            button1.Left = 50 * c;

        }
    }
}







Excel VB

首先,至檢視點選錄製聚集
在表單輸入1後按Enter,並至檢視停止錄製聚集
在點選檢視的檢視聚集做編輯
左上點選插入-->自訂表單
選擇空白按鈕並新增兩個
點選buttin2後進入程式輸入條件,即可執行
Ex:
Dim c, d As Integer

Private Sub CommandButton2_Click()

If CommandButton1.Left > 150 Then
d = -1
End If
If CommandButton1.Left < 50 Then
d = 1
End If
c = c + d
CommandButton1.Left = 50 * c

End Sub





沒有留言:

張貼留言