help w homework (usercontrol)
this is the task i got:
"Program starts with 2 forms: Dog and Cat (pink).
Each form has:
-arrUC: 2 yellow UserControl1s (each holds Labels).
-Labels: Min_Max_label (“Min/Max”), Image_Text_label (“Image/Text”), bySize_byBrightness_label (“bySize/byBrightness”).
-Rule: “Text” → “bySize”, “Image” → “byBrightness”.
-RadioButtons: Red / Green.
-MinMax_Result_label: small white label for results.
Each UserControl holds an array of Labels with random size, Name (Dog/Cat), and one of 8 defined styles (Dog/Cat, Red/Green, text or image).
Section A (Clicks 1 & 2):
On click → collect Labels into CatList / DogList.
Filter by chosen RadioButton color (background for text, image color for image).
Find Min/Max Label by Min_Max_label + criteria (bySize = area / byBrightness).
Show result in MinMax_Result_label on both forms.
Section B (Clicks 3 & 4):
As a continuation of Section A, all controls from two filtered UserControls
• Should be sorted according to the “by Size” or “by Brightness” text of bySize_byBrightness_label
• Should be displayed in the two UserControls selected by the mouse clicks.
Note: Sections can be combined so that after clicks, both the Min/Max and the sorted order are shown."
Im in section B and I got stuck at a part in my code because whenver i press on my usercontrol i need it to kind of align in one after the other whetere its byasize or bybrightness, but when i click on it just kind of gets arranged one on top of the other... can someone help (i attached a vid of what hppend)?
code : https://paste.mod.gg/zmpfmzbnlucm/0
BlazeBin - zmpfmzbnlucm
A tool for sharing your source code with the world!


65 Replies
how are you setting the position of the moved element?, are you keeping track of the element's width size when your moving and setting the position of the elements?
also this is winform right?
if its winform use something like a wrap panel to store ur elements when you move them, if not you have to keep track of all your element's size(width), like create a integer/float variable and each time you move a element, you add += (the element's width), and when you move and set the position of a element you use that float/int variable + (with a offset if u want (optional)), something like element.location = new (0, totalwidth + offset)
oh just realized it is a winform
use a wrap panel
and add ur element to that wrap panel so it aligns one after the other
What is it ?
it's a control like (text, button) but a container
oh wait
the wrap panel is in WPF, my bad u want the ' FlowLayoutPanel'
JFLHV
YouTube
C# - Add, update, delete flow layout panel controls at runtime
#csharp example to add, update and delete controls at runtime inside a flowlayoutpanel. A picturebox and labels are created to show a movie image, title and release date.
.cs files in video:
https://drive.google.com/drive/folders/1XI4zIjOwWX9ET2s-jjY__VXiBSqzn3Ff?usp=share_link
private void AddMovieToUI(Movie movie)
{
//Cr...

But I already did it in the code
@🕊 ILoveBirds 🕊
it seems the issue is with the label's width
it doesn't update when you write more text
u should set 'AutoSize' to true for the label
lemme just whip up some code

the above code creates 10 labels makes the font size a bit bigger and enables auto size for the font, sets a random text and aligns them one after the other
note(important) read the label's width after adding it to the win form

thats how i inserted it into my code
@🕊 ILoveBirds 🕊
but when i use the autoSize it makeall the image and texts i press on like this

are you refering to the horziontal spacing?, I used a winform to add my controls, is 'firstUC' a winform or another user control?
firstUC is a user control
hence the name
what kind of a user control?
is it like a panel/container that stores other elements?
its like the panel

those are the sec and fst UC's (w the pics inside of it
I tried to give chatGpt the code so he could try and help me fix it but he just said to changethe sizes of the pic like its gonna change anything
I wouldn't rely on chatGPT when it comes to coding, I tried that and it usually ends up giving errors and mistakes
yeah he just makes a mess but occasionally he helps
so ur using a panel right?
yea
not like a flowlayoutpanel?
just a panel?
i havent learned this so ig no
just to be sure click ur panel and in ur properties panel above u should see which panel ur using

no its not like a generic panel from winforms
its once i made of my own
and theure kinda acting like a panel
its two new usercontrols that i made
does ur custom class(panel) inherit from anything?
nah



wait is it a user control?
ya
oo one sec


seems to be working 4 me, my solution only keeps track of the widths to set position, are u by chance tracking the height too for setting the position
no i used only the x pos
i didnt notice that
i didnt think i would need it tho
could u show a picture of several items moved to the custom panel?
like when i click the pics
?
when u click the pics u move them to ur user control right?, yea a pic of that
u showed a picture b4


tahst after i did severl clicks
I'm a little confused about this picture since I see only one, do u want the label's (eg: the red background) to expand so its like a square? or is it the horizontal gap
yeah i was too all the pics just vanished after i clicked on someof them
it just kimd of i think shrinked it ? even tho i didnt do anything w the size
I tested with labels not pictures, do pictures have a auto size feature?
wait lemme check
idk
😭
lol
i dont think we learned this type of stuff on class but maybe we can use them after searching on the net
just tested with picture boxes

seems to be working 4 me, could u show/upload ur code
is it supposed to be all green
?
its just a picture for testing
does Form_event_FromUC executes one time?
also ur setting autosize to false in line 340 for the labels
ur removing the controls from the panel and re-adding them right?

if this method(Form_event_FromUC ) is called after u click an image to move it and add to ur user control panel, then I'm sorta 90% sure thats whats messing things up
I got explained that I’m not dupposed to press in the pics themselves, im supposed to press in the yellow user control in the back
Do I need to fix that part
Wdym ?
Yeah I should remove it it doesn’t do anything
like each time after u move a label to ur custom panel, do you call that method?, if not I'm pretty sure I know whats going wrong
so if ur not calling that method 'Form_event_FromUC ' every time after moving a label to ur custom panel
remember that your x variable only exists within ur function scope, so when you add a new item(label) to your panel ur not tracking the total width of all the labels, ur labels have auto size disabled (remember you need this set to true) to get the correct width but auto size only makes the label auto scale it self to fit its text, it won't work with image
so labels having a image you need auto size disabled and you have to manually scale it and read it's width
BlazeBin - yipfxzhchqcu
A tool for sharing your source code with the world!
thats a code one of my friends did and it worked
so see if that helps to understand the question cuz i feel like my codes a mess
@🕊 ILoveBirds 🕊
@0ri3lla- send me ur project folder instead I can fix the auto size issue but I think it would be best if u did ur assignment on ur own by observing ur friend's code
but again this channel is for helping so I could fix ur issue, I can leave comments so u will understand whats going on