动态和交互式面板 [英] Dynamic and Interactive Panels

查看:59
本文介绍了动态和交互式面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我需要创建一个面板数组,并在其中每个(panel [i])中添加X控件名称.到现在为止没有问题,问题是我需要在该面板[i]中添加一个按钮,单击该按钮将当前面板更改为面板[i + 1].而且我真的不知道如何处理点击事件.

这是一些使它更清晰的代码


Hello,

I need to create a Array of panel and add in every one of them (panel[i]) an X nombre of controls . Till now no problem, The problem is i need to add to that panel[i] a button that when clicked change the current panel to the panel [i+1]. and i really don''t know how to work on it click event .

here is some code to make it more clear


Panel[] panel = new Panel[5];
           int x = 0;

               for (int j = 0; j < 5; j++)
               {
                   panel[j] = new Panel();
               }
          while (x<5){

               this.Controls.Add(panel[x]);
               panel[x].Visible = true;
               panel[x].BringToFront();
               panel[x].AutoScroll = true;
               panel[x].AutoScrollMargin = new Size(30, 30);
               panel[x].Dock = System.Windows.Forms.DockStyle.Fill;
               panel[x].Location = new System.Drawing.Point(0, 0);
               panel[x].Name = "panel" + x;
               MessageBox.Show("Panel :" + panel[x].Name);
               int i;
               for (i = 0; i < 20; i++)
               {
                   Label l1 = new Label();
                   l1.Text = "position " + (i + 1) + "/" + x;
                   l1.Location = new System.Drawing.Point(28, 28 + i * 30);
                   l1.Size = new System.Drawing.Size(80, 13);
                   TextBox t1 = new TextBox();
                   t1.Name = "pos" + (i + 1);
                   t1.Location = new System.Drawing.Point(110, 28 + i * 30);
                   t1.Size = new System.Drawing.Size(80, 13);
                   panel[x].Controls.Add(l1);
                   panel[x].Controls.Add(t1);
               }
               Button b1 = new Button();
               b1.Text= "valider et ajouter";
               b1.Name= "button"+x;
               b1.Location = new Point(269, 28 + i * 30);
               b1.Size = new Size(100, 30);
               panel[x].Controls.Add(b1);
               int current = x;
              // I need to put the event for the button here but I don't know how
              x++;




我希望我有道理.

请帮助我,如果您有解决此问题的想法




I hope that I making sense .

Please help me , if you have an idea how to solve this issue

推荐答案

<
     Salaam Aleikum,SalouaK(希望如此),欢迎加入CP QA.

    太长了,太详细了,无法对OP进行评论,但是我将推迟给予
   一个示例,说明如何有效且轻松地完成SalouaK在这里要做的事情直到他展示
   在这里对我的详细分析做出回应的一些主动行动
.

   用英语来说,适合此类条件"以进行进一步投资的惯用法是:
   在游戏中放置皮肤." [ ^ ]
>


请考虑:

0.根据您在此处显示的代码,我们只能幻想 panel1是什么,它的创建位置,还有什么可能在将新的运行时创建的控件粘贴到其中之前,请先按住..

panel1是否是要将所有运行时创建的面板添加到ControlCollection的容器,而不是"this"?

Occam的Razor建议 是最可能的答案,并且有一种更好的方式来挂起(提示,提示)panel1的运行时外观.继续创建所有这些东西,如果确实有必要...就像在Form Load EventHandler外部在运行时随意地执行它,以响应UI中的某些用户操作时一样.

1. 主要缺陷:您在运行时创建的所有面板都被添加到"this by
<
    Salaam Aleikum, SalouaK (hope that''s appropriate), and welcome to CP QA.

    This is much too long, and detailed, to be a comment on the OP, but I am going to hold off on giving
    an example of how to accomplish what SalouaK wants to do here efficiently and easily until he shows
    some initiative in responding to my detailed analysis here
.

    In English, the idiom that is appropriate for this type of "conditionality" for further investment is:
    "putting skin in the game."[^]
>


Please consider:

0. Based on the code you''ve shown us here: we can only fantasize what panel1 is, where it''s created, what else it may hold before you stick your new run-time created Controls into it..

Is panel1 meant to be the container that you will add all the run-time created Panels to the ControlCollection of: not "this" ?

Occam''s Razor suggests that is the most likely answer, and there is a better way to suspend (hint, hint) panel1''s run-time appearance while you go about creating all this stuff, if that''s really necessary ... like when it''s done outside the Form Load EventHandler, arbitrarily, at run-time, in response to some user action in the UI.

1. major flaw: all your run-time created Panels are added to the ControlCollection of ''this by
this.Controls.Add(panel[1]);

好"的ControlCollection中,在这种情况下,面板是您的变量包含五个元素的面板数组的名称(如果希望将来其他人理解您的代码,则"panelAry"或"ArrayOfPanels"将是此变量名称的更合适的选择).另请参见下面的评论3.

但是:

    a.请记住,当您创建面板数组时:类型Panel []''panel的变量最初包含5个空条目.

    b.因此,每次执行时:Controls.Add(panel [1]:当``x == 0:一次,当" x == 0时,您添加了一个"null"在面板[1]中插入了一个新的面板:然后:

    c.对于''x> = 1的每次迭代,您将把 panel [1]重新添加到``this!"的ControlsCollection中. 这不会导致运行时错误,但是:只有一个,并且只有一个实例"panel [1]添加到此" ControlCollection中!

    〜除了:我实际上想看看如果重新添加相同的内容,Visual Studio会抛出运行时错误
     某些控件的实例到ControlCollection中,或添加"null! 〜


显然,您要做的是使用for循环中的索引将运行时面板添加到目标"容器中:

Okay, ''panel in this case is your variable name for a five-element array of Panels (''panelAry or ''ArrayOfPanels would be much more appropriate choices for this variable name, if you want other people to understand your code in the future). Also see comment #3 below.

But:

    a. remember that when you create your array of Panels: your variable of type Panel[] ''panel, initially, contains 5 null entries.

    b. so every time you execute: Controls.Add(panel[1]: you add a ''null, when the value of ''x == 0: once the value of ''x == 1: when you''ve actually inserted a new Panel into panel[1]: then:

    c. for each iteration where ''x >= 1, you will be re-adding panel[1] to the ControlsCollection of ''this ! This will not cause a run-time error, but: there will be one, and only one, instance of ''panel[1] added to this'' ControlCollection !

    ~ aside: I''d actually like to see Visual Studio throw a run-time error if you re-add the same
    instance of some Control to a ControlCollection, or add a ''null ! ~


Clearly, what you want to do is to add your run-time Panel to the "target" container using the index in your for-loop:

this.Controls.Add(panel[x]);

请注意,如果您这样做,正如我所怀疑的那样,确实打算将"panel1设置为所有新的运行时创建的面板及其标签/文本框对的容器:那么您想要看到类似

note that if you do, as I suspect, really intend ''panel1 to be the container for all the new run-time created Panels and their Label/TextBox pairs: then you''d want to see something like

panel1.Controls.Add(panel[x]);

2的东西.我们可以看到您添加了每个新的运行时在"this:"的ControlCollection中创建了Panel,但是如果在"this:"的ControlCollection中已经存在其他控件,则在该ControlCollection中运行时创建的Panel的Index属性将由多少个其他Control确定其中:以后是否会为您造成问题:

如果您这样做,如我所料,打算将"panel1用作所有新的运行时创建的面板及其标签/文本框对的容器:那么只要",这不是问题. panel1中没有其他控件..

3.最令人困惑的是:每次执行for循环时,都将Panel的"Visible"属性"panel1"设置为"false".您只需使其一次不可见即可:)

4.您在此处使用的固定值既用于创建的Panel的数量,又用于在创建的每个Panel中添加的Label/TextBox对"的数量:您不是真的要使代码更灵活通过使用变量来包含面板和Label/TextBox对的数量?

好吧,好的,也许永远是5个面板和20个Label/TextBoxes:没问题:)

5.因为您将Dock属性设置为" DockStyle.Fill,所以在运行时创建的所有面板,并且您添加到任何容器的ControlCollection中的面板都将在其容器控件中彼此重叠显示",因此在面板之间导航"可能要求代码略有不同:

记住:将控件添加到ControlCollection并设置``Dock =''DockStyle.Fill:第一个添加的控件将显示在顶部;最后添加的是在底部(默认顺序是从前到后):如果您要添加的最后一个在顶部,而第一个在底部(在最后的顺序):添加控件后使用'BringToFront.

如果不想从面板到面板连续导航,并根据某些用户操作选择放置在面板顶部的那一个,则无关紧要.



现在:如何更轻松地完成此操作:SalouaK尽快对我在这里提出的问题以及我在您的代码中指出的问题做出明确的答复:

我很乐意为您提供进一步的帮助.

最好,比尔

2. we can see you add each new run-time created Panel to the ControlCollection of ''this: but if there are other Controls already in the ControlCollection of ''this: then the Index property of your run-time created Panels in that ControlCollection is going to be determined by how many other Controls are in it: is that going to create a problem for you later:

If you do, as I suspect, intend for ''panel1 to be the container for all the new run-time created Panels, and their Label/TextBox pairs: then that''s not an issue as long as ''panel1 has no other Controls in it..

3. most confusing of all: you set the Visible property of the Panel, ''panel1, to ''false every time you execute the for-loop. You only to make it not visible once :)

