.net 如何系统地创建具有子对象的对象?

0sgqnhkj  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(126)

我已经创建了这个功能,创建标签在正确的位置与正确的文本.我使应用程序3840x1080,他们是不可见的也没有任何屏幕外的标签.如果我做的是完全不符合标准或直线上升愚蠢,请至少给我一个链接.

public void CreateTourPanel(string sittingArrangement,
            string fromTo, string departureTime, string duration, string cost)
        {
            Point holderPos = new Point(12, 208);

            Panel panelTemp = new Panel();
            panelTemp.Size = new Size(470, 71);

            panelTemp.Location = (new Point(19 + holderPos.X, 14 + (77 * (TourList.Count + 1)) + holderPos.Y));
            for (int i = 0; i < 5; i++)
            {
                Label labelTemp = new Label();
                switch (i)
                {
                    case 0:
                        labelTemp.Location = (new Point(27 + 19 + holderPos.X,
                            22 + 14 + (77 * (TourList.Count + 1)) + holderPos.Y));
                        labelTemp.Text = sittingArrangement;
                        labelTemp.Font = ChangeFontSize(labelTemp.Font, 14);
                        break;
                    case 1:
                        labelTemp.Location = (new Point(3 + 19 + holderPos.X,
                            46 + 14 + (77 * (TourList.Count + 1)) + holderPos.Y));
                        labelTemp.Text = fromTo;
                        labelTemp.Font = ChangeFontSize(labelTemp.Font, 15);
                        break;
                    case 2:
                        labelTemp.Location = (new Point(190 + 19 + holderPos.X,
                            1 + 14 + (77 * (TourList.Count + 1)) + holderPos.Y));
                        labelTemp.Text = departureTime;
                        labelTemp.Font = ChangeFontSize(labelTemp.Font, 20);
                        break;
                    case 3:
                        labelTemp.Location = (new Point(192 + 19 + holderPos.X,
                            32 + 14 + (77 * (TourList.Count + 1)) + holderPos.Y));
                        labelTemp.Text = duration;
                        labelTemp.Font = ChangeFontSize(labelTemp.Font, 14);
                        break;
                    case 4:
                        labelTemp.Location = (new Point(373 + 19 + holderPos.X,
                            5 + 14 + (77 * (TourList.Count + 1)) + holderPos.Y));
                        labelTemp.Text = cost;
                        labelTemp.Font = ChangeFontSize(labelTemp.Font, 20);
                        break;
                }
                panelTemp.Controls.Add(labelTemp);
            }
            panel3.Controls.Add(panelTemp);
            TourList.Add(panelTemp);
        }

        Font ChangeFontSize(Font font, float size)
        {
            return new Font(font.FontFamily, size, font.Style);
        }

字符串
我已经尝试用Contains代替Controls,用PointToScreen代替Location

s6fujrry

s6fujrry1#

由于panelTemplabelTemp对象的父对象,所以它们的Bounds矩形必须落在panelTempClientRectangle内。

public void CreateTourPanel(string sittingArrangement,
    string fromTo, string departureTime, string duration, string cost)
{
    Point holderPos = new Point(12, 208);

    Panel panelTemp = new Panel();
    panelTemp.Size = new Size(470, 71);

    panelTemp.Location = (new Point(19 + holderPos.X, 14 + (77 * (TourList.Count + 1)) + holderPos.Y));
    for (int i = 0; i < 5; i++)
    {
        Label labelTemp = new Label
        {
            BackColor = Color.LightBlue,
            AutoSize = true,
        };
        switch (i)
        {
            .
            .
            .
        }
        panelTemp.Controls.Add(labelTemp);
        Debug.WriteLine($"i={i}");
        Debug.WriteLine($"Panel={panelTemp.ClientRectangle} Label={labelTemp.Bounds}\n");
        Debug.WriteLine($"Panel contains labelTemp={panelTemp.ClientRectangle.Contains(labelTemp.Bounds)}");
        Debug.WriteLine($"Panel intersects labelTemp={panelTemp.ClientRectangle.IntersectsWith(labelTemp.Bounds)}");
    }
    panel3.Controls.Add(panelTemp);
    panelTemp.BackColor = Color.LightGreen;
    TourList.Add(panelTemp);
}

字符串


的数据
Y的计算结果并不接近。你最好把它们都设置为0,因为至少现在它们是可见的。


switch (i)
{
    case 0:
        labelTemp.Location = (new Point(27 + 19 + holderPos.X,
            0));
        labelTemp.Text = sittingArrangement;
        labelTemp.Font = ChangeFontSize(labelTemp.Font, 14);
        break;
    case 1:
        labelTemp.Location = (new Point(3 + 19 + holderPos.X,
            0));
        labelTemp.Text = fromTo;
        labelTemp.Font = ChangeFontSize(labelTemp.Font, 15);
        break;
    case 2:
        labelTemp.Location = (new Point(190 + 19 + holderPos.X,
            0));
        labelTemp.Text = departureTime;
        labelTemp.Font = ChangeFontSize(labelTemp.Font, 20);
        break;
    case 3:
        labelTemp.Location = (new Point(192 + 19 + holderPos.X,
            0));
        labelTemp.Text = duration;
        labelTemp.Font = ChangeFontSize(labelTemp.Font, 14);
        break;
    case 4:
        labelTemp.Location = (new Point(373 + 19 + holderPos.X,
            0));
        labelTemp.Text = cost;
        labelTemp.Font = ChangeFontSize(labelTemp.Font, 20);
        break;
}

相关问题