C
C#2w ago
0ri3lla-

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!
No description
No description
65 Replies
🕊 ILoveBirds 🕊
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
0ri3lla-
0ri3lla-OP2w ago
What is it ?
🕊 ILoveBirds 🕊
it's a control like (text, button) but a container oh wait the wrap panel is in WPF, my bad u want the ' FlowLayoutPanel'
🕊 ILoveBirds 🕊
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...
0ri3lla-
0ri3lla-OP2w ago
No description
0ri3lla-
0ri3lla-OP2w ago
But I already did it in the code @🕊 ILoveBirds 🕊
🕊 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
🕊 ILoveBirds 🕊
// total width & offset
int totalWidthAndOffset = 0;


// loop
for (int i = 0; i < 10; i++)
{
// create a label & enable AutoSize
Label label = new Label();
label.Font = new Font(FontFamily.GenericSansSerif, 18);

label.AutoSize = true;

// set a random text
string[] strs = new string[] { "Cat", "Dog", "Bird" };
label.Text = strs[new Random().Next(0, strs.Length)];

// add control to winform
this.Controls.Add(label);


// set control location
label.Location = new Point(totalWidthAndOffset, 5);

// NOTE: very important, you should read the width after adding the control to your winform or panel
int widthLabel = label.Width;

// total width and offset
totalWidthAndOffset += widthLabel + 0;
}
// total width & offset
int totalWidthAndOffset = 0;


// loop
for (int i = 0; i < 10; i++)
{
// create a label & enable AutoSize
Label label = new Label();
label.Font = new Font(FontFamily.GenericSansSerif, 18);

label.AutoSize = true;

// set a random text
string[] strs = new string[] { "Cat", "Dog", "Bird" };
label.Text = strs[new Random().Next(0, strs.Length)];

// add control to winform
this.Controls.Add(label);


// set control location
label.Location = new Point(totalWidthAndOffset, 5);

// NOTE: very important, you should read the width after adding the control to your winform or panel
int widthLabel = label.Width;

// total width and offset
totalWidthAndOffset += widthLabel + 0;
}
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
0ri3lla-
0ri3lla-OP7d ago
No description
0ri3lla-
0ri3lla-OP7d ago
thats how i inserted it into my code @🕊 ILoveBirds 🕊
0ri3lla-
0ri3lla-OP7d ago
but when i use the autoSize it makeall the image and texts i press on like this
No description
🕊 ILoveBirds 🕊
are you refering to the horziontal spacing?, I used a winform to add my controls, is 'firstUC' a winform or another user control?
0ri3lla-
0ri3lla-OP5d ago
firstUC is a user control hence the name
🕊 ILoveBirds 🕊
what kind of a user control? is it like a panel/container that stores other elements?
0ri3lla-
0ri3lla-OP5d ago
its like the panel
0ri3lla-
0ri3lla-OP5d ago
No description
0ri3lla-
0ri3lla-OP5d ago
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
🕊 ILoveBirds 🕊
I wouldn't rely on chatGPT when it comes to coding, I tried that and it usually ends up giving errors and mistakes
0ri3lla-
0ri3lla-OP5d ago
yeah he just makes a mess but occasionally he helps
🕊 ILoveBirds 🕊
so ur using a panel right?
0ri3lla-
0ri3lla-OP5d ago
yea
🕊 ILoveBirds 🕊
not like a flowlayoutpanel? just a panel?
0ri3lla-
0ri3lla-OP5d ago
i havent learned this so ig no
🕊 ILoveBirds 🕊
just to be sure click ur panel and in ur properties panel above u should see which panel ur using
No description
0ri3lla-
0ri3lla-OP5d ago
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
🕊 ILoveBirds 🕊
does ur custom class(panel) inherit from anything?
0ri3lla-
0ri3lla-OP5d ago
nah
0ri3lla-
0ri3lla-OP5d ago
No description
0ri3lla-
0ri3lla-OP5d ago
No description
🕊 ILoveBirds 🕊
wait is it a user control?
0ri3lla-
0ri3lla-OP5d ago
ya
🕊 ILoveBirds 🕊
oo one sec
0ri3lla-
0ri3lla-OP5d ago
No description
🕊 ILoveBirds 🕊
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
0ri3lla-
0ri3lla-OP5d ago
C#
int x = 20, y = 5;//place

foreach (Label l in sortedCat)
{
l.AutoSize = false; // חשוב
l.Location = new Point(x, y);
firstUC.Controls.Add(l);

x += l.Width + 20;
}
C#
int x = 20, y = 5;//place

foreach (Label l in sortedCat)
{
l.AutoSize = false; // חשוב
l.Location = new Point(x, y);
firstUC.Controls.Add(l);

x += l.Width + 20;
}
no i used only the x pos i didnt notice that i didnt think i would need it tho
🕊 ILoveBirds 🕊
could u show a picture of several items moved to the custom panel?
0ri3lla-
0ri3lla-OP5d ago
like when i click the pics ?
🕊 ILoveBirds 🕊
when u click the pics u move them to ur user control right?, yea a pic of that
🕊 ILoveBirds 🕊
u showed a picture b4
No description
0ri3lla-
0ri3lla-OP5d ago
No description
0ri3lla-
0ri3lla-OP5d ago
tahst after i did severl clicks
🕊 ILoveBirds 🕊
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
0ri3lla-
0ri3lla-OP5d ago
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
🕊 ILoveBirds 🕊
I tested with labels not pictures, do pictures have a auto size feature? wait lemme check
0ri3lla-
0ri3lla-OP5d ago
idk 😭
0ri3lla-
0ri3lla-OP5d ago
i dont think we learned this type of stuff on class but maybe we can use them after searching on the net
🕊 ILoveBirds 🕊
just tested with picture boxes
🕊 ILoveBirds 🕊
seems to be working 4 me, could u show/upload ur code
0ri3lla-
0ri3lla-OP5d ago
is it supposed to be all green ?
🕊 ILoveBirds 🕊
its just a picture for testing
0ri3lla-
0ri3lla-OP5d ago
C#
C#
🕊 ILoveBirds 🕊
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?
🕊 ILoveBirds 🕊
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
0ri3lla-
0ri3lla-OP5d ago
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
🕊 ILoveBirds 🕊
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
0ri3lla-
0ri3lla-OP4d ago
BlazeBin - yipfxzhchqcu
A tool for sharing your source code with the world!
0ri3lla-
0ri3lla-OP4d ago
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 🕊
🕊 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

Did you find this page helpful?