4. you are using fixed values here both for the number of Panels created, and then the number of Label/TextBox "pairs" added into each Panel created: don''t you really want to make your code more flexible by using variables to contain the numbers of Panels, and Label/TextBox pairs ?

Well, okay, maybe it will always be 5 Panels and 20 Label/TextBoxes: no problem :)

5. because you set the Dock property to ''DockStyle.Fill, all Panels created at run-time, and that you add to whatever container''s ControlCollection will appear "on top" of each other in their container Control, so "navigating" from Panel to Panel may require slight differences in code:

remember: as you add Controls to a ControlCollection, and set ''Dock = ''DockStyle.Fill: the first added will appear on top; the last added is at the bottom (default order is first to last): if you want the last added to appear on top, and the first on the bottom (last to first order): use ''BringToFront after you add the Control.

If do not want continuous navigation in-order from Panel to Panel, and are choosing which one to put on top based on some user action: this is irrelevant.

~

Now: how to do this much more easily: as soon as you, SalouaK, give some clear response to the questions I''ve asked here, and the issues I''ve pointed out in your code:

I''ll be happy to help you further.

best, Bill




要实现您的解决方案,您需要做两件事,首先是创建Button控件并将其添加到您的特定面板中.其次,您需要为每个按钮创建按钮单击事件.在单击事件中,您需要获取该控件并将其从其所属的面板中删除,然后在表单的下一个直接面板中添加.
Hi,

