简体   繁体   English

如何使标签中的值根据值的大小自动更改其位置?

[英]How can i make that the value in the label will be automatic change its location according to the value size?

I have two labels for example the first one is: 我有两个标签,例如第一个是:

The temperature is now - 现在的温度是-

And the second label: 第二个标签:

46c 46c

But if sometimes only for the question if the temperature will get hight to 100 so it should be: 但是如果有时仅是为了询问温度是否会升高到100,那么应该是:

The temperature is now - 100c 现在的温度是-100摄氏度

But then the c of the 100 will get over the "-" of the other label. 但是,100的c将超过另一个标签的“-”。 So if its two digits its ok but if its three digits its getting over the other label. 因此,如果它的两位数可以,但是如果它的三位数可以克服另一个标签。

Is there any way to calculate or to update automatic the label with the numbers so it will change its location according to the number of digits currently it has and according to the other label location so they wont get one over the other one ? 有什么方法可以计算或自动更新带有数字的标签,以便它会根据当前拥有的位数和另一个标签的位置来更改其位置,从而使它们彼此之间不会相互抵触?

My code in the constructor. 我的代码在构造函数中。

temperature_label = new Label();
    textMode_label = new Label();
    this.Controls.Add(textMode_label);
    this.Controls.Add(temperature_label);

    temperature_label.Location = new Point(260, 200);
    temperature_label.Height = 250;
    temperature_label.Width = 500;
    temperature_label.ForeColor = Color.Red;
    temperature_label.Font = new Font("Arial", 35, FontStyle.Bold);
    temperature_label.Text = "200c";

    textMode_label.Location = new Point(350, 200);
    textMode_label.Height = 250;
    textMode_label.Width = 500;
    textMode_label.ForeColor = Color.Blue;
    textMode_label.Font = new Font("Arial", 35, FontStyle.Bold);
    textMode_label.Text = "The Temperature Now Is - ";

Then in the timer tick event im updating the temperature_label with the values: 然后在计时器滴答事件中,im使用以下值更新temperature_label:

private void timer_Tick(object sender, EventArgs e)
        {
            Computer computer = new Computer();
            computer.Open();
            computer.GPUEnabled = true;

            foreach (var hardwareItem in computer.Hardware)
            {

                if (hardwareItem.HardwareType == HardwareType.GpuNvidia)
                {
                    foreach (var sensor in hardwareItem.Sensors)
                    {
                        if (sensor.SensorType == SensorType.Temperature)
                        {
                            sensor.Hardware.Update();

                            temperature_label.Text = sensor.Value.ToString()+ "c";
                            timer.Interval = 1000;
                            if (sensor.Value > 90)
                            {
                                Logger.Write("The current temperature is ===> " + sensor.Value);
                                button1.Enabled = true;
                            }
                            this.Select();
                        }
                    }
                }

            }     
        }

You could use a single lable instead of two and do a string.Format on the text. 您可以使用单个标签而不是两个标签,并在文本上执行string.Format。 Use the Tag-property of the label to hold the text you want to format and, after formatting, assign it as the labels text. 使用标签的标签属性保存要格式化的文本,格式化后将其分配为标签文本。

So something along the line of: 因此,遵循以下原则:

temperature_label = new Label();
this.Controls.Add(temperature_label);

temperature_label.Location = new Point(260, 200);
...
temperature_label.Tag = The Temperature Now Is - {0}C;
temperature_label.Text = "Fetching temperature...";

and then in the inner-loop of your tick-handler: 然后在滴答处理程序的内部循环中:

....

temperature_label.Text = string.Format(temperature_label.Tag, sensor.Value);

....      

This only works if there is no need to right align the temperature value, though. 但是,这仅在不需要正确对齐温度值的情况下有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM