I'm trying to dynamically position controls on a page, I've got the hang of "wrapping" the controls to the next line when the width is smaller than the total width of the controls.
The problem I'm having now, is getting the spacing correct.
I currently have the following;
public void AddControl(Control controlToAdd, int parentWidth, int allRowsHeight)
{
RowControls.Add(controlToAdd);
int seperationWidth = (parentWidth - RowControls.Sum(c => c.Width)) / (RowControls.Count + 1);
int count = 0;
foreach (Control c in RowControls)
{
int xLocation = (seperationWidth*(count+1));
for (int i = 0; i < count; i++)
{
xLocation += (RowControls[i].Width);
}
c.Location = new Point(xLocation, allRowsHeight);
count++;
}
}
This almost works, but as you can see from the screenshot, the controls are going a bit too far and I'm not quite sure why that is?
The "wrapping" check is basically a repetition of the seperationWidth line, it ensures of minimum spacing of 1, i.e if the spacing is less than 1, it "wraps" instead.
Perhaps there's a completely different and better way to do this? I do have access to DevExpress too if anyone is familiar with that.
EDIT:
Suspect my method has an issue with rounding. Not sure how I can get around it though;
In this instance, seperationWidth is 7 and the control widths are 128.
7+128+7+128+7+128+7+128+7=547.
Not sure how I can get around this issue though?
OK, so the issue here was that I was passing the Width property into the parentWidth parameter. What I actually needed to do was pass the ClientSize.Width property. This fixed the issue, buttons are now laid out correctly.
Related
I am trying to do something similar to the one in the example but changing the colour of the text/label.
Example:
Label1.Text = Label2.Text;
Label2.Text = Label3.Text;
Label3.Text = Label4.Text;
//so and and so forth
I am looking for a very simple solution like the one shown in the example but with colors.
I am using Windows Form App with .Net Framework 4.7.2, I am using C# 9.0.
I have tried using .ForceColor as a way to change the color but as you can see, i am trying to accomplish a similar thing as the one in the example.
At first, I suggest put labels in a container, And don't put anything except labels.
Then:
Do for loop, and length = ContainerPanel.Controls.Count - 1, with condition i = ContainerPanel.Controls.Count
Cast Control[i] to labelFirst
Cast Control[i + 1] to labelSecond
labelFirst.ForeColor = labelSecond.ForeColor
In this example, Container is: ContainerPanel
for (int i = 0; i < ContainerPanel.Controls.Count - 1; i++)
{
// This is a simple way without variables
(Label(ContainerPanel.Controls[i])).ForeColor = (Label(ContainerPanel.Controls[i + 1]));
}
Note: last Label You should change its ForeColor, Or it will not be changed.
Q&A
Q: Why length = ContainerPanel.Controls.Count - 1??
A: Because we should get Cast Control[i + 1] to labelSecond, And we can't, except if the length is less than controls count by one
I am trying to make a simple WYSIWYG editor. I found pretty difficult to format the rtb.
It is supposed to format basic things like bold, italic, coloring(and mixed).
What have I found and tried so far:
private void boldButton_Click(object sender, EventArgs e)
{
int start = rtb.SelectionStart;
int length = rtb.SelectionLength;
for (int i = start, max = start + length; i < max; ++i)
{
rtb.Select(i, 1);
rtb.SelectionFont = new Font(rtb.Font, rtb.SelectionFont.Style | FontStyle.Bold);
}
rtb.SelectionStart = start;
rtb.SelectionLength = length;
rtb.Focus();
}
rtb = richtextbox.
This works as expected, but is terribly slow.
I also found the idea about using and formatting directly the RTF, but the format seems too complicated and very easy to mistake it.
I hope it is a better solution.
Thank you.
The performance hit is probably down to the fact you're looping through each character instead of doing everything in one go:
var start = this.rtb.SelectionStart;
var length = this.rtb.SelectionLength;
this.rtb.Select(start, length);
this.rtb.SelectionFont = new Font(this.rtb.Font, this.rtb.SelectionFont.Style | FontStyle.Bold);
I've had the same problem myself. Interestingly, I found out that you can speed up formatting by an order of magnitude if you refrain from referencing to the control's properties when looping through. Instead, put the necessary control properties in separate variables before entering the loop. For instance, instead of continuously referencing e.g. richTextBox1.Length, replace with int len = richTextBox1.Length, and then refer to len inside the loop. Instead of referencing to richTextBox1.Text[index], replace with string text = richTextBox1.Text before the loop, and then instead text[index] inside the loop.
I want to display labels for specific data points in MSChart pie chart winform application with the following code
if (Accountchart.Series[0].Points.Count > 0)
{
for (int i = 0; i < Accountchart.Series[0].Points.Count; i++)
{
double calc=(yValues[i] * 100 / (double)totalTimeSpent);
if ( calc< 10.00)
Accountchart.Series[i].Points[i]["PieLabelStyle"] = "Disabled";
}
}
But getting the following error while executing the code segment in if block second time
[Screenshot]![1
You probably ONLY have Series[0], I guess.
Series[i] looks like a mistake to me - in the whole context of this code.
So, when i==1 (the second time through), the Series[i] does NOT exist! Thus the exception, cause index is out of range.
But it is only a guess!
I think You in fact wanted to write: Series[0]....... in the IF-command, just like in the lines above the for loop.
I am trying to draw a box in a console but I cant figure out to do it without entering a new line.
I make something like this:
-----
| |
_____
But the input line comes directly underneath it and removing the upper row of the box.
I tried with Console.setCursorPosition but the last line is already filled thus already a new line created.
How can I prevent the new line from being created?
Thanks in advance
Edit
It is possible to use the console window as a canvas?
Lets assume a window with the following size
Height : 50
Width : 100
I can place any character on any of the tiles within the above given console size.
Would this be possible if so how?
It would be helpful if you posted code you wrote to create this box from ASCII chars.
If you use Console.WriteLine(), just use Console.Write() method for the last line instead. So, for the box in your question the code can look like:
...
Console.WriteLine("-----");
Console.WriteLine("| |");
Console.Write("-----");
...
Let me know if this helps.
I made a test in a sample application and the extra line seems not to be created.
Update
If you want to use the box a border to the Console window, I suppose it would be the easiest solution (if not the only one) not to add the last - character in the bottom line of the box you are drawing.
Update II
You can try to use a simple trick and dynamically change height of the console window before drawing the last line. This will prevent the window from being scrolled.
Try code like this:
int width = Console.WindowWidth;
int height = Console.WindowHeight;
int i, j;
for (i = 0; i < width; ++i)
{
Console.Write("-");
}
for (j = 0; j < height - 2; ++j)
{
Console.Write("|");
for (i = 0; i < width - 2; ++i)
{
Console.Write(" ");
}
Console.Write("|");
}
//enlarge window in order to prevent it from being scrolled
Console.WindowHeight += 1;
for (i = 0; i < width; ++i)
{
Console.Write("-");
}
//restore window's original size
Console.WindowHeight -= 1;
//set cursor inside the border
Console.SetCursorPosition(1, 1);
This should allow you to get the whole border visible in the window.
Update III
The suggested solution
Use the code above, but with Console.BufferWidth instead of Console.WindowHeight. It seems to work correctly and does not require to modify window's dimensions.
I think that very similair task have guys in Microsoft.
Have a look:
http://msdn.microsoft.com/en-us/library/system.console.setcursorposition.aspx
There's something very similair. Hope this helps.
EDIT:
Console.Write("\b\b");
This should remove last '\r\n' characters for new line.
I am trying to populate a container with any number of controls that have the same height and width. I allow this container to be shrunk or grown by the user and the container will organize the controls so that it fits the most controls on one row as possible. Here is the code to organize it:
int row = 0;
int column = 0;
for (int i = 1; i <= controls.Count; i++)
{
controls.Values[i-1].Top = row * controls.Values[0].Height;
controls.Values[i-1].Left = column * controls.Values[0].Width;
if (i % controlsPerRow == 0)
{
// This finishes a row
row++;
column = 0;
}
else
{
column++;
}
}
The problem i run into is that on the first iteration of the loop, I will be multiplying the control height by the row and assigning that value to the control Top property. The first row is 0 and the first height is 165. 0 * 165 = 0, but the Top property contains a magical -20 after assigning the 0.
Anyone have any idea how this can happen?
You're trying to rewrite the FlowLayoutPanel.
Consider using it instead.
Also, it looks like your controls field is a Dictionary<Something, Control>.
Be aware that the iteration order of Dictionary.Values is not guaranteed, meaning that you aren't looping over the controls in the order that they were added to the dictionary.