设计"健康秤"程序,界面设计如图3-37所示。具体要求如下:

设计"健康秤"程序,界面设计如图3-37所示。具体要求如下:,第1张

Private Sub Command1_Click()

Dim s%, a%, b%

s = Val(Text1.Text)

a = Val(Text2.Text)

b = s - 105

If a >(1.1 * b) Then

Label3.Caption = "偏胖,加强锻炼雹旅,注意节食"

ElseIf a <(0.9 * b) Then

Label3.Caption = "偏瘦,增加备信营养"

Else

Label3.Caption = "正常,继续维持"

End If

End Sub

Private Sub Form_Load()

Text1.MaxLength = 3

Text2.MaxLength = 3

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii <>8 And (KeyAscii <48 Or KeyAscii >57) Then

KeyAscii = 0

End If

End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)

If KeyAscii <>8 And (KeyAscii <48 Or KeyAscii >57) Then

KeyAscii = 0

End If

End Sub

根据我做仿肆轮的一定对的,我调试过的,不过要看仔细,有些分号这种标点也是很致命的哦

界面代码如下:

namespace HealthScale

{

partial class Form1

{

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.IContainer components = null

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

/// 枯腔闭<param name="disposing">没裂如果应释放托管资源,为 true;否则为 false。</param>

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose()

}

base.Dispose(disposing)

}

#region Windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.label1 = new System.Windows.Forms.Label()

this.label2 = new System.Windows.Forms.Label()

this.txtHeight = new System.Windows.Forms.TextBox()

this.txtWeight = new System.Windows.Forms.TextBox()

this.btnOK = new System.Windows.Forms.Button()

this.btnCancel = new System.Windows.Forms.Button()

this.label3 = new System.Windows.Forms.Label()

this.label4 = new System.Windows.Forms.Label()

this.SuspendLayout()

//

// label1

//

this.label1.AutoSize = true

this.label1.Location = new System.Drawing.Point(33, 37)

this.label1.Name = "label1"

this.label1.Size = new System.Drawing.Size(29, 12)

this.label1.TabIndex = 0

this.label1.Text = "身高"

//

// label2

//

this.label2.AutoSize = true

this.label2.Location = new System.Drawing.Point(33, 87)

this.label2.Name = "label2"圆埋

this.label2.Size = new System.Drawing.Size(29, 12)

this.label2.TabIndex = 1

this.label2.Text = "体重"

//

// txtHeight

//

this.txtHeight.Location = new System.Drawing.Point(80, 34)

this.txtHeight.MaxLength = 6

this.txtHeight.Name = "txtHeight"

this.txtHeight.Size = new System.Drawing.Size(143, 21)

this.txtHeight.TabIndex = 2

this.txtHeight.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtHeight_KeyPress)

//

// txtWeight

//

this.txtWeight.Location = new System.Drawing.Point(80, 84)

this.txtWeight.MaxLength = 6

this.txtWeight.Name = "txtWeight"

this.txtWeight.Size = new System.Drawing.Size(143, 21)

this.txtWeight.TabIndex = 3

this.txtWeight.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtWeight_KeyPress)

//

// btnOK

//

this.btnOK.Location = new System.Drawing.Point(40, 144)

this.btnOK.Name = "btnOK"

this.btnOK.Size = new System.Drawing.Size(75, 23)

this.btnOK.TabIndex = 4

this.btnOK.Text = "健康状况"

this.btnOK.UseVisualStyleBackColor = true

this.btnOK.Click += new System.EventHandler(this.btnOK_Click)

//

// btnCancel

//

this.btnCancel.Location = new System.Drawing.Point(148, 144)

this.btnCancel.Name = "btnCancel"

this.btnCancel.Size = new System.Drawing.Size(75, 23)

this.btnCancel.TabIndex = 5

this.btnCancel.Text = "结束"

this.btnCancel.UseVisualStyleBackColor = true

this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click)

//

// label3

//

this.label3.AutoSize = true

this.label3.Location = new System.Drawing.Point(229, 37)

this.label3.Name = "label3"

this.label3.Size = new System.Drawing.Size(17, 12)

