plz keep in mind I'm very noobish...
What I'm trying to do is add "blips" to my form. I have a calculation that determines where all these "blips" will be, which is plotted on a graph. the solution determines that the coordinates are "blipHours, blipAltitude"
I want to somehow add small dots to my form at these locations. At first I was going to create something to be my "blips" but then I realized that I want it so small that I could just use an empty picture box with the background color what I want (I know this isn't the best way but I'm still very new to this).
I've created the code that will add the blip
PictureBox blip = new PictureBox();
blip.Location = new Point(blipHours, blipAltitude);
blip.Size = new Size(6, 6);
blip.BackColor = System.Drawing.Color.Lime;
blip.Text = "";
blip.Name = callsign;
this.Controls.Add(blip);
It adds the blip, but it always adds it underneath other controls. Is there a way to make it add the new blip on top of everything else so that it's visible?
my second question is how do I remove all of the blips that get created at once with the click of a button?
An alternative to nobugz' answer is to change the Z-order of your controls via the Form.Controls.SetChildIndex method:
this.Controls.Add(blip);
this.Controls.SetChildIndex(blip, 0);
You can use .AddAt to set its position in the list of controls.
Related
I am currently working on a Editor, which lets the user design his own WinForm overlay, at least to a certain point.
Therefore I want the user to decide, which AnchorStyles the current selected Control should have. I would like it to be handled by checkboxes. Here's how I had it in mind:
As you can see, the user has currently selected a dynamically added Panel, called Grid. Handled by the CheckBoxes to the right, he should now be able to set the selected Controls AnchorStyles.
Here's my problem: I can't seem to find a usable solution, to dynamically add a specific AnchorStyle to the already existing ones, or the opposite, remove the AnchorStyle, but keep the other ones as they are.
I was trying to get it to work with...
SelectedControl.Anchor += AnchorStyles.Top;
which doesen't work at all. So i thought of this...
SelectedControl.Anchor = SelectedControl.Anchor | AnchorStyles.Top
which I imagine could work, but I haven't even tested it, since I wouldn't know how to remove ones unchecked AnchorStyle.
Building a gigantic if(){} else if(){}... doesen't seem to be a good Idea :)
I'm open for any ideas / solutions.
Thanks in advance!
Assuming you have four check box controls named top, bottom, left and right, you can handle CheckedChange event of them using a single method and set the anchor of the desired control based on the checked value of the controls. For example:
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
var values = new[] { top.Checked, bottom.Checked, left.Checked, right.Checked };
byte[] data = new byte[1];
new BitArray(values).CopyTo(data, 0);
selectedControl.Anchor = (AnchorStyles)data[0];
}
Note: AnchorStyles is a flag enum having top=1, bottom=2, left=4 and right=8. Using above code I've mixed those flags to create the AnchorStyles and have assigned to the Anchor property of control.
i'm using telerik control.
So i want to ask,
In winforms application ,Is it possible to add more than one panel in same location and display one at a time just like show/hide property.
Make sure you have placed all panel control in same container or form. then you can use Visible property to show and hide panel. BringFront and SendToBack function will be used to bring panel on top or send it to back. If you have placed any panel in another panel then that will be disappeared when you Hide parent panel. So, Make sure all panels' parent control must be same. To determine the parent control simply select that panel and press escape key to select their parent.
private void LoadPanels()
{
panel1.Location = new Point(10,10);
panel2.Location = new Point(10,10);
panel3.Location = new Point(10,10);
panel4.Location = new Point(10,10);
panel5.Location = new Point(10,10);
VisiblePanel("panel1");
}
private void VisiblePanel(string panelName)
{
string[] panels = new string[]{"panel1","panel2","panel3","panel4","panel5"};
for (int i=0;i<panels.Length;i++)
this.Controls[panels[i]].Visible = (panels[i] == panelName);
this.Controls[panelName].BringToFront(); //Not required you can remove this line.
}
Here's a slightly different approach you might want to consider...
Are you wanting to be able to programmatically select the contents of a rectangular area at runtime, selecting among various controls to display? If so, you could use a custom TabControl which has its tabs (not the pages) hidden.
Then you can select which page is displayed by programmatically changing its SelectedIndex property at runtime.
Doing it like this means that your form editor will show a normal tab control, which allows you to much more easily add the content to each page - but at runtime the tabs will be hidden from the user; they will just see the contents of the currently selected page.
See Hans Passant's answer here for how to create such a custom tab control.
(However, you might also want to override the OnKeyDown for the custom tab control in order to ignore Ctrl-Tab.)
So I am trying to add a UserControl to a windows form, however I want to add it in a variable location when a button is clicked.
So I have a groupbox in one location and I want the first one to go to the extreme left directly under the groupbox, I then want the next one to be in a position relative to the first, and all subsequent ones to be in a position in relation to the one before. However with restricted space a new line of these controls would have to be eventually created.
I am not sure if this is possible, or how I would do it. Currently I only know how to define a specific point for the control to be created at.
The only part of the code that really matters:
private void addpilot_Click(object sender, EventArgs e)
{
PilotControl newPilot = new PilotControl();
newPilot.Location = new Point();
this.Controls.Add (newPilot);
}
I think that this behavior could be similar to a WrapPanel. If it is not, you may try to solve this using another Panel, or also, implementing your own panel to create an specific location behavior.
Try seen Panels Overview in the MSDN.
Look into docking and flow controls.
I'm having a difficulty in sizing my form!
I dynamically create buttons on a form and need to know if they are all fully visible or if I need to grow the form and in what direction to make all the buttons fully visible.
I don't want to use the autosize property as I need to control the layout.
So how do I tell if a dynamically created controls bounds are within that of the form?
thanks
This a .Net 4 classic forms app.
When you add the button to the controls collection, to see if it is visible check the contains on the forms bounds - Form.Bounds.Contains(button.Bounds));. If that returns false then you need grow your form. Here is some basic code to do the form growing, it will not necessarily produce the prettiest output and is not necessarily the best way, just written to give you a quick idea of how it could be accomplished.
// Add the control
form.Controls.Add(button);
var formBounds = form.Bounds;
var controlBounds = button.Bounds;
if (!formBounds.Contains(controlBounds))
{
formBounds.Left = Math.Min(controlBounds.Left, formBounds.Left);
formBounds.Right = Math.Max(controlBounds.Right, formBounds.Right);
// Do similar for top and bottom this will ensure your button is visible
form.Bounds = formBounds;
}
Can you add the button, can't you compare the Width of the container vs the Left + Width properties of the newly added button?
Having a slight problem on C#, still quite new to the language but hoping you can help. I have a program which dynamically creates tab forms and then I'm trying to add controls to the tabform (text boxes and labels), but no matter what I try it just doesn't seem to want to work. Here's the code I'm currently using (just to get one textbox in each form):
int i = dogresults;
while (i > 0)
{
i--;
DataRow dogrow = ds1.Tables["confirmdogs"].Rows[i];
string dogname = dogrow.ItemArray.GetValue(3).ToString();
TabPage newpage = new TabPage(dogname);
tcNewCustomer.TabPages.Add(dogname);
TextBox tb1 = new TextBox();
tb1.Location = new Point(20, 10);
newpage.Controls.Add(tb1);
tb1.Name = "txtDogNo" + i;
}
Any help would be greatly appreciated!
EDIT: Doh! Got it!
You're not adding the new TabPage you're creating. This line:
tcNewCustomer.TabPages.Add(dogname);
should be like this:
tcNewCustomer.TabPages.Add(newpage);
(A small test app shows the tab pages being created without any textboxes with the first version, but with the second version working fine.)
That looks okay at a glance (although I haven't tried it - a short but complete demo program would help). When you say it "just doesn't seem to want to work" - what exactly is happening?
Have you tried moving the location down a bit? I know some controls are odd in terms of where their logical "top" is (i.e. it's not the first visible pixel).
What about setting the text in the textbox? Currently you're just setting the name...
Although I'd still expect you to see a border on the box + background colour assuming that differs from the tabpage background.