如何从面板上删除标签并在运行时将其添加到另一个面板? [英] How can I remove a label from on panel and add it to another panel in runtime ?

查看:63
本文介绍了如何从面板上删除标签并在运行时将其添加到另一个面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的表格中有11个面板和15个标签。一个小组是MasterContainer,它包含了所有15个标签。其余10个小组准备包含这些标签。用户可以通过左键单击从这10个面板中选择任何一个面板。他可以双击每个标签。然后双击标签应移至选定的面板。这是我的伪代码。

- Label的双击事件

1.从MainContainer中删除点击的标签。

2.将其添加到选定的面板。

3.修复此标签的新位置点以适合所选面板。



但我无法做到第三步因为它不会给出任何结果。毕竟,如果我为新位置编写代码,那么移动的标签就会消失。但是,如果不编写位置更改代码,则会在新面板上显示少量标签。但并非所有点击的标签。我正在使用Select Case语句为移动的标签指定位置。

那是



Hi all,
I have 11 Panels and 15 Labels in my form. One Panel is MasterContainer and it cotains all the 15 Labels. Rest of the 10 Panels are ready to contain these labels. The user can select any one panel from these 10 Panels by left clicking it. And he can double click on each Label. Then the double clicked label should move on to the selected panel. This is my pseudo code.
- Label's double clicking event
1. Remove the clicked lable from MainContainer.
2. Add it to the selected panel.
3. Fix new location point for this label to fit on the selected panel.

But i can't do the third step because it won't give any results. After all, if i write code for new locations, then the moved label is vanishing. But if don't write code for location change, the few labels will appear on new panel. But not all clicked labels. I am using a Select Case statement for assign locations for the moved labels.
That is

Dim No_Of_Labels_In_Panel As Integer = SelectedPanel.Controls.Count
Select Case No_Of_Labels_In_Panel
	Case 0
		' assign location for first label
	Case 1
		' assign location for the second panel
	Case 3
		' Shrink other labels and make room for third label.





等等。

面板最多可包含7个标签。



我尝试了什么:





And so on.
A panel can contain up to 7 labels.

What I have tried:

Private Sub SelectedLabel_MouseDoubleClick(sender As Object, e As MouseEventArgs) 
        SelectedLabel = DirectCast(sender, Label)
        MainContainer.Controls.Remove(SelectedLabel)
        Dim NewLoc As Point
        No_Of_Cntrls_In_Panel = SelectedPanel.Controls.Count
        Select Case No_Of_Cntrls_In_Panel

            Case 0
                NewLoc = New Point(Me.SelectedPanel.Bounds.X + 5, Me.SelectedPanel.Bounds.Y + 5)
            Case 1
                NewLoc = New Point(Me.SelectedPanel.Bounds.X + 50, Me.SelectedPanel.Bounds.Y + 5)
            Case 2
                NewLoc = New Point(Me.SelectedPanel.Bounds.X + 5, Me.SelectedPanel.Bounds.Y + 50)
        End Select
        With SelectedLabel
            .Size = New Size(30, 30)
            .Location = NewLoc
        End With
        
        SelectedPanel.Controls.Add(SelectedLabel)
        SelectedPanel.SendToBack()
        SelectedLabel.BringToFront()
        Me.Refresh()
       
    End Sub

推荐答案

首先简化一下,然后试着看看究竟发生了什么:

Start off by simplifying things and try to see what exactly is happening:
Private Sub SelectedLabel_MouseDoubleClick(sender As Object, e As MouseEventArgs) 
    SelectedLabel = DirectCast(sender, Label)
    MainContainer.Controls.Remove(SelectedLabel)
    SelectedPanel.Controls.Add(SelectedLabel)
End Sub

现在,当您双击标签时,它将从主面板中移除并移动到另一个标签。

快速测试应该证明有效。

如果确实如此,那么问题在于你的代码的其余部分,并且几乎可以肯定这个位置是不可见的,或者大小意味着你看不到标签。没有你的其他代码,我们无法分辨 - 所以把尺寸放回去:

Now, when you double click a label, it will be removed from the main panel and moved to the other.
A quick test should prove that works.
If it does, then the problem is in the rest of your code, and is almost certainly the location is not visible, or the size means you can't see the label. Without your other code, we can't tell - so put the size bit back:

Private Sub SelectedLabel_MouseDoubleClick(sender As Object, e As MouseEventArgs) 
    SelectedLabel = DirectCast(sender, Label)
    MainContainer.Controls.Remove(SelectedLabel)
    SelectedLabel.Size = New Size(30, 30)
    SelectedPanel.Controls.Add(SelectedLabel)
End Sub

看看现在发生了什么。继续这样做,问题显而易见。





您好OriginalGriff,

感谢您的快速回复。但它并没有像我想象的那样奏效。我承认我需要更改字体大小以缩小标签,以便用户可以读取标签文本。现在,根据您的建议,我们并没有告诉该程序将标签放在特定位置。因此,一些标签显示在面板内。但有些人不是。奇怪的是,标签出现在面板中的方式与它们在MainContainer中出现的方式相同。相同的宽度和相同的顺序。




这就是重点。因为您只是移动标签,所以位置和大小不会改变 - 但它证明其余代码有效。正如我所建议的那样,尝试添加部分代码 - 或许改变大小 - 看看会发生什么。这样,您正在缩小原始代码的哪个部分导致问题。然后你可以解决这个问题,然后再尝试其余的代码。







因为我是自学者,所以我从未使用过BreakPoint。因为,我没有看到任何关于如何使用它的教程。相反,我使用Debug.WriteLine命令来查看那里发生的事情。在这种情况下,我将在面板的ControlAdded事件中执行相同的操作。如果我做错了什么,或者有没有机会改进我的调试方法,请纠正我。




