Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将新构造的控件传递给函数_C#_.net - Fatal编程技术网

C# 将新构造的控件传递给函数

C# 将新构造的控件传递给函数,c#,.net,C#,.net,我将通过以下方式向主控件添加新控件: Controls.Add(new ComboBox() { Text = "dsnfsdbfsdbfjsdbfsdjbfsmdfbsdbfsbf", Anchor = AnchorStyles.Left | AnchorStyles.Right, Width = DropDownWidth(/*Here should be smth. similar to "this" but for currently created combob

我将通过以下方式向主控件添加新控件:

Controls.Add(new ComboBox()
{
    Text = "dsnfsdbfsdbfjsdbfsdjbfsmdfbsdbfsbf",
    Anchor = AnchorStyles.Left | AnchorStyles.Right,
    Width = DropDownWidth(/*Here should be smth. similar to "this" but for currently created combobox*/)
});

public int DropDownWidth(ComboBox myCombo)
{
    int maxWidth = 0, temp = 0;
    foreach (var obj in myCombo.Items)
    {
        temp = TextRenderer.MeasureText(obj.ToString(), myCombo.Font).Width;
        if (temp > maxWidth)
        {
            maxWidth = temp;
        }
    }
    return maxWidth;
}
我想将新的组合框传递给函数并获得所需的宽度

是否有一些关键字类似于此,但对于我可以传递给函数的新创建的组合框

请不要采取变通办法!我知道我可以先创建组合框,用属性填充它,然后在下一步添加到控件。 现在只有简短的形式有趣


谢谢大家!

您无法将其传递给函数,因为它还不存在

例如@J.Steen:

    public class CustomCombo : System.Windows.Forms.ComboBox
{
    private int _width;

    public int Width
    {
        get { return _width; }
        set { _width = value; }
    }


    public CustomCombo()
    {
        _width = getWidth(this);
    }
    public int getWidth(System.Windows.Forms.ComboBox combo)
    {
        //do stuff
        return 0;
    }
}

无法将其传递给函数,因为它尚不存在

例如@J.Steen:

    public class CustomCombo : System.Windows.Forms.ComboBox
{
    private int _width;

    public int Width
    {
        get { return _width; }
        set { _width = value; }
    }


    public CustomCombo()
    {
        _width = getWidth(this);
    }
    public int getWidth(System.Windows.Forms.ComboBox combo)
    {
        //do stuff
        return 0;
    }
}

不可以。在实际创建对象之前,不能使用对象的引用,从技术上讲,它不在对象初始化器中,因为它是创建语句的一部分。在这种情况下,需要“变通办法”

类似于

var myTextArray = new[] { "Hi", "ho", "Christmas" }

Controls.Add(new ComboBox()
{
    Text = "dsnfsdbfsdbfjsdbfsdjbfsmdfbsdbfsbf",
    Anchor = AnchorStyles.Left | AnchorStyles.Right,
    Width = DropDownWidth(myTextArray, this.Font)
});
public int DropDownWidth(object[] objects, Font font)
{
    int maxWidth = 0, temp = 0;
    foreach (var obj in objects)
    {
        temp = TextRenderer.MeasureText(obj.ToString(), font).Width;
        if (temp > maxWidth)
        {
            maxWidth = temp;
        }
    }
    return maxWidth;
}
…其中,
当然是您的
表单
或其他父控件

修改后的
DropDownWidth
方法的内容如下:

var myTextArray = new[] { "Hi", "ho", "Christmas" }

Controls.Add(new ComboBox()
{
    Text = "dsnfsdbfsdbfjsdbfsdjbfsmdfbsdbfsbf",
    Anchor = AnchorStyles.Left | AnchorStyles.Right,
    Width = DropDownWidth(myTextArray, this.Font)
});
public int DropDownWidth(object[] objects, Font font)
{
    int maxWidth = 0, temp = 0;
    foreach (var obj in objects)
    {
        temp = TextRenderer.MeasureText(obj.ToString(), font).Width;
        if (temp > maxWidth)
        {
            maxWidth = temp;
        }
    }
    return maxWidth;
}

不可以。在实际创建对象之前,不能使用对象的引用,从技术上讲,它不在对象初始化器中,因为它是创建语句的一部分。在这种情况下,需要“变通办法”

