Click here to Skip to main content
15,913,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

VB
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.


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

What I have tried:

VB
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
Posted
Updated 3-Jan-17 6:58am
v3

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
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:
 
Share this answer
 
v3
Comments
Vinod Kc 3-Jan-17 9:32am    
Hi, When i wrote code for changing location, i can't see any labels on SelectedPanel. The Labels which receive Double click event are vanishing from MainContainer, but not appearing in SelectedPanel. If i comment out that line of code(location change code), Labels are appearing in SelectedPanel, but they are maintaining a gap which i don't like.
OriginalGriff 3-Jan-17 9:55am    
So the next thing to do is find out exactly where it is putting them.
If you put a breakpoint on the "Location = NewLoc" line, what is the Point it is setting it to?
Vinod Kc 3-Jan-17 11:54am    
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.
OriginalGriff 3-Jan-17 12:16pm    
Answer updated
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900