How to dynamically load a panel in Windows Forms? - c#

there are some buttons on the top of the winform, and when I click one of them, the panel below will load different predefined panel, how can I implement this ?
please see this example:

Here's a solution using a standard WinForms TabControl, where the Tabs are hidden at run-time, but of course they are available at design-time.
Assumptions :
You don't want to get into creating OwnerDrawn Tabs, which is possible.
A standard WinForms TabControl will meet all your design-time needs.
Code :
In the Form Load event of the Form that hosts your TabControl use code like this :
tabControl1.Region = new Region(tabControl1.DisplayRectangle);
To hide the Tabs.
Then, "wire" up your buttons to handle selecting the different TabPages in the TabControl. Obviously you could do this in a more elegant way than this :
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages[0];
}
private void button2_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabControl1.TabPages[1];
}
Note : if you want to insert secondary Forms or UserControls into the TabPages of a TabControl : that's not a problem : of course simpler to use UserControls. Insert them into the Controls collection of each TabPage and set their 'Dock Property to 'DockStyle.Fill.
Note : there are fancier ways you can hide the Tabs, like using a derived TabControl as shown here on CodeProject : TabControl on a WinForm without showing the Tab header? There are other solutions out there that use a modified WndProc. They're not hard to find.

I don't know exactly what you're trying to do, but if you've got a Panel on your form named contentArea and a bunch of user controls created (but not on the form), then you could use this as an event handler for a button:
public void myButton_Click(object sender, EventArgs e) {
contentArea.Controls.RemoveAt(0);
contentArea.Controls.Add(new MyUserControl());
}
...though as other people have said, a tab control would be better in this case.

What you can do is have them each in a separate Panel. Set the Visible property to false to each. When the Click event on the button, set the Visible property of all of them to false and set the one you want shown's Visible to true.

For example if you have two form Form1 and Form2 and you want to load form2 inside from1. when you press a button to load form2 the code is like this
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
this.Controls.Clear();
foreach(Control c in this.Controls)
{
this.Controls.Add(c);
}
}
this code will load all the controls in the form2 into form1.

Related

Access a ListView located in a UserControl from the MainForm in Winforms

I have an app that uses a side menu, and for each button (there are 3) on the left side menu, it changes the pages shown.
I tried doing it with multiple panels, but it's a nightmare to maintain in designer, and it's probably not a very good programming habit, I expect.
So I search and found what seemed to be a great idea: UserControl.
But as usual, it's not that simple (for a badly self-taught guy like me)
The general flow of the program is as follows:
a Btn_uc1_Check button that gathers informations and displays them in a uc1_ListView,
a Btn_uc2_Seek button that gathers informations on the net based on the uc1_ListView , and displays them on uc2_ListView,
a Btn_uc3_compile that compiles the info from uc2_ListView into a file,
a Clear button that clears the ListView depending on the UserControl on screen.
Now to the problem:
How on earth do I gain access to a ListView located in a UserControl to be able to read, clear, and add items from the MainFrom or from another UserControl?
I searched and honestly found nothing corresponding to what I needed?
Quite many questions.
You can gain access to any controls in UC. Just change the property "Modifiers" of the ListView in your UC to "Public".
Set that method to public. Do not use keyword "static". Each control
in your form is an instance of a class, not a static class actually. In the main form, create a button and double click on it in VS designer. A method will automatically generated, something like private void button1_Click. When the button is clicked, all of the code lines in button1_Click will run.
Create a public event handler of your user control, then pass the method in main to the handler.
So the UC class will be similar to this:
public event EventHandler button_UC_Click_handler;
public UserControl1()
{
InitializeComponent();
}
private void button_UC_Click(object sender, EventArgs e)
{
button_UC_Click_handler.Invoke(sender, e);
}
In main form:
public MainForm()
{
InitializeComponent();
userControl11.button_UC_Click_handler += UserControl11_button_UC_Click_handler;
}
private void UserControl11_button_UC_Click_handler(object sender, EventArgs e)
{
MessageBox.Show("You have clicked it!");
}
Good luck!

Which control supports Label with Close Button for windows forms

