How to control the space between two labels during run time - c#

Im using Visual Studio 2015 Community C#.
I have two labels on a Windows form suppose Label1 and Label2.
These labels will get filled up with user input namely first name and last name.
How to put even space between them so that during runtime the first name doesn't over lap the last name.
AbrahLincoln Abraham Lincoln
(Label1^)(^Label2) (^Label1) (^Label2)
For example: how to make this ^ INTO that >>>>>>>>>>>>^^
Because if I put space in the Form Design before runtime then for other names It will come like this: John(unnecessary space)Doe
Hope you have understood my problem.
Thanks for your time. :D

Controls are located in a form based on coordinates. Luckily for you these controls have properties that tell you the coordinate for the top, left, right, bottom of a control. So you could dynamically move the right label after setting the text.
Label2.Point = new Point(Label1.Right + 5, Y-coord);
An easier way would be to play about with the labels properties in the designer.

You could also try to anchor label1 to the right and label2 to the left. That way you should have a clean middle line, and as the text grows larger it pushes outwards on does not overlap inwards over each other.
However you need an object to anchor to and luckily the SplitContainer works excellent for this.
Also consider setting the autosize property to off and maxing the widths of the labels. Large enough for the string you expect.

Have you considered making it one label?
As in
theOnlyLabel.Text = $"{dataObject.FirstName} {dataObject.LastName}";
or, if you're using textboxes, something like
theOnlyLabel.Text = $"{txtFirstName.Text} {txtLastName.Text}";
Otherwise, I'm afraid, you'd have to realign these two labels every time your first or last name changes.

Related

Chart X Axis wrong labels

I have a chart and a datagridview, both of them are databound to a dictionary, for example:
freqChart.Series[0].Points.DataBindXY(symCount.Keys, symCount.Values);
And on the screenshot below you can see the difference in X-Axis label/key names. Datagridview shows it correctly, while chart flips char if it's punctuation char. What's the problem? How do I fix it?
Screenshot (Chart and DataGridView):
My comment was wrong; in fact this is a funny effect you get when you have a RightToLeft turned on.
There are several values but this one is enough to reproduce:
freqChart.RightToLeft = RightToLeft.Yes
Either this is what you want, then you can turn it on for the DGV as well; or it isn't then simply turn it off..
freqChart.RightToLeft = RightToLeft.No
As you can see it is a Control.Property so it will work on all controls. Also to be noted: The RightToLeft property is an ambient property, so it propagates to child controls.
Why it acts so strangly I can't say, though. The docs basically talk about alignment, not punctuation. If you are interested you may want to read up on it in wikipedia

How to add labels onto a form one under another?

