Resolved How pass a String from a Array to a Methode as - private void Label_1_Paint(object sender, PaintEventArgs e) to verticalize the Label

Ivo

Member
Joined
Feb 8, 2022
Messages
18
Programming Experience
Beginner
How to pass the array string to that paint methode:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using System.IO;


namespace SignalWatch
{
    public partial class frmMain : Form
    {

        public frmMain()
        {
            InitializeComponent();
            
        }

string[] IP = new string[40];


private void frmMain_Load(object sender, EventArgs e)
        {
            LoadIPdata();
        }

        private void LoadIPdata()
        {
            for (int i = 1; i < 40; i++)
            {
              IP[i] = File.ReadLines("IP.txt").Skip(i - 1).First(); // How to pass the IP[string from Arry] to the
          //methode private void Label_1_Paint(object sender, PaintEventArgs e) ????? to put in to e.Graphics.DrawString(IP[0], Myfont, Mybruhs, 0, 0);
            }
            return;
        }


        private void Label_1_Paint(object sender, PaintEventArgs e)
        {
                Font Myfont = new Font("Sans Serif", 9);
                Brush Mybruhs = new System.Drawing.SolidBrush(System.Drawing.Color.White);
                e.Graphics.TranslateTransform(3, 57);
                e.Graphics.RotateTransform(-90);
                e.Graphics.DrawString(IP[0], Myfont, Mybruhs, 0, 0);
        }
 
You can't pass anything to an event handler because you don't call the event handler. What you need to do is to put the required data somewhere and then the code in the event handler gets it from there. That would most like be a private field.

By the way, ALWAYS dispose objects that support it when you are done with them. That means your Font and your SolidBrush objects. You should create such objects with a using statement, in which case the will be disposed implicitly.
 
You can't pass anything to an event handler because you don't call the event handler. What you need to do is to put the required data somewhere and then the code in the event handler gets it from there. That would most like be a private field.

By the way, ALWAYS dispose objects that support it when you are done with them. That means your Font and your SolidBrush objects. You should create such objects with a using statement, in which case the will be disposed implicitly.
Please, can you show a small example about what you mean ?
You can't pass anything to an event handler because you don't call the event handler. What you need to do is to put the required data somewhere and then the code in the event handler gets it from there. That would most like be a private field.
 
What you're doing does really make sense anyway. That's not how you use the Paint event. How about you do what you should have done in the first place and provide a FULL and CLEAR explanation of what you're trying to achieve and then we can explain the best way to achieve it, because what you're doing isn't it. A title and some code is NOT an acceptable question. ALWAYS describe the issue in detail in the post and then summarise that in the title.
 
What you're doing does really make sense anyway. That's not how you use the Paint event. How about you do what you should have done in the first place and provide a FULL and CLEAR explanation of what you're trying to achieve and then we can explain the best way to achieve it, because what you're doing isn't it. A title and some code is NOT an acceptable question. ALWAYS describe the issue in detail in the post and then summarise that in the title.
Hi, i understand what you mean, but please, im not a pro coder, i need help . I want to get a string out of a array, passing to a methode, where i use the paint methode to become vertical text, but the string will varry, so as example, a random pick from out a array of strings passing to that vertical label.

Its just to know how to pass that string, or another way to do it.

I userd now the Dispose() to clear the font and brushes

on my pic you can see the vertical labels, but thay will change value ( string[0] ), thats why i try to get thet string in that methode, what not seems to be possible.

Thanks for your time, and hope you understand what im looking for

I just try to learn from the mistakes etc....

Regards from Belgium ( EU )
Ivo Donckers
 

Attachments

  • Meter.png
    Meter.png
    3.8 KB · Views: 6
im not a pro coder
The request for a clear explanation of what your objectives/goals are has nothing to do if you are an experienced programmer or not. It has to do with communicating what you are trying to achieve. Often, people (specially programmers of any experience level) get stuck in the X-Y problem where they are latched into asking how to solve X, when the actual problem they have is Y, and there are better ways to solve Y without using X. This is why it often pays dividends to step back and clear explain what you are trying to achieve in terms of the forest, instead of trying to deal with the trees.
 
The request for a clear explanation of what your objectives/goals are has nothing to do if you are an experienced programmer or not. It has to do with communicating what you are trying to achieve. Often, people (specially programmers of any experience level) get stuck in the X-Y problem where they are latched into asking how to solve X, when the actual problem they have is Y, and there are better ways to solve Y without using X. This is why it often pays dividends to step back and clear explain what you are trying to achieve in terms of the forest, instead of trying to deal with the trees.
OK,

you understand now what i try to achieve ?
 
As mentioned above, you should not call the Paint event handler directly, so there is no way to pass in a string directly to it. It's the system that fires the Paint event when the area under the control needs to be rendered. And even if you could invoke any kind of event handler, the way you would normally pass parameters to the event handler is through the second parameter of the handler which takes an EventArgs (or derived type). The PaintEventArgs doesn't have any room in it for you to pass in a string.

Anyway, the Label class has a public Text property. Just set the Text value of the string that you want to be rendered. That is the correct way to pass a string to a Label. Your paint event handler can read that property when it gets invoked.

So the issue you have though, is that your Paint event handler will likely be called by the default Label Paint event handler so you'll have the text rendered normally horizontally first, and then when your Paint event handler is called, you'll also render the text rotated 90 degrees (and apparently offset and enlarged if I'm reading your call to TranslateTransform() correctly.

What you need to do is not use a standard Label. The absolutely most correct object-oriented thing to do is write a custom class derived from the Label. Call it a RotatedLabel if you want. In your derived class, you should override the Paint() method of the class, not call the base implementation, and instead use your own painting code.

A cheat that you could potentially perform without following object-oriented principles, is instead use a Panel instead of a Label. You can then simply attach your paint event handler to the Panel's Paint event. Since the Panel also has a Text property, you can still pass in your string that way and pick it up in your event handler. Additionally, the Panel's default Paint event handler doesn't draw the Text value, you don't have to contend with the horizonally rendered text.

So that's how you pass in values to a control where it so happened that an exposed property's name actually fits perfectly with your needs. But going back to the RotatedLabel, what if you wanted to be able to specify the actual angle of rotation? In that case, your derived class should expose a new property called Rotation, and again your Paint() method override should now read that property along with Text property when rendering stuff. The nice thing about is that this approach scales up to when you need more than one thing to be passed on to the control. Just add more properties.

Okay, but what if you are feeling cheap and/or don't believe in this object-oriented thing? Then this is where Tag property that ever WinForms control has comes into play. You can put anything into the Tag property, and your event handlers can later read those values (and also change them if needed). So like in the example above, you could put the rotation angle into Tag. If you have more than one value to pass in, then you'll need to create a class (or struct) that holds all the things that you want to pass in. Instantiate this class, and store the reference in the Tag.
 
In another thread you said that you are an autodidact. I recommend learning from Petzold's book "Programming Windows". Pick up the edition that covers WinForms. Also learn from the books "Effective C#" and "More Effective C#". And for general problem solving approaches, the second edition of "Design Patterns Explained" will open up your view to design patterns used to solve common programming problems that are seen in not only C# programming, but everywhere else as well.

Additionally, I highly recommend breaking your current habit of using Hungarian notation for your programming. The .NET (Framework and Core)'s naming convention actually recommends eschewing the use of Hungarian.
 
Back
Top Bottom