Display multiple groupboxes on winforms - c#

So, in my WinForms application, I need to display several "pages" (that are groupboxes holding textbox, buttons, maskedbox and etc) on one main page on my application. Firstly I tried to use a "user controller", but it didn't work out because the name of the controllers didn't match the names that I passed to my database connection for example. So I tried to place these groupboxes one on top of another one via the "location property". All working just fine, but now I have a significant problem when I decide to make a few changes to these groupboxes controllers. It's very hard to access them and I wonder if there's another way to do so because it looks very amateur approach... How do I achieve this kind of functionality without placing groupboxes on top of another one? And I also wonder, is it correct to do it with groupbox or should I use panel?
An example of the approach:
private void btnCadastrarBeneficiario_Click(object sender, EventArgs e)
{
groupBoxUsuarioCadastro.Visible = false;
groupBoxClienteCadastro.Visible = false;
groupBoxHospitalCadastro.Visible = false;
GroupBoxMonitoramento.Visible = false;
groupBoxBeneficiarioCadastro.Visible = true;
}
I use the visibility property so I can show (or not) the groupbox and make them behave like an actual page.
---EDIT----
In order to make myself clear I created a minimal version of what I'm trying to show here:
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
groupBox2.Visible = false;
groupBox3.Visible = false;
groupBox1.Visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
groupBox1.Visible = false;
groupBox3.Visible = false;
groupBox2.Visible = true;
}
private void button3_Click(object sender, EventArgs e)
{
groupBox1.Visible = false;
groupBox2.Visible = false;
groupBox3.Visible = true;
}
}
}
This is the code of a minimal application similar to what I'm doing. It works just fine, but I wonder if there's another way to do it, a more efficient, organized, and sophisticated way.

I would tend to do something like this:
private void button1_Click(object sender, EventArgs e)
{
HideAllBut(groupBox1);
}
private void button2_Click(object sender, EventArgs e)
{
HideAllBut(groupBox2);
}
private void HideAllBut(GroupBox groupToShow)
{
var groups = new[] {groupBox1, groupBox2};
foreach(var gb in groups)
{
gb.Visible = (gb == groupToShow);
}
}
You can obviously extend that to as many controls of whatever kind you like.

Related

C# .NET Substitutes for UserControl Panels

I'm fairly new to GUI-development in C# .NET, using WinForms, and I'm creating an application with one form, (like a main menu), and with different "sub-menus", that can all be opened in the same form. This is what I currently have:
Application with UserPanel1 open.
Application without any UserPanel selected.
I've seen some shady youtuber use UserControls for this, and that's what I'm currently working with. I'm using a TableLayoutPanel, with the UserControls dropped into it.
But I have some problems with this. First, I don't know if this is the correct way to use UserControls and if this will throw any errors.
Second of all, I don't know the exact way to use multiple UserControls in one form, because I can't drag multiple UserControls in a single cell of a TableLayoutPanel, and if I instantiate a new UserControl using some simple code, it won't display all of the custom controls I previously placed (in that UserControl). Here is that code:
private void button1_Click(object sender, EventArgs e)
{
Form1_UserControl1 form1_UserControl1 = new Form1_UserControl1();
form1_UserControl1.Show();
form1_UserControl1.BringToFront();
form1_UserControl1.Dock = DockStyle.Fill;
}
I know that this is probably a simple thing that I'm doing wrong, but I only need some simple advice.
Am I using the right control, or is there a better/easier way to achieve what I'm looking for?
Initialize all UserControls to private fields
TableLayoutPanel add all these UserControls
Use buttons to hide/show them.
My sample code:
public partial class Form1 : Form
{
private UserControl[] myUserControls = new UserControl[3];
public Form1()
{
InitializeComponent();
InitializeUserControls();
}
private void InitializeUserControls()
{
for (int i = 0; i < myUserControls.Length; ++i)
{
myUserControls[i] = new UserControl();
myUserControls[i].Hide();
myUserControls[i].BringToFront();
myUserControls[i].BackColor = Color.Blue;
myUserControls[i].Dock = DockStyle.Fill;
tableLayoutPanel2.Controls.Add(myUserControls[i]);
}
}
private void button1_Click(object sender, EventArgs e)
{
myUserControls[0].Visible = !myUserControls[0].Visible;
}
private void button2_Click(object sender, EventArgs e)
{
myUserControls[1].Visible = !myUserControls[1].Visible;
}
private void button3_Click(object sender, EventArgs e)
{
myUserControls[2].Visible = !myUserControls[2].Visible;
}
}

Action perform button visible false does not work

