I'm creating a simple game using c# windows forms. I have a class named Enemy based on the PictureBox. I placed a Label named Display inside the class. Now I can easily put an Enemy on a form.
I tried am placing an Enemy inside a tab control, but I am not able to make this Label appear. I have already declared it, set the TabIndex, Location and the size.
How can I make the Label show on the screen?
class Enemy : PictureBox
{
public Label Display;
public void CreateDisplay()
{
Display = new Label();
Display.AutoSize = true;
Display.Location = new System.Drawing.Point(298, 120);
Display.Name = "test";
Display.Size = new System.Drawing.Size(35, 13);
Display.TabIndex = 3;
Display.Text = "test";
}
}
This is the class code. I call the CreateDisplay() function when the user clicks on the object.
The problem is the new Label is not part of the control tree on the form. That is, when painting the form .Net will start with the form itself, then look inside the form's .Controls collection, then for each control look inside their .Controls collections, and so on. Just because this Label is a member of the Enemy type does not make it part of that Controls collection. Moreover, a PictureBox does not really support adding child controls.
Instead, create a new UserControl or custom control based on a Panel. This new control should have both a PictureBox and a Label and childs of this new control. Do this right, and the PictureBox and Label will both be members of the appropriate Controls collection and show on the screen as expected.
Related
I have a C# winform which displays snapshots from a camera. The form has four PictureBox controls and When an image is taken it is placed into pictureBox1 and the previous images are bumped along to 2,3 and 4. Under each picture box is also a label which displays the time stamp and the order number (each image is given a number 1-4, that stays with it until it is bumped off in which the newest image takes that number). Currently I am doing it like below. However I feel this is very inefficient and will cause me problems later on if I decide to add key down events to change the backcolors of some of the labels (to indicate status).
Does anyone know of a better way to do this?
if (count > 4)
{
count = 0
}
count ++;
pictureBox4.image = pictureBox3.image;
pictureBox3.image = pictureBox2.image;
pictureBox2.image = pictureBox1.image;
pictureBox1.imagelocation = (#"http://192.168.X.X/image.cgi")
label4.Text =label3.text;
label3.text = label2.text;
label2.text = label1.text;
label1.text = count.ToString()+ " " + datetime.now();
I could create a new Control, most likely a Panel that contains all of these UI elements in it (PictureBox, Label, anything else). Have a constructor for your Control that takes a URL of the image. Load the image into your PictureBox, and set your label.
Have all of that logic encapsulated in your Control. So when a new one is added, you just create the new Control, and remove the last one in your row, and move the .Left properties of the 3 remaining to their new locations.
Don't forget to implement IDisposable, and Dispose of the Controls when they're removed to free up the resources of displaying the images.
EDIT
If it's not there already, you can provide references back to the top Control in each of your inner Controls (PictureBox and Label), and even to your main form in your top Control by passing this as a parameter in the constructor as well and setting a private member variable inside those controls. That way, when someone clicks on the PictureBox, you can go up the line to this.Parent and get your outer Control. You could even have that reference to your Main Form (hopefully a Panel in there that holds your 4 of these objects). That could be this.Parent.Parent to call a method on there. (I think there's already a public property of Parent on all Controls, so that's fine.)
A little bit of quick coding:
You have your main Form (mainForm). In there is a Panel (picturePanel). picturePanel holds your 4 new Panels, which we'll call customPanel. Each customPanel has a PictureBox (imageBox), and Label (fileNameLabel).
Your customPanel constructor would look like this:
public partial class CustomPanel : Panel {
private PictureBox _imageBox;
private Label _fileNameLabel;
public CustomPanel() {} // This is most likely tied into the code behind file. Sorry, It's been a while since I've done WinForms
public CustomPanel(string imageFileName, Panel parent) {
// Set the source for the PictureBox.
// Set the Text of the label.
_parent = parent;
}
}
Continue with this down the line through the PictureBox and Label. Then in your events, you have your PictureBox work up the chain. To find picturePanel. If you want to get really fancy, you could have that derive from Panel as well and just add a public property that handles all of the switching around of which customPanel sent the message.
So down in your PictureBox event, you could have a line of code like this:
if (this.Parent.Parent is PicturePanel) {
((PicturePanel)this.Parent.Parent).RemovePicture(this.Parent);
}
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.)
I'm using microsoft visual c# and making forms. I don't know if my vocabluary is correct but the idea is that when i create a panel using Panel Figure = new Panel ();
I cannot control it, i.e. change location. when I try
Point MoveLeft = Figure.Location;
MoveLeft.Offset(-25, 0);
Figure.Location = MoveLeft;
it requires already existing panel named "Figure" and my newly created panel doesn't respond to commands.
also is there any way that when I create a panel, it's an already existing panel i.e to create the same panel as panel "triangle"?
If you already have a Panel on your form called "triangle", you can simply reference it in code like this
triangle.Left = -25;
If you create a new Panel you need to add it to the Form's Controls list
Panel Figure = new Panel();
Form1.Controls.Add(Figure);
Form1.Location = new Point(-25, 0);
I have a windows form and i dont want to make any other windows forms just one windows form and different user controls how can i change between user controls for example hide one and show the other user control programmatically ?
private void Btt_info_Click(object sender, EventArgs e)
{
Frm_Main frm_main = new Frm_Main();
frm_main.Controls["panel1"].Controls.Clear();
UC_Info uc_info = new UC_Info();
frm_main.Controls["panel1"].Controls.Add(uc_info);
}
i added this but it doesnt work
Add a container control (if I remember correctly, there's a containers section in the toolbox?), like a panel. Create usercontrols for what you want to dynamically switch around. So make like a 'HomePage' usercontrol and a 'LoginPage' usercontrol. Dynamically add the usercontrol that you want to display to the container. WHen you want, remove it from the container and add a different usercontrol:
Panel myPanel = new Panel();
LoginPage ctlLoginPage = new LoginPage();
HomePage ctlHomePage = new HomePage();
//add the loginpage to the panel first
myPanel.Controls.Add(ctlLoginPage);
...do stuff...
//remove whatever control is currently in the Panel
myPanel.Controls.Clear();
//add the other control, the HomePage control instead now
myPanel.Controls.Add(ctlHomePage);
..do other stuff...
I usually do it this way so you leave your form itself open to add common controls and stuff that might be shared between your different 'pages'.
EDIT: Note that I normally would add the panel in the designer and not create it dynamically in the code. This was just an example.
EDIT: The interaction between your mainform and usercontrols can be handled in a few different ways, and I am not saying that any of these is the correct method.
You create a static property for your Panel on the Mainform, so that
you can always access it to swap your controls around.
In this example I'll also add a static method for it
enum PanelControlsEnum {HomePage, LoginPage};
public static Panel MyContainerPanel {get;set;}
public static void SwitchPanelControls(PanelControlsEnum selControl){
..put your switch panels code here..
}
Then inside your usercontrol you call a predefined method, something like:
MainForm.SwitchPanelControls(PanelControlsEnum.HomePage);
Another method is to bind the button click event on your mainform
instead of inside the form.
Like This:
HomePage ctlHomePage = new HomePage();
ctlHomePage.Click += MyClickEvent;
myPanel.Controls.Add(ctlHomePage)
...
private void MyClickEvent(object sender, RoutedEventArgs e)
{
..switch user control code here...
}
Create a method that returns a UserControl object. Then put conditions in that method as to which control you want to load at a specific condition and then in your main form code.
UserControl control = GetControlFromMyMethod();
form1.Controls.Add(control);
where 'control' is the returned control from your method.
To remove the existing one you have to loop through the form1.Controls and find out the control and call 'Remove'.
Update:
Mike C has a better idea of adding a panel and loading your desired control on the panel as then it's easy to remove your control and you then don't have to loop through the forms controls to find it and then remove it.
Try this:
this.Controls.Clear();
usercontrol load = new usercontrol ();
this.Controls.Add(load);
load.Show();
you could try this it will definitely help you as it did helped me a lot it short and straight to the point hope that will help
I am trying to prototype a GUI and created a minimal WPF project using Expression Blend 4.
I have a main window (XAML+codebehind) with eight instances of an UserControl, which is defined separately (XAML+codebehind). This user control has some containers and shapes, and I would like to have each of its instances in the main window to have a different appearance.
The idea would be to expose some properties of this control, and treat them as parameters. Then, I could use code behind to populate the main window, and for each instance I could add some hardcoded parametrizations. For example in pseudo-code:
first = new mycontrol;
mainwindow.maincontanier.add(first);
first.leftpanel.width = 100;
first.rightpanel.background = Gray;
second = new mycontrol;
mainwindow.maincontainer.add(second);
second.leftpanel.smalldot.stroke = Red;
second.leftpanel.insideborder.thickness = 2;
etc.
The problem is: I have no idea how this is usually done, so I appreciate any suggestion, specially in the form of minimally working code blocks.
Just to remember, I am doing this only as a design study, so I can compare different possible visual states of the control. I am not a C# programmer, and there is not any other GUI functionality involved.
If you are asking how to add items programatically:
//get mainwindow
Window main = App.Current.MainWindow;
myControl c1 = new myControl();
//change properties
myControl c2 = new myControl();
//change properties
//choose a container you want
StackPanel s = new StackPanel();
//add objects to container
s.Children.Add(c1);
s.Children.Add(c2);
//make container as your main content
main.Content = s;
First, you need to know some C# to do this. Second, your control can expose properties that map to your internals appearance properties so when the user change them, you change the equivalent inside your control.