To achieve your solution two things you need to do, First is to create Button control and add it in your particular Panel. Secondly you need to create button click event for each and every button. In click event you need to get that control and remove it from the panel that it belongs and Add in the immediate next panel in your form.
yourButton.Click += new EventHandler(yourButton_Click);

private void yourButton_Click(Object sender, EventArgs e)
{
// Here sender is your button control. 
// Remove Controls from (i)th Panel.
Panel[i].Controls.Clear();
// Add Button control in (i+1)th Panel.
Panel[i+1].Controls.Add(sender as Button);
}


希望它对您有用,
谢谢
-Amit Gajjar


Hope it will work for you,
Thanks
-Amit Gajjar


使用lambda表达式/匿名方法添加内联事件处理程序.

不要忘记捕获相关变量的当前值(作为lambda捕获变量而不是值,即,如果捕获"x",则在单击按钮时将为5)

示例(在按钮创建下方添加)
Use lambda expressions / anonymous method to add inline event handler.

Don''t forget to capture current value of relevant variable (as lambda capture variable and not value, i.e. if it captures ''x'' it will be 5 when the button is clicked)

example (to add below the button creation)
int current = x; // WARNING: don't use x! You have been warned!
b1.Click += delegate {
  for (int i=0; i<panels.length;>  panel[(current+1)%panel.Length].Visible = true;
}


这篇关于动态和交互式面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