Hiding and showing panels - c#

EDIT
I've found and posted the solution.
I am trying to make an installer for my application and I am trying to do that with panels (I don't know if this is a good way of doing it, but this gives me more customization options instead of using the install shield program). What would be the best way to do this?
This is the code I have right know:
C# Code
foreach (var c in Controls)
{
if (!(c is Panel)) continue;
if (c.Name == "pnlBottom") continue;
c.Visible = c.Name.Contains(_currentPanel.ToString());
if (c.Visible) return;
}

Try this, it changes the Visibility of a single Panel:
private void PanelVisible(string panelName, bool visible)
{
var panel = this.Controls.OfType<Panel>().FirstOrDefault(p => p.Name == panelName);
if (panel != default(Panel)) panel.Visible = visible;
}
If you want to make all Invisible, but one:
private void PanelVisible(string panelName)
{
foreach(var panel in this.Controls.OfType<Panel>().Where(p=>p.Name!="pnlBottom"))
{
panel.Visible = panel.Name == panelName;
}
}

This is the code I use to show a panel:
foreach (var c in Controls)
{
if (!(c is Panel)) continue;
if (c.Name == "pnlBottom") continue;
c.Visible = c.Name.Contains(_currentPanel.ToString());
if (c.Visible) return;
}
And this is the code I use to call it and browse through my panels:
Next button
private void btnNext_Click(object sender, EventArgs e)
{
if (pnlContent1.Visible) { ShowPanel("2"); return; }
if (pnlContent2.Visible) { ShowPanel("3"); return; }
if (pnlContent3.Visible) { ShowPanel("4"); return; }
if (pnlContent4.Visible) { ShowPanel("5"); return; }
}
Back button
private void btnBack_Click(object sender, EventArgs e)
{
if (pnlContent2.Visible) { ShowPanel("1"); return; }
if (pnlContent3.Visible) { ShowPanel("2"); return; }
if (pnlContent4.Visible) { ShowPanel("3"); return; }
if (pnlContent5.Visible) { ShowPanel("4"); return; }
}
I hope this will be use to someone else as well :D!

Related

How to enable button after all textboxes are not empty in c# winforms?

How can I make button property set to enabled=true after all my textboxes are not empty?
I'm learning programming and my apps are simple.
I know how to enable this property when one of my textboxes have text but this is not the case.
Use case is that user need to put data in both textboxes and after that will be able to click btn.
How in most simple way can I validate all form and then enable button?
There are just 2 tb:
https://i.imgur.com/JUslNWE.png
You need to create a TextBox_TextChanged event and subscribe to all text boxes.
private void TextBox_TextChanged(object sender, EventArgs e)
{
int notEmptyTextBoxCount = 0;
int textBoxCount = 0;
foreach (var item in Controls)
{
if (item is TextBox txtb)
{
textBoxCount++;
if (txtb.Text != String.Empty)
notEmptyTextBoxCount++;
}
}
if (textBoxCount == notEmptyTextBoxCount)
button.Enabled = true;
else
button.Enabled = false;
}
Thanks guys for all feedback.
I have managed to do this this way:
private void ValidateTextBoxes()
{
if (loginTextBox.Text.Length != 0 && passTextBox.Text.Length != 0)
{
generateHashBtn.Enabled = true;
}
else
{
generateHashBtn.Enabled = false;
}
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
ValidateTextBoxes();
}
private void TextBox2_TextChanged(object sender, EventArgs e)
{
ValidateTextBoxes();
}

Add an OnLeave function to 140 objects?

I got a windows form with around 140 numericUpDown elements and want all of them to do this:
private void numericUpDown_B1_RS_LS_Leave(object sender, EventArgs e)
{
if (numericUpDown_B1_RS_LS.Value < 1 || numericUpDown_B1_RS_LS.Value > 6)
{
numericUpDown_B1_RS_LS.BackColor = Color.Red;
}
else
{
numericUpDown_B1_RS_LS.BackColor = Color.White;
}
}
Is there a more comfortable way of doing this than just adding 140 functions to the form manually?
Assuming you want the same method for all NUDs on your form this will do it:
public Form1()
{
InitializeComponent();
foreach (Control ctl in this.Controls)
if (ctl.GetType() == typeof(NumericUpDown) ) ctl.Leave += numericUpDown_Leave;
}
private void numericUpDown_Leave(object sender, EventArgs e)
{
NumericUpDown NumericUD = (NumericUpDown) sender ;
if (NumericUD.Value < 1 || NumericUD.Value > 6)
{
NumericUD.BackColor = Color.Red;
}
else
{
NumericUD.BackColor = Color.White;
}
}
You can just as easily iterate over the Controls collection of another container, say a Panel or a GroupBox if the NUDs are not directly sitting on the Form. And if some NUDs should be excepted from that behaviour you could mark them in some way, maybe in their Tag or Name and check for that before adding their handlers ..
You should not write multiple functions for same code. Instead you can create just one function and assign it to the event method of your numericUpDown element i.e. "UpDown" event.
So whenever any of the element's "UpDown" event will fire, your same function will be executed. And as far as you want to write different methods for different elements, then consider that in your method
Method(object sender, EventArguments e)
sender is the actual sender object of your updownElement type, and that code will be executed for that particular object only.
You can take for loop to iterate your all 140 elements and assign this function to the "UpDown" element.
maybe you could write a static method to handle the adding of event handler recursively, something like:
private void frmMain_Load(object sender, EventArgs e)
{
AddHandleNumericUpdownLeave(this);
}
public static void AddHandleNumericUpdownLeave(Control theContrl)
{
if (theContrl.Controls != null && theContrl.Controls.Count > 0)
{
foreach (Control cControl in theContrl.Controls)
{
AddHandleNumericUpdownLeave(cControl);
}
}
else
{
NumericUpDown nudCtrl = theContrl as NumericUpDown;
if (nudCtrl != null)
{
nudCtrl.Leave += (object senderT, EventArgs eT) =>
{
var tmpCtrl = senderT as NumericUpDown;
if (tmpCtrl != null)
{
if (tmpCtrl.Value < 1 || tmpCtrl.Value > 6)
{
tmpCtrl.BackColor = Color.Red;
}
else
{
tmpCtrl.BackColor = Color.White;
}
}
}
}
}
}
but this might be costly if you have too many controls in a form...

Disable controls when a specific combo-box item is selected

I'm writing a fairly simple application in which a user can select certain inputs and it processes data. I am having a problem in when a user selects a specific string from one of the combo-boxes, it doesn't disable other controls. Basically when the user selects the combo-box and selects 'Initiate' all other controls should be disabled.
private void cbalpha_SelectedIndexChanged(object sender, EventArgs e)
{
var operatorcheckbox = (CheckBox)sender;
foreach (Control cb in this.Controls)
if ((StringtDataChoiceorSelect.SelectedItem != "(Initiate)")
{
continue;
}
else
{
cb.Enabled = operatorcheckbox.Checked;
}
}
if you want to diable all the controls why are you using checkbox value to enable disable. It should be simply this
private void cbalpha_SelectedIndexChanged(object sender, EventArgs e)
{
bool isEnabled = string.Compare(StringtDataChoiceorSelect.SelectedItem.ToString(),"(Initiate)",StringComparison.OrdinalIgnoreCase) == 0;
foreach (Control cb in this.Controls)
cb.Enabled = !isEnabled ;
}
try
private void cbalpha_SelectedIndexChanged(object sender, EventArgs e)
{
var operatorcheckbox =(CheckBox)sender;
foreach (Control cb in this.Controls)
{
if ((StringtDataChoiceorSelect.SelectedItem != "(Initiate)")
{
if(!cb.Enabled)
{
cb.Enabled = true;
}
}
else
{
cb.Enabled = false;
}
}
}
on SelectedIndexChanged event check if the selected objected is same on which you want to disable other control and then disable it.
protected void ddltext_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlPerc.SelectedValue.Equals("0.01"))
{
txtbox.Enabled = false;
}
}
I'd use delegates and events for this.
if(operatorcheckbox.Checked &&
StringtDataChoiceorSelect.SelectedItem == "(Initiate)")
{
EventHandler -= checkboxdelegate;
}
else
EventHandler += checkboxdelegate;
You can add a method if there's controls you do not want to keep enabled.

Enable button by event handling

Have a tab page with 2 panels, a data grid view and a 'clear' button.There are only textboxes in the panels and the grid is unbound. Data input is by user. The clear btn is disabled by default. My requirement is to enable it only if any of the textboxes is not empty or there is more than 1 row in the grid. This code isn't working. Please help.
public Form1()
{
InitializeComponent();
foreach (Control c in InvoiceTab.Controls)
{
if (c is DataGridView)
{
DataGridView dgv = c as DataGridView;
if (dgv.RowCount > 1)
{
EnableClearBtnBool = true;
btnClear.Enabled = true;
break;
}
else
{
EnableClearBtnBool = false;
btnClear.Enabled = false;
break;
}
}
}
foreach (Control c1 in panel1.Controls)
{
if (c1 is TextBox)
{
if (c1.Text != "")
{
EnableClearBtnBool = true;
c1.TextChanged -= EnableClearBtn;
c1.TextChanged += EnableClearBtn;
break;
}
else
EnableClearBtnBool = false;
}
}
foreach (Control c2 in panel2.Controls)
{
if (c2 is TextBox)
{
if (c2.Text != "")
{
EnableClearBtnBool = true;
c2.TextChanged -= EnableClearBtn;
c2.TextChanged += EnableClearBtn;
break;
}
else
EnableClearBtnBool = false;
}
}
}
bool EnableClearBtnBool = false;
private void EnableClearBtn(object sender, EventArgs e)
{
if (EnableClearBtnBool == true)
btnClear.Enabled = true;
else
btnClear.Enabled = false;
}
That code is almost certainly not working because of its location first and foremost. However, there are some fundamental changes we should be able to make as well. First we're going to need this code in a method that can be called frequently:
private void RefreshClearButton()
{
btnClear.Enabled = textBox1.Text.Length > 0 ||
textBox2.Text.Length > 0 || ...
dataGridView.RowCount > 1;
}
but, we also need to leverage the TextChanged event on all of the text boxes:
private void textBox_TextChanged(object sender, EventArgs e)
{
RefreshClearButton();
}
so you need to hook all of them up to this event handler. Now, we have two more events we need to consume, on the DataGridView, RowsAdded and RowsRemoved:
private void dataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
RefreshClearButton();
}
private void dataGridView_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
RefreshClearButton();
}
and so now we're notified every time something changes. If you must iterate through the panels rather than naming every single text box along the way then you might want to do something like this:
private bool HasTextBeenEntered(ControlCollection controls)
{
foreach (var c in controls)
{
if (c is TextBox && ((TextBox)c).Text.Length > 0) { return true; }
else if (c is Control && HasTextBeenEntered(((Control)c).Controls)) { return true; }
}
return false;
}
which would change the RefreshClearButton method slightly:
private void RefreshClearButton()
{
btnClear.Enabled = HasTextBeenEntered(this.Controls) || dataGridView.RowCount > 1;
}
DISCLAIMER: none of this code is compiled so don't be surprised if you have to tweak it, but it will get you more than 90% of the way.