this.label3.TabIndex = 6

this.label3.Text = "cm"

//

// label4

//

this.label4.AutoSize = true

this.label4.Location = new System.Drawing.Point(229, 87)

this.label4.Name = "label4"

this.label4.Size = new System.Drawing.Size(17, 12)

this.label4.TabIndex = 7

this.label4.Text = "kg"

//

// Form1

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F)

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

this.ClientSize = new System.Drawing.Size(292, 213)

this.Controls.Add(this.label4)

this.Controls.Add(this.label3)

this.Controls.Add(this.btnCancel)

this.Controls.Add(this.btnOK)

this.Controls.Add(this.txtWeight)

this.Controls.Add(this.txtHeight)

this.Controls.Add(this.label2)

this.Controls.Add(this.label1)

this.Name = "Form1"

this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen

this.Text = "健康秤"

this.Load += new System.EventHandler(this.Form1_Load)

this.ResumeLayout(false)

this.PerformLayout()

}

#endregion

private System.Windows.Forms.Label label1

private System.Windows.Forms.Label label2

private System.Windows.Forms.TextBox txtHeight

private System.Windows.Forms.TextBox txtWeight

private System.Windows.Forms.Button btnOK

private System.Windows.Forms.Button btnCancel

private System.Windows.Forms.Label label3

private System.Windows.Forms.Label label4

}

}

后台处理代码如下:

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

using System.Text.RegularExpressions

namespace HealthScale

{

public partial class Form1 : Form

{

//正则表达式

private Regex regex

public Form1()

{

InitializeComponent()

}

private void Form1_Load(object sender, EventArgs e)

{

//初始化正则表达式,判断是否为数字

regex = new Regex(@"^\d+\.{0,1}\d*$")

}

/// <summary>

/// 健康状况按钮

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void btnOK_Click(object sender, EventArgs e)

{

//判断是否为空

if (String.IsNullOrEmpty(txtHeight.Text))

{

MessageBox.Show(this, "请输入身高", "健康秤", MessageBoxButtons.OK, MessageBoxIcon.Information)

return

}

if (String.IsNullOrEmpty(txtWeight.Text))

{

MessageBox.Show(this, "请输入体重", "健康秤", MessageBoxButtons.OK, MessageBoxIcon.Information)

return

}

//获取身高体重

float height = float.Parse(txtHeight.Text)

float weight = float.Parse(txtWeight.Text)

//判断体重范围给出结果

if (weight > (height - 105) * 1.1)

{

MessageBox.Show(this, "偏胖,注意节食", "健康秤", MessageBoxButtons.OK, MessageBoxIcon.Information)

}

else if (weight < (height - 105) * 0.9)

{

MessageBox.Show(this, "偏瘦,增加营养", "健康秤", MessageBoxButtons.OK, MessageBoxIcon.Information)

}

else

{

MessageBox.Show(this, "正常,继续保持", "健康秤", MessageBoxButtons.OK, MessageBoxIcon.Information)

}

}

/// <summary>

/// 结束按钮

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void btnCancel_Click(object sender, EventArgs e)

{

this.Close()

}

/// <summary>

/// 身高文本框键盘按下

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void txtHeight_KeyPress(object sender, KeyPressEventArgs e)

{

//如果不是数字并且不是退格键

if (!regex.IsMatch(txtHeight.Text + e.KeyChar.ToString()) && e.KeyChar != (char)Keys.Back)

{

MessageBox.Show(this, "非法字符", "健康秤", MessageBoxButtons.OK, MessageBoxIcon.Information)

e.Handled = true

}

}

/// <summary>

/// 体重文本框键盘按下

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void txtWeight_KeyPress(object sender, KeyPressEventArgs e)

{

//如果不是数字并且不是退格键

if (!regex.IsMatch(txtWeight.Text + e.KeyChar.ToString()) && e.KeyChar != (char)Keys.Back)

{

MessageBox.Show(this, "非法字符", "健康秤", MessageBoxButtons.OK, MessageBoxIcon.Information)

e.Handled = true

}

}

}

}

界面样式如图:


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12554693.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存