Is it possible to get a Text with Close button option for a windows Forms Application.Please let me know if we have any Dev Express control is available for the below functionality.
If you need this on across multiple interface. I would recommend you to create yourself an UserControl. (Or component if you prefer) Put a label and a pictureBox in the user control. Then implement the two ClickEvent needed.
public partial class UCTextWithImage : UserControl
{
public event EventHandler TextClick;
public event EventHandler ImgClick;
public UCTextWithImage()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
if (ImgClick != null)
ImgClick(sender, e);
}
private void label1_Click(object sender, EventArgs e)
{
if (TextClick != null)
TextClick(sender, e);
}
}
Once done, build you project, so the new userControl become available in form designer toolbox. Then drag and drop this UserControl into your interface. Finally bind and implement the two click event.
If you need this for only a few area, then the ButtonEdit is the closest to what you need.
https://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraEditorsButtonEdittopic
In the properties, you have a button array. Remove the default one and add a new one of type glyph. Set the glyph you want. You can then use the ButtonClick event for the ImageClick and Click event for the LabelClick. However, in LabelClick, you need to check if underlying control isn't a button.
Finally, you need to change the appearance so the ButtonEdit so it become exactly like a label instead of Textbox.
To do so :
1- BackGroundColor must be transparent
2- BorderStyle set to None
Also, with devexpress, in order to have appearance set be taken into consideration, you need to remove the LookNFeel that overwrite everything...
So, it is possible but if you really want to, but userControl solution is easier.

Making a new form window appear in c#

I'm trying to make a card game using Windows Application Forms.
The thing is that I don't know how to do the following- for example if i'd have 3 buttons- one of them named, for example, "Play", if i'd click on it, it would open the actual game, but in the same window, it would only make the buttons dissapear, and when i'd click back, it would open the window with buttons again. I don't really know how to explain my problem better, hopefully someone can tell me how to do that.
You don't have to hide / show the buttons. What you can do instead is to make a new form with the cards on it. That Form will pop up after you click the play button.
private void PlayButton_Click(object sender, EventArgs e)
{
// You other functionality goes here
GameForm GF = new GameForm();
GF.Show();
//Or - try this and see the difference
GF.ShowDialog();
}
Good Luck!
In addition to Leez's answer, in your situation, you should think about using container controls rather than handling the visible states of individual controls.
You could put related controls in a Panel, GroupBox or TabControl and set the visible properties of those containers instead.
you can use Visible property of button to do that as follows.
private void button1_Click(object sender, EventArgs e)
{
// You other functionality goes here
button1.Visible = false;
}

Whats the best way to switch form in C#

So in the project I'm planning, the user will be switching between different screens frequently. These different screens will have different controls with different functions, and really nothing in common.
Should I create a form for each screen and just form.close and form.show(?) to switch screen? Or is it better to keep hiding and showing certain controls, so if a user wants to go to form B from form A, all of form A's controls disappear, and form Bs controls appear? I don't need to know how to do it, I just need to know which is the recommended or proper way, since I haven't really seen it anywhere.
You shouldn't use
form.close();
as this will close the current form (and if its your main form, exit the application) use
form.hide();
instead. An example is given below
from Form1:
private void btnModify_Click(object sender, EventArgs e)
{
Form mod = new modifyForm();
mod.Owner = this;
mod.Show();
this.Hide();
}//end btnModify_Click
from modifyForm:
private void btnCancel_Click(object sender, EventArgs e)
{
this.Owner.Show();
this.Close();
}
You can also try using the TabControl. You can group your different screens into the different tab items. All you have to do is drag it to the form and edit its content in the Properties window.

how to set default focus on C# Winform ToolStripMenuItem when Form Shown?

I want to set default focus on C# Winform ToolStripMenuItem when my form is Shown. Is this possible or not? If so, how do I do it?
In the Shown event of your Form, set focus to the containing Toolstrip using Focus(), then use Select() to select the specific tool strip item:
private void Form1_Shown(object sender, EventArgs e)
{
this.toolStrip1.Focus();
this.toolStripMenuItem1.Select();
}
In the constructor for the Form, after creating your ToolStripMenuItem control, use the instance method .Focus();. This will immediately give focus to the control.

Categories