Cut, Copy, and paste in C#?

I have a lot of textboxes. I have a button that will cut the selected text of the Focused textbox. How do i do that? I have tried this:
if (((TextBox)(pictureBox1.Controls[0])).SelectionLength > 0)
{
((TextBox)(pictureBox1.Controls[0])).Cut();
}
Hope it is WinForms
var textboxes = (from textbox in this.Controls.OfType<TextBox>()
where textbox.SelectedText != string.Empty
select textbox).FirstOrDefault();
if (textboxes != null)
{
textboxes.Cut();
}
Loop through the controls to find the one with selected text:
foreach (Control x in this.PictureBox1.Controls)
{
if (x is TextBox)
{
if (((TextBox)x).SelectionLength > 0)
{
((TextBox)(x).Cut(); // Or some other method to get the text.
}
}
}
Hope this helps!
Try using common Enter and Leave events to set the last TextBox that had Focus.
private void textBox_Enter(object sender, EventArgs e)
{
focusedTextBox = null;
}
private void textBox_Leave(object sender, EventArgs e)
{
focusedTextBox = (TextBox)sender;
}
private void button1_Click(object sender, EventArgs e)
{
if (!(focusedTextBox == null))
{
if (focusedTextBox.SelectionLength > 0)
{
Clipboard.SetText(focusedTextBox.SelectedText);
focusedTextBox.SelectedText = "";
focusedTextBox = null;
}
}
}

Categories

Resources