Well, I have this situation, in a program I put a Button whose code is activated with PerformClick (programmatically), that button must be invisible in the interface so I put the value visible=false since the beginning of the program but the action on the event click doesn't perform, but if I put visible = true, the action actually is performed, any ideas of the problem?
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if(_datosDe == "Insumos")
{
_btnRecargarInsumos.PerformClick();
}
this.Close();
}
_btnRecargarInsumos: is the button and is actually performed in another Form.
private void btnRecargarInsumos_Click(object sender, EventArgs e)
{
objGeneral.regresaDescripciones(ref dsDescripciones);
cbACDescripcion.DataSource = dsDescripciones.Tables[0];
cbACDescripcion.DisplayMember = "Nombre";
cbACDescripcion.ValueMember = "ID";
cbACDescripcion.SelectedIndex = -1;
cbACDescripcion.Text = "";
}
cbACDescripcion: Combobox which will be "reloaded" with the values of the DataSet: dsDescripciones.
The property visible is false since the beginnig of the program, but I also try to set visible=true and just before the method PerformClick() change it, but is the same.
But if I put visible=true since the beginning it works in that way.
If you click a button that's not visible or not enabled, nothing happens, even if you click it programmatically. Here's a workaround that works for me, although it's a bit of a hack:
_btnRecargarInsumos.SuspendLayout();
_btnRecargarInsumos.Visible = true;
_btnRecargarInsumos.PerformClick();
_btnRecargarInsumos.Visible = false;
_btnRecargarInsumos.ResumeLayout();
Why not just put your code in a separate method?
Example:
private StuffToDoAtClick()
{
objGeneral.regresaDescripciones(ref dsDescripciones);
cbACDescripcion.DataSource = dsDescripciones.Tables[0];
cbACDescripcion.DisplayMember = "Nombre";
cbACDescripcion.ValueMember = "ID";
cbACDescripcion.SelectedIndex = -1;
cbACDescripcion.Text = "";
}
//Your Button.Click() code//
private void btnRecargarInsumos_Click(object sender, EventArgs e)
{
StuffToDoAtClick()
}
//Your Datagridview code//
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if(_datosDe == "Insumos")
{
StuffToDoAtClick();
}
this.Close();
}

Edit Items in a ListBox

I am creating a program using WinForms so users can input info into textboxes on one form which then are saved into a Listbox on another form. I would like to be able to edit the items saved in the listbox by opening the original form on a button click. Really struggling with it as I can't think of the code and I can't seem to find a solution.
My Code:
private void btnAdd_Click(object sender, EventArgs e)
{
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.ShowDialog();
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
listBoxRooms.Items.Add(newRoomDisplayForm.value);
}
newRoomDisplayForm.Close();
}
private void btnRemove_Click(object sender, EventArgs e)
{
this.listBoxRooms.Items.RemoveAt(this.listBoxRooms.SelectedIndex);
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
So i've got a Add and Remove button which work perfectly just need a solution to the edit button.
Thanks in advance
I'm guessing newRoomDisplayForm.value is a property or a public member inside the form. You just need to do something like this:
private void btnEdit_Click(object sender, EventArgs e)
{
if(listBoxRooms.SelectedIndex < 0) return;
var tmpValue = listBoxRooms.Items[listBoxRooms.SelectedIndex].ToString();
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.value = tmpValue;
newRoomDisplayForm.ShowDialog();
//TODO: inside "newRoomDisplayForm" set the value to the textbox
// ie.: myValueTextBox.Text = this.value;
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
// replace the selected item with the new value
listBoxRooms.Items[listBoxRooms.SelectedIndex] = newRoomDisplayForm.value;
}
}
Hope it helps!
You can simply remove the listitem in that specific position, create a new item and add it again. it's kind of replacement.

Remeber Me C# app setting

I Create login form and want to put "remember me" check box on it.
But every time i open program it doesn't change.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
Project.Properties.Settings.Default.rememberMe = true;
Project.Properties.Settings.Default.Save();
}
else
{
Project.Properties.Settings.Default.rememberMe = false;
Project.Properties.Settings.Default.Save();
}
}
Also i want to save user login information, should i save them in app setting just like remember me setting or there is better way?
You're saving the settings, but you need to retrieve those settings too.
Subscribe to the Form's load event and set the value of the CheckBox.
private void Form1_Load(object sender, EventArgs e)
{
checkBox1.Checked = Project.Properties.Settings.Default.rememberMe;
}
Also, and this is just common practice, but your code could be shorter:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Project.Properties.Settings.Default.rememberMe = checkBox1.Checked;
Project.Properties.Settings.Default.Save();
}

in c#, how can you disable a button after the 1st click, and enable it again after you click another button?

I cannot figure this out
i'm making a windows form application with visual basic in c#
i have a scan button and it scans everything in the folder and lists all of the files in the listbox
if you click it another time the list of files appear again
how can you make it so you can only press the scan button once, and then you can press it again if you click the browse button?
the browse button is to select the folder you want to scan
thanks
This is pretty trivial
private void ScanButtonClick(object sender, EventArgs e)
{
// do something
(sender as Button).Enabled = false;
}
private void BrowseButtonClick(object sender, EventArgs e)
{
ScanButton.Enabled = true;
}
Its a bit unclear if you're writing in C# or vb.net, but since the question is tagged as C#...
private void btnScan_Click(object sender, EventArgs e) {
btnScan.Enabled = false;
// other code here
}
private void btnBrowse_Click(object sender, EventArgs e) {
btnScan.Enabled = true;
//other code here
}
I tried this in my windows form application in C# and it works fine!
private void button3_Click_1(object sender, EventArgs e)
{
int count = 0;
count++;
//add your code here
if (count == 1) {
button3.Enabled = false;
//only one click allowed
}
}

Categories

Resources