这就是为什么成为自学者是一个非常糟糕的主意 - 你永远不会知道你缺少什么,因为你甚至不知道它存在!得到一本书 - 或者更好地继续学习 - 然后继续学习。它应该以一种结构良好的方式介绍所有内容,这样你就不会错过重要的东西。



调试器就像使用Debug.WriteLine一样 - 只有更好,没有你有添加任何声明。从Visual Studio中显示的方法开始,将光标放在第一行。转到菜单,然后选择Debug ... Toggle Breakpoint - 行的左侧会出现一个红点 - 这就是断点指示符,如果单击它,它就会消失。再次点击,它会回来。现在你知道了点的位置,这是切换断点的最快方法:单击该列,该行将获得断点。

现在在调试器中运行代码 - 通常,这只是一个案例按下F5,但菜单会告诉你:Debug ... Start Debugging会这样做,并告诉你分配了哪个键。

当你的应用程序到达断点时它会停止,并控制你。您可以通过将鼠标悬停在变量内容上来检查变量的内容,或者使用VS屏幕上输出区域的自动,本地和监视面板。而且你可以单步执行代码 - 这意味着你可以逐行执行它,以确切了解发生了什么以及它为什么会这样做。不仅如此,你可以在应用程序运行时更改代码,看看是否能解决问题!



它功能强大,功能强大 - 但这是基础知识和它应该足够长时间了(我几乎没有使用更高级的功能!)

试一试 - 你很快就会想到没有它你会做什么! :笑:

And see what happens now. Keep doing this, and it should become obvious where the problem is.


Hi OriginalGriff,
Thanks for the quick reply. But it is not working as i imagine. I am admitting that i need to change the font size in order to shrink the label so that user can read the label text. Now, as per your suggestion, we are not telling this program to put the label in a specific location. So some of the labels are displaying inside the panel. But some are not. And the strange thing is, the labels are appearing in the panel like the same way as they appear in the MainContainer. The same width from each other, and the same order.


That's the whole point. Because you are only moving the labels, the location and size are not changed - but it proves that the rest of the code works. So as I suggested, try adding back part of the code - change the size perhaps - and see what happens. That way, you are narrowing in on what part of your original code is causing the problem. You can then fix that, and try the rest of the code afterwards.


Hi,
Since i am a self learner, I never used BreakPoint yet. Because, i didn't see any tutorial on how to use it. Instead, i use a Debug.WriteLine command to see what happens there. In this context, i am going to do the same in the ControlAdded event of the panel. If i am doing something wrong, or is there any chance to improve my method of debugging, please correct me.


That's one reason why being a "self learner" is a very bad idea - you never know what you are missing, because you don't even know it exists! Get a book - or better go on a course - and follow it through. It should introduce everything in a well structured manner so you don't miss important stuff.

The debugger is like using Debug.WriteLine - only better, and without you having to add any statements. Start with your method on display in Visual Studio, and put the cursor on the first line. Go to the menu, and select "Debug...Toggle Breakpoint" - a red dot will appear at the left hand side of the line - that's the "breakpoint indicator" and if you click it, it will go away. Click again, and it will come back. Now you know where the dot goes, that's the quickest way to toggle a breakpoint: click that column and that line will get a breakpoint.
Now run you code in the debugger - normally, that's just a case of pressing F5, but the menu will tell you: "Debug...Start Debugging" will do it, and tell you which key is assigned to it.
When your application reaches a breakpoint it will stop, and hand control to you. You can examine to contents of variables by hovering the mouse over them, or use the "Auto", "Locals", and "Watch" panels of the Output area on the VS screen. And you can single step you code - which means you can execute it line by line to see exactly what happens and why it does what it did. Not only that, but you can change code while your app is running to see if that fixes a problem!

It's powerful, very powerful - but that is the basics and it should be enough for a very long time (I hardly use the more advanced features at all!)
Give it a try - you will soon wonder what the heck you did without it! :laugh:




感谢您的帮助。我从来没有读过这些东西。事实上,我从没想过像这样的事情确实存在。作为一个自学者,我主要依赖书本。但我找不到一本能告诉我们这类东西的书。我读过的每本书都告诉我写下你的代码并点击运行。所以我做到了。当我需要找出程序开始和结束之间发生的事情时,我依赖于Debug.WriteLine。我对此很满意,因为它将我想知道的内容写入输出控制台。当我学习基础知识时,我想我错过了这些东西。在那个时候,我不是一个敏锐的读者。谢谢你的提示。现在,来到这一点。我在位置变化线上添加了断点。这部分代码完美无缺。正如我想象的那样,它将标签移动到适当的位置。但标签被隐藏了。无论如何,现在我决定我将学习更多关于调试的知识。
Hi,
Thanks for helping me. I never read before these kind of stuff. Infact, i never imagine that things like this do exists. As a self learner, i mostly depend on books. But i can't find a book that tells us about these kind of stuff. Every books i read teold me that write your code and hit run. So i did it. And when i need to find out what is happening in between the program start and end, i depend on Debug.WriteLine. And i am happy with that, since it wrote what i want to know into output console. I think i missed this stuff when i learned basics. On that time, i was not a keen reader. Thanks for the tips. Now, coming to the point. I added breakpoint on the line of location change. That part of code is working perfectly. As i imagine, it move the label to appropriate positions. But the labels are being hidden. Anyhow, Now i decided that i will learn more about debugging.


这篇关于如何从面板上删除标签并在运行时将其添加到另一个面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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