想请教各位几个问题,这个程序是你点击它,label默认向左走,到了窗体边时候,自动往回走,到那个边自动往回走,一直这样。另一个按钮是设置它到底向哪个方向走如何让它碰到窗体就往回走,我写时候,它只定在窗体边,没反应啦
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace 随机数产生
{
public partial class Form1 : Form
{
delegate void dfun(string text);
dfun dfun1;
private Thread thread;
int i;
public Form1()
{
dfun1 = new dfun(settext);
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "开始")
{
thread = new Thread(new ThreadStart(fun));
//label1.Text = "0";
thread.Start();
button1.Text = "暂停";
}
else {
if (thread.IsAlive) { thread.Abort();
button1.Text = "开始";
}
} }
public void fun() {
while (true)
{
int x = Convert.ToInt32(label1.Text);
string s = Convert.ToString(x);
label1.Invoke(dfun1, new object[] { s });
Thread.Sleep(100);
}
}
private void settext(string text)
{
label1.Left -= 3;
i = label1.Left;
if (i<0)
// if (label1.Left < 0 && label1.Right >this.Width)
{
label1.Left += 3;
i=label1.Left;
}
if (i > this.Width - 1)
{
label1.Left -= 3;
i = label1.Left;
}
string temp = "";
int j;
j = label1.Right;
temp += j.ToString() + "\n";
label3.Text = temp; //显示在label1中*/
Random ran = new Random(); int arr1 = ran.Next(0, 9); string temp1 = ""; temp1 += arr1.ToString() + "\n";
label1.Text = temp1; //显示在label1中*/
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog();
} }}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace 随机数产生
{
public partial class Form1 : Form
{
delegate void dfun(string text);
dfun dfun1;
private Thread thread;
int i;
public Form1()
{
dfun1 = new dfun(settext);
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "开始")
{
thread = new Thread(new ThreadStart(fun));
//label1.Text = "0";
thread.Start();
button1.Text = "暂停";
}
else {
if (thread.IsAlive) { thread.Abort();
button1.Text = "开始";
}
} }
public void fun() {
while (true)
{
int x = Convert.ToInt32(label1.Text);
string s = Convert.ToString(x);
label1.Invoke(dfun1, new object[] { s });
Thread.Sleep(100);
}
}
private void settext(string text)
{
label1.Left -= 3;
i = label1.Left;
if (i<0)
// if (label1.Left < 0 && label1.Right >this.Width)
{
label1.Left += 3;
i=label1.Left;
}
if (i > this.Width - 1)
{
label1.Left -= 3;
i = label1.Left;
}
string temp = "";
int j;
j = label1.Right;
temp += j.ToString() + "\n";
label3.Text = temp; //显示在label1中*/
Random ran = new Random(); int arr1 = ran.Next(0, 9); string temp1 = ""; temp1 += arr1.ToString() + "\n";
label1.Text = temp1; //显示在label1中*/
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog();
} }}
解决方案 »
- 关于RichTextbox与Textbox的区别
- C#中 cookie的问题!
- 如何给一个类的实例在运行时动态添加一个属性?
- 如何实现sql的函数显示?
- 麻烦给一个正则表达式 判断 MM-dd HH:mm 这类形式的 可以不考虑闰年的情况
- C#读取文件出错
- C#中怎样获得OS的版本,及window的系统目录,和windows的零时目录(即window中的Temp目录)
- remoting服务器宿主到iis上,第一次运行客户端程序正常,关掉后,再次运行就说无法连接到服务器
- 请教一个简单问题,数据类型 string 和 String 的区别在哪里?
- 在北京寻找工作ing......
- 如何让C#的版本能自增
- 关于异步的方法区别例如BeginConnect和Connect
用个Timer就完了,搞这么复杂。TIP:中文的命名空间最好改掉这个坏习惯
bool b= true;
private void timer1_Tick(object sender, EventArgs e)
{
this.label1.Location = new Point(pos,this.label1.Location.Y);
if (pos== this.Width-this.label1.Size.Width)
b= false;
if(pos==0)
b= true;
if (p)
pos++;
else
pos--;
}
http://topic.csdn.net/u/20090606/10/5fc58b85-d448-4449-a46e-feb3fd9b0bcc.html
private decimal V = 1; //小球移动速度
private decimal X1 = 0; //当前小球X轴位置
private decimal Y1 = 0; //当前小球Y轴未知
private bool XPositive = true; //小球改变X轴方向标志
private bool YPositive = true; //小球改变Y轴方向标志 //窗体初始化
private void BallForm_Load(object sender, EventArgs e)
{
this.timer1.Tag = false;
this.button_ball.Location = new Point(0, 0);
} //X轴方向步进
private decimal DeltaX()
{
if (this.XPositive)
{
return this.V;
}
else
{
return 0 - this.V;
}
} //Y轴方向步进
private decimal DeltaY()
{
if (this.YPositive)
{
return this.V;
}
else
{
return 0 - this.V;
}
} //设置X轴换向
private void SetXPositive()
{
if (this.X1 < 0 || this.X1+this.button_ball.Width > this.ClientSize.Width)
{
this.XPositive = !this.XPositive;
}
} //设置Y轴换向
private void SetYPositive()
{
if(this.Y1<0 || this.Y1+this.button_ball.Height>this.ClientSize.Height)
{
this.YPositive=!this.YPositive;
}
} private int X()
{
this.X1= X1 + DeltaX();
return (int)this.X1;
} private int Y()
{
this.Y1= Y1 + DeltaY();
return (int)this.Y1;
} //设置速度
private void button_submit_Click(object sender, EventArgs e)
{
decimal.TryParse(this.textBox_speed.Text, out this.V);
} //Timer控件运行
private void timer1_Tick(object sender, EventArgs e)
{
this.SetXPositive();
this.SetYPositive();
this.button_ball.Location = new Point(this.X(), this.Y());
} //开始按钮
private void button_start_Click(object sender, EventArgs e)
{
bool running = false;
if (bool.TryParse(this.timer1.Tag.ToString(), out running))
{
if (running)
{
this.timer1.Stop();
this.button_start.Text = "运行";
}
else
{
this.timer1.Start();
this.button_start.Text = "停止";
}
running = !running;
this.timer1.Tag = running;
}
}