-4

How could I align text dynamically to other controls?

I want to align texts under picture boxes:

enter image description here

EDIT: Here is my code: http://pasted.co/6347d9e4 but i didnt have any code, i coded these recently, and i dont understand why i get -6 ? (Sorry for bad english)

12
  • Take a look at the TableLayoutPanel. Commented Jul 27, 2016 at 6:26
  • You could wrap it into a Button is has the Property TextImageRelation where you can handle things like that. Commented Jul 27, 2016 at 7:15
  • @diiN_ I didn't try anything because i can't :\ Commented Jul 27, 2016 at 10:30
  • @Nudity Thanks but i dont wanna delete and replace all that thing, isn't there a way with code? Commented Jul 27, 2016 at 10:34
  • @TalhaTalipAçıkgöz can you provide some of you code ? Commented Jul 27, 2016 at 10:48

2 Answers 2

2

If it is necessary to group picture and text together I would create user control by combining PictureBox and Label. Text alignment can be implemented inside the control. Each TableLayoutPanel cell can be populated by this user control.

Sign up to request clarification or add additional context in comments.

3 Comments

And then for each control created, you can set the Picture source and text. I wouldn't recommend TableLayoutPanel for the control though. I would recommend using a label capable of wrapping the text to fit.
Thanks for help! But i have to replace all that picture boxes, isn't there any way with code?
I made a usercontrol but i cant still align though :\
0

So here's a quick and dirty example on how to get this done:

The UserControl:

public partial class ImageWithText : UserControl
{
    public ImageWithText(Image image = null, string champName = null)
    {
        InitializeComponent();
        if(image != null)
        {
            ChampionImage.Image = image;
        }
        if(!string.IsNullOrEmpty(champName))
        {
            ChamptionName.Text = champName;
        }
    }

    private void ImageWithText_Load(object sender, EventArgs e)
    {
        //Get the Width of the usercontrol:
        int ucWidth = this.Width;
        int centerPoint = ucWidth / 2;

        //Place text in the center:
        int labelWidth = ChamptionName.Width;
        int labelCenter = labelWidth / 2;
        //Set the lable to the centerPoint and then shift half of the lable to the left!
        //We leave the Top value untouched since we only want to set the HORIZONTAL ALIGNMENT
        ChamptionName.Left = centerPoint - labelCenter;
    }

    public void SetChampionImage(Image image)
    {
        ChampionImage.Image = image;
        ChampionImage.SizeMode = PictureBoxSizeMode.StretchImage;
        Application.DoEvents();
    }

    public void SetChampionName(string name)
    {
        ChamptionName.Text = name;
        Application.DoEvents();
    }
}

The Designer Code:

partial class ImageWithText
{
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Component Designer generated code

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.ChampionImage = new System.Windows.Forms.PictureBox();
        this.ChamptionName = new System.Windows.Forms.Label();
        ((System.ComponentModel.ISupportInitialize)(this.ChampionImage)).BeginInit();
        this.SuspendLayout();
        // 
        // ChampionImage
        // 
        this.ChampionImage.Location = new System.Drawing.Point(5, 5);
        this.ChampionImage.Margin = new System.Windows.Forms.Padding(5);
        this.ChampionImage.Name = "ChampionImage";
        this.ChampionImage.Size = new System.Drawing.Size(120, 120);
        this.ChampionImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
        this.ChampionImage.TabIndex = 0;
        this.ChampionImage.TabStop = false;
        // 
        // label1
        // 
        this.ChamptionName.AutoSize = true;
        this.ChamptionName.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.ChamptionName.Location = new System.Drawing.Point(33, 130);
        this.ChamptionName.Name = "label1";
        this.ChamptionName.Size = new System.Drawing.Size(73, 13);
        this.ChamptionName.TabIndex = 1;
        this.ChamptionName.Text = "SampleText";
        // 
        // ImageWithText
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.Transparent;
        this.Controls.Add(this.ChamptionName);
        this.Controls.Add(this.ChampionImage);
        this.Name = "ImageWithText";
        this.Size = new System.Drawing.Size(130, 150);
        this.Load += new System.EventHandler(this.ImageWithText_Load);
        ((System.ComponentModel.ISupportInitialize)(this.ChampionImage)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.PictureBox ChampionImage;
    private System.Windows.Forms.Label ChamptionName;

So when I apply this on a Window:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        //Create new Instance of UserControl:
        //Simple:
        ImageWithText champion = new ImageWithText();
        //Advanced (pass parameters in construcor):
        ImageWithText champion = new ImageWithText(Properties.Resources.Ashe, "Ashe");
        //Set Image and Name (if not set in constructor:)
        champion.SetChampionName("Ashe");
        champion.SetChampionImage(Properties.Resources.Ashe);
        //Add to Window:
        this.Controls.Add(chamption);
    }       
}

I get this result:

enter image description here

Riot Games API:

var request = (HttpWebRequest)WebRequest.Create("https://tr.api.pvp.net/api/lol/tr/v1.2/champion?api_key=<YourApiKey>");
var response = (HttpWebResponse)request.GetResponse();

var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
ChampionList list = Newtonsoft.Json.JsonConvert.DeserializeObject<ChampionList>(responseString);

use the class below: (from http://json2csharp.com/)

public class Champion
{
     public int id { get; set; }
     public bool active { get; set; }
     public bool botEnabled { get; set; }
     public bool freeToPlay { get; set; }
     public bool botMmEnabled { get; set; }
     public bool rankedPlayEnabled { get; set; }
}
public class ChampionList
{
     public List<Champion> champions { get; set; }
}

EDIT: For me it is working. I stored the reponse you got in a file.

enter image description here

10 Comments

That's what i've always wanted! Thank you!!
Np mate. We're here to help !
I've noticed that riot api method is better but; pasted.co/48621837 code won't work: Unexpected character encountered while parsing value: {. Path '', line 1, position 1
@TalhaTalipAçıkgöz this is because you are converting the output to <string>. Use json2csharp.com to get a class form the responses you get and then use JsonConvert.DeserializeObject<YourClass>to get it there. Note I recommend not to pose your apikey online :P next time better hide it ^^ it can be seen in the url in your code :D
@TalhaTalipAçıkgöz see my edit above !
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.