C# C-将文本与代码动态对齐

C# C-将文本与代码动态对齐,c#,winforms,C#,Winforms,如何将文本与其他控件动态对齐 我想对齐图片框下的文本: 编辑:这是我的代码:但我没有任何代码,我最近编写了这些代码,我不明白为什么我得到-6?抱歉,英语不好如果有必要将图片和文本分组在一起,我将通过结合PictureBox和Label创建用户控件。文本对齐可以在控件内部实现。每个TableLayoutPanel单元格都可以由此用户控件填充。因此,下面是一个关于如何完成此操作的快速而肮脏的示例: 用户控件: 设计器代码: 我得到这个结果: 防暴游戏宣传短片: 使用下面的类:from 编辑: 对我来

如何将文本与其他控件动态对齐

我想对齐图片框下的文本:


编辑:这是我的代码:但我没有任何代码,我最近编写了这些代码,我不明白为什么我得到-6?抱歉,英语不好

如果有必要将图片和文本分组在一起,我将通过结合PictureBox和Label创建用户控件。文本对齐可以在控件内部实现。每个TableLayoutPanel单元格都可以由此用户控件填充。

因此,下面是一个关于如何完成此操作的快速而肮脏的示例:

用户控件:

设计器代码:

我得到这个结果:

防暴游戏宣传短片:

使用下面的类:from

编辑: 对我来说,它正在发挥作用。 我把你得到的回复储存在一个文件里


看看TableLayoutPanel。你可以把它包装成一个按钮,它的属性是TextImageRelation,你可以在这里处理类似的事情。@diiN\ui我没有尝试任何东西,因为我不能:\@裸体谢谢,但我不想删除和替换所有这些东西,有没有办法使用代码?@TalhaTalipAıkgöz您能提供一些代码吗?然后,对于创建的每个控件,您可以设置图片源和文本。不过,我不建议将TableLayoutPanel用于控件。我建议使用一个能够包装文本的标签。谢谢你的帮助!但是我必须替换所有的图片框,难道没有办法使用代码吗?我做了一个用户控件,但我仍然无法对齐:\n这是我一直想要的!非常感谢。Np配偶。我们是来帮忙的!我注意到riot api方法更好,但是;代码无法工作:分析值时遇到意外字符:{.路径,第1行,位置1@TalhaTalipAçıkgöz这是因为您正在将输出转换为。使用从您获得的响应中获取类,然后使用JsonConvert.DeserializeObjects将其获取。请注意,我建议不要将apikey置于联机状态:P下次最好隐藏它^^^可以在代码的url中看到:D@TalhaTalipAçıkgöz见我的编辑上面!
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();
    }
}
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;
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);
    }       
}
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);
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; }
}