I made this program and it worked until it didn't... I was adding labels with text onto a form and setting label.Location = new Point(0, yPos); and then doing yPos += labelHeight;
It didn't make sense to me why at first my labels were fine and then I saw huge gaps between then, turns out yPos overflowed, so I can't use this method, is there some sort of container I can use to add labels one after another without setting label location? Also my labels can be of any height and there can be a lot of them.
I was adding these labels as controls of TabPage.
You're ignoring the main problem which is that you are somehow overflowing the yPos value. So either your logic for setting the y position is flawed or you are displaying WAY too much data in one form. My large 32-inch monitor runs at a resolution of 2,500 X 1,600. The maximum value for int (and thus the maximum y value) is 2,147,483,647. Even a scrollable form that's over 1.3 Million "pages" of data at that resolution. If I could process one "screen" of data per second it would take me 373 hours (15.5 days) to consume all of the labels in that form.
So the problem is not which control to use - it how to reduce the amount of data in one form to a manageable amount. You need to look at filtering, searching, sorting, paging, etc. to get the amount of data to a manageable level. Otherwise it's write-only memory. You are displaying it but noe one is reasonably able to use it.
(Looking past the fact that you may be trying to add too many labels to begin with)
You might want to use TableLayoutPanel for adding multiple controls.
https://blogs.msdn.microsoft.com/jpricket/2006/04/05/winforms-autolayout-basics-tablelayoutpanel/
I believe this is a method you can run on something like that
Table.Controls.Add(new Label() { Text = "textHere", Anchor = ... etc};
That way you don't have to explicitly position everything within the panel, only the panel itself.
There are probably a few ways of doing what you're asking. A little bit of research on my part found that this method is generally the right way to go.
Unfortunately I am unable to test this at the moment, but it may put you on the right track.
Turns out when you add things to a form, that has AutoScroll set to true, you should always do:
this.AutoScrollPosition = new Point(0,0);
This worked, thanks to Hans Passant.

Chart control: Design messed Up after clearing and re-adding Y-Values

Been struggling with this through out the day. I have three series on a chart that look like this. NOTE: I am using the winforms control.
The values are being added based on calculations from input. I am using this code to add the values to each series.
this.chart1.Series["green"].Points.AddY(greenvalue);
this.chart1.Series["totalsaving"].Points.AddY(totalsavingvalue);
this.chart1.Series["blue"].Points.AddY(bluevalue);
The series properties I have set like this. Green and totalsaving are both set to StackedColumn, blue is set to Column for chart type.
I then have a button to start over which brings the user back to the input area and I am then using this code to clear the series values on the start over click.
chart1.Series["totalsaving"].Points.Clear();
chart1.Series["green"].Points.Clear();
chart1.Series["blue"].Points.Clear();
The same calculation click is being used as above to calculate and populate the series data. The problem is when I click the calculate button to calculate the values after I have cleared them, the total savings, and the green are missing. Just the blue is shown.
Am I doing something wrong with the way I am trying to clear the values so I can re calculate?
OK, from the edits, comments, our chat and the joim.me session enough data has accumulated to answer the question.
You have twisted the display by adding an extra data point to the blue series in the designer.
This point occupies slot 1 but remains invisible as its value = 0.
This pushes the next point in the series to slot 2
After clearing the points it is gone and the display doesn't work anymore.
The disappearing of the two columns probably was caused by hard coded widths.
You have several paths you can follow:
recreating the extra point with value = 0 before adding the real data (not recommended)
not adding the extra point in the first place but forcing each point into its slot by using Points.AddXY instead of Points.AddY with X being the slot.
not clearing the points but updating their values by using the SetValueY method. After all three data points have beend assigned their new values you need to call chart1.Invalidate() to make it show.
Fore easiest styling of all those properties, some of which are deeply burried inside of property strings(!), you may even decide to add and style&polish all three points in the designer and only update their y-values like this:
chart1.Series["green"].SetValueY(greenvalue);
chart1.Series["totalsaving"].SetValueY(totalsavingvalue);
chart1.Series["blue"].SetValueY(bluevalue);
chart1.Invalidate();
The choice is yours, but in any case I recommend setting the proper X values, be it in code or in the desginer..

maintain button's position in c#, no matter the resolution of the screen

i designed a game in c# and finished it... but i tried it on my friend's laptop with different screen size and resolution, all my design was in a total mess!!
if there is a way to keep everything (panels, picturebox,buttons,labels,...) in their positions despite the size and resolution of screen!?!?
really need help, my project's deadline is on Monday :(
Use anchors on your controls:
I assume this is a windows form application? If so, you can use docking to maintain positions. Also, the positions should stay the same anyway unless the form is not a fixed size.
So use docking or a fixed sized form.
Also, please make sure to specify what type of GUI framework you're using next time. My answer is incredibly wrong if you're using something other than windows forms.
Aside from docking, another option would be to place all of your objects within a panel, and then center it horizontally and vertically on your resize event. e.g.
panel1.Left = this.Width/2 + panel1.Width/2;
panel1.Top = this.Height/2 + panel1.Height/2;
This will ensure that your applications static contents are always centered, regardless of resolution.

Control.PointToScreen gives different results - why?

I have some Label controls sitting on Panel controls on a Form. I want to get the labels' positions relative to the form's origin so that at run time I can hide the panel and the labels and draw some other text in their place directly onto the form.
Basically, I'm doing the following calculation: Get the absolute screen position of a label with Control.PointToScreen() and convert it back to a relative position with Control.PointToClient(), so either:
Dim newloc As Point = Me.PointToClient(ctl.PointToScreen(Point.Empty))
or
Dim newloc As Point = Me.PointToClient(ctl.Parent.PointToScreen(ctl.Location))
I have found that the two methods sometimes give me different results - putting my new point out of the visible area with negative values! - but haven't been able to determine why. I would have thought they should be identical (and they are, most of the time).
Reading the docs didn't help the first time around, but perhaps I skipped over something... Anyway, I'd be thankful for any help before I start tearing my hair out.
Or, on the other hand, is there a better way to do this?
Edit: Sample results
So, here's a real example.
Label1 at {X=4,Y=6} on Panel1; Label2 at {X=163,Y=6} on the same parent, Panel1. Obviously I'm expecting different X-positions, but Y should be identical for both.
When I run the project both ctl.PointToScreen(Point.Empty) and ctl.Parent.PointToScreen(ctl.Location) give me the same screen location for Label1 at {X=959,Y=119} (the absolute values here can vary, of course, depending on the position of the form itself) and therefore the correct location relative to the form when Me.PointToClient is applied (at {X=5,Y=32}).
The very next lines do the same calculations for Label2 (remember, same Parent, same Y-value within the parent (6)), but the results are totally off: ctl.Parent.PointToScreen() gives me {X=1114,Y=63}. X is almost correct (959-4+163=1118), but Y is nowhere near the 119 I got for Label1. And then ctl.PointToScreen() gives me {X=166,Y=29} - translated back to Form-Coordinates (Me.PointToClient) {X=-784,Y=-2}.
These numbers are calculated and printed to the debug window directly after each other, without moving anything around... Madness.
OK, this is getting rather messy, but I still hope someone has a simple explanation. Thanks!
OK, I found the solution.
I happened to be calling Control.PointToScreen before the control was created: Control.IsHandleCreated = False.
If I ensure that the control is created first (Control.CreateControl) both methods work equally well.
The reason I had differing results on subsequent calls is that the first call to Control.PointToScreen also causes the control to be created (and therefore its parent and any other controls sited on the parent), meaning the second succeeds.
Well, I'm sure glad to be done with this :-)
I think it's the sleep. :) Instead of:
ctl.PointToScreen(Point.Empty)
ctl.Parent.PointToScreen(ctl.Location)
try:
ctl.PointToScreen(Point.Empty)
ctl.PointToScreen(ctl.Location) // Note no .Parent!
instead and you'll see the difference in the x/y coordinates.
Also, try using Control.TopLevelControl or Control.FindForm() to get the outermost Form when doing your PointToScreen math.

Categories