【问题标题】:Picture box filling with some percentages图片框填充了一些百分比
【发布时间】:2013-02-07 11:59:32
【问题描述】:

我对 C 语言编程非常陌生。我的设计如下图所示。

我的概念是我必须在“传输音量”文本框中设置一些音量(例如 100),然后按“设置”按钮。它会自动设置图片框的比例,效果很好。

现在我想在单击“重新生成”按钮时用颜色填充图片框。图片框要填充的颜色百分比应该是TextBox中关于颜色或液体的数字。

例如 如果我设置 GasPhase =5 ;烃类液体=5;水= 5;油基泥浆 =5;水基泥=5;未识别 = 75。

那么图片必须用“未识别”颜色填充 75%,用 5% e.t.c. 填充 GasPhase 颜色

我已经编写了如下所示的一些代码。

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 test
  {
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void txtTransferVolume_TextChanged(object sender, EventArgs e)
    {

    }

    private void txtTransferSet_Click(object sender, EventArgs e)
    {
        string value = txtTransferVolume.Text;
        double midvalue = Convert.ToDouble(value);
        lblTransferBottleMax.Text = value;
        lblTransferBottleMid.Text = (midvalue / 2).ToString();

    }

    private void chkTransferManual_CheckedChanged(object sender, EventArgs e)
    {


    }

    private void btnTransferBottleRegenerate_Click(object sender, EventArgs e)
    {

    }

   }
 }

请帮我按我的要求填写。

【问题讨论】:

    标签: c# picturebox


    【解决方案1】:

    这可以通过直接在图片框控件上绘图或在内存中创建位图并显示在图片框中来轻松实现。

    例子:

    private void DrawPercentages(int[] percentages, Color[] colorsToUse)
    {
       // Create a Graphics object to draw on the picturebox
       Graphics G = pictureBox1.CreateGraphics();
    
       // Calculate the number of pixels per 1 percent
       float pixelsPerPercent = pictureBox1.Height / 100f;
    
       // Keep track of the height at which to start drawing (starting from the bottom going up)
       int drawHeight = pictureBox1.Height;
    
       // Loop through all percentages and draw a rectangle for each
       for (int i = 0; i < percentages.Length; i++)
       {
           // Create a brush with the current color
           SolidBrush brush = new SolidBrush(colorsToUse[i]);
           // Update the height at which the next rectangle is drawn.
           drawHeight -= (int)(pixelsPerPercent * percentages[i]);
           // Draw a filled rectangle
           G.FillRectangle(brush, 0, drawHeight, pictureBox1.Width, pixelsPerPercent * percentages[i]);
        }    
    }
    

    当然,您必须检查两个数组的长度是否相同,等等。我只是想告诉您如何执行此操作的基本概念。

    这是一个关于如何在数组中获取数据并将它们传递给函数的概念。由于您为每个值使用不同的文本框,因此很难对它们进行迭代,所以现在我将向您展示如何使用现有的 6 个值来实现。

    private void btnTransferBottleRegenerate_Click(object sender, EventArgs e)
    {
      int[] percentages = new int[6];
      percentages[0] = int.Parse(txtTransferNotIdentified.Text);
      percentages[1] = int.Parse(txtTransferWater.Text);
      // And so on for every textbox
    
      Color[] colors = new Color[6];
      colors[0] = Color.Red;
      colors[1] = Color.Yellow;
      // And so on for every color
    
      // Finally, call the method in my example above
      DrawPercentages(percentages, colors);
    } 
    

    如果您的百分比总和不等于 100,您可以使用指定总和的第三个参数,并在 DrawPercentages 方法中将值 100f 更改为此值。

    【讨论】:

    • 我只是通过一个例子向您介绍绘图的概念。您可以随意解释它,但我看不出收集数据并将其作为数组传递如何是客观的。您还可以使用 Graphics 对象在其自己的线上绘制每个矩形,但我总是喜欢保持它的可伸缩性。对于我的方法,任何未来添加的数据都不会出现问题,因为为数据中的每个项目编写一行可能需要额外的代码。
    • 是的,你是对的,我也在考虑如何从那里收集数据(文本框和标签)并通过数组传递它。
    • 您知道如何将这些输入的值传递到百分比数组中吗?
    • 非常感谢您的回答,它的工作完美。但是您的答案需要进行小改动,即必须更改数组名称到颜色。
    猜你喜欢
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多