类似于

var myTextArray = new[] { "Hi", "ho", "Christmas" }

Controls.Add(new ComboBox()
{
    Text = "dsnfsdbfsdbfjsdbfsdjbfsmdfbsdbfsbf",
    Anchor = AnchorStyles.Left | AnchorStyles.Right,
    Width = DropDownWidth(myTextArray, this.Font)
});
public int DropDownWidth(object[] objects, Font font)
{
    int maxWidth = 0, temp = 0;
    foreach (var obj in objects)
    {
        temp = TextRenderer.MeasureText(obj.ToString(), font).Width;
        if (temp > maxWidth)
        {
            maxWidth = temp;
        }
    }
    return maxWidth;
}
…其中,
当然是您的
表单
或其他父控件

修改后的
DropDownWidth
方法的内容如下:

var myTextArray = new[] { "Hi", "ho", "Christmas" }

Controls.Add(new ComboBox()
{
    Text = "dsnfsdbfsdbfjsdbfsdjbfsmdfbsdbfsbf",
    Anchor = AnchorStyles.Left | AnchorStyles.Right,
    Width = DropDownWidth(myTextArray, this.Font)
});
public int DropDownWidth(object[] objects, Font font)
{
    int maxWidth = 0, temp = 0;
    foreach (var obj in objects)
    {
        temp = TextRenderer.MeasureText(obj.ToString(), font).Width;
        if (temp > maxWidth)
        {
            maxWidth = temp;
        }
    }
    return maxWidth;
}


另外,由于
Items
集合(在您的代码片段中)非常空,因此该代码将返回
0
。在适当的情况下,您希望如何使用此选项?=)@现在它是空的,这不是完整的代码,只是想知道在这种情况下是否有类似“this”的东西:)您可以编写自己的ComboBox类,然后在构造函数中调用设置宽度的方法。此时,对象将存在,并且您将能够设置所需的所有属性wish@EricFrick我很确定构造函数是在对象初始化器之前调用的。这意味着,在对象初始化器中添加的任何项都不存在。@J.Steen我知道,但是在构造函数中,您可以将宽度值设置为私有字段,然后使用指向该字段的公共属性将其公开。此外,由于
集合(在您的代码段中)非常空,因此该代码将返回
0
。在适当的情况下,您希望如何使用此选项?=)@现在它是空的,这不是完整的代码,只是想知道在这种情况下是否有类似“this”的东西:)您可以编写自己的ComboBox类,然后在构造函数中调用设置宽度的方法。此时,对象将存在,并且您将能够设置所需的所有属性wish@EricFrick我很确定构造函数是在对象初始化器之前调用的。也就是说,在对象初始值设定器中添加的任何项都不存在。@J.Steen我知道,但在构造函数中,您可以将宽度值设置为私有字段,然后使用指向该字段的公共属性将其公开。啊,是的,这就是我认为您的意思。但是GetWidth()方法需要使用组合框的项。如果它们还没有被分配(因为对象初始化器发生在构造函数之后),这无论如何都不会起作用@Steen,将字符串数组作为构造函数参数传递可能会起作用:)@VladL-Sure,Sure。现在你说话了。但您也可以轻松地对原始代码段执行此操作,只要您还传递了字体对象即可请参见编辑。在我的示例中,这是指初始化的对象,因此它将work@J.Steen我可以很容易地做任何我想做的事情,如果我确信,这个物体不是在需要的时刻被创造出来的,是的,这就是我认为你的意思。但是GetWidth()方法需要使用组合框的项。如果它们还没有被分配(因为对象初始化器发生在构造函数之后),这无论如何都不会起作用@Steen,将字符串数组作为构造函数参数传递可能会起作用:)@VladL-Sure,Sure。现在你说话了。但您也可以轻松地对原始代码段执行此操作,只要您还传递了字体对象即可请参见编辑。在我的示例中,这是指初始化的对象,因此它将work@J.Steen我可以很容易地做任何我想做的事情,如果我确信,那个物体不是在需要的时刻被创造出来的。这是一个非常好的主意,我希望在这里找到,并且对负面反应感到好奇。非常感谢。这是一个非常好的主意,我希望在这里找到,并想知道负面反应。非常感谢。