0

I have a windows form that I can drag around programmatically created control labels. I am using the location property to determine each label position.

Each label is created as a preview with placeholder '#' where a number is to be. I want to know what the form coordinates are of a specific character in that label.

As a code example:

Label myLabel = new Label
            {
                Text = "Average: ##.#Unit",
                Name = "myLabelName"
            };

Controls.Add(myLabel);

...

// After label is dragged around
Point myLabelLocation = myLabel.Location;

This gives me the label's coordinates. Let's say I want similar coordinates, but I want them for the first #.

Another way to look at this, I "split" the label right before '#'. How do I get this location?

Praetir
  • 1
  • 3
  • You have added your new Controls to the Parent's `Controls` collection. You can get their reference back from there. -- You didn't specify when (in what context) / how (by `Name`, `Text`, other) you want to get the reference of one of your Controls. – Jimi Jul 07 '22 at 22:22
  • It's possible but not simple. You have to measure the string to the left of the `#` using `Graphics.MeasureString()`. You can then add the length to the coordinates of the label itself in order to determine the position of the `#`. See [this answer](https://stackoverflow.com/a/388964/2791540). – John Wu Jul 07 '22 at 22:33
  • @Jimi when is irrelevant. Before the label is moved, I can get the location of the specified character in reference to the whole label and add it to the whole label location later. Or after the label is moved, I can somehow get the character location. I am not sure what you mean by how. I just need to know the way to get the location at the specified character at any time. – Praetir Jul 07 '22 at 22:52
  • @JohnWu I appreciate the help. I think this will get me to where I need. Updates to come. – Praetir Jul 07 '22 at 22:53
  • Yes, well, you have to explain a little better what is the purpose of all this. Why do you need the coordinates of a piece of text inside a child Control is reference to the Parent Form? Try to describe what this procedure is for, in the end (what you're actually trying to achieve or what you're actually doing there). – Jimi Jul 07 '22 at 23:03
  • Each label is placed in a box that represents a display that will some statistics. The '#'s represents the places that a digit may appear. The labels shown are previews if the label has 3 digits (the maximum number of digits to be represented). There are various other labels with differing number of digits, so programmatically determining the locations of the digits and beyond is my best choice. The display in question is a small SPI display, and I need to know where to blank out the screen rather than have the whole screen keep blinking. – Praetir Jul 08 '22 at 01:01
  • Not clear. Are you programming a *software display* (in place of a hardware Serial Interface) that receives serial data from, say, an Arduino? If that's the case, are you using movable Labels to display the data? Are the Labels bound to a data source that is updated via serial coms? Why should the *screen keep blinking*? Did you double-buffer your Form? -- Are you trying to *determine the location of the digits* to do what exactly? Draw the values on the Form's surface? If this is the senario, it doesn't make much sense. – Jimi Jul 08 '22 at 01:59
  • The data is going to the Arduino with that handles a small display. The form shows a preview of what the display will show. – Praetir Jul 08 '22 at 16:54

0 Answers0