I have several panels on a form that I want to appear corresponding to a numericUpDown value.
(ie- panel 1 is visible when the value is 1, panels 1 and 2 are visible when the number is 2, panels 1 2 and 3 are visible when the value is 3, ect)
I am able to get the initial panel to function as expected with my existing code, but the subsequent ones are not appearing or disappearing as I intended. I'm not quite sure why. Is it because the value of the NUP is not updating when it is changed?
Code:
private void petNumNumericUpDown_ValueChanged(object sender, EventArgs e)
{
if ((petNumNumericUpDown.Value == 1) || (petNumNumericUpDown.Value == 2) ||(petNumNumericUpDown.Value == 3) || (petNumNumericUpDown.Value == 4) || (petNumNumericUpDown.Value == 5))
{
pet1Panel.Visible = true;
}
else
{
pet1Panel.Visible = false;
}
if((petNumNumericUpDown.Value == 2) || (petNumNumericUpDown.Value == 3) || (petNumNumericUpDown.Value == 4) || (petNumNumericUpDown.Value == 5))
{
pet2Panel.Visible = true;
}
else
{
pet2Panel.Visible = false;
}
}
I'm looking to have this continue up until 5. Any insight on what I'm doing wrong would be appreciated.
You can write simpler code to achieve your goal:
pet1Panel.Visible = (petNumNumericUpDown.Value >= 1);
pet2Panel.Visible = (petNumNumericUpDown.Value >= 2);
...
Related
Let's say I have a 5 textbox and 1 combobox and I make the value inputted pass to a variable like this
name = textboxName.Text.ToString();
address = txtboxAddress.Text.ToString();
destination = comboboxDestination.Text.ToString(); // combobox
position = txtboxPosition.Text.ToString();
station = txtboxStation.Text.ToString();
purpose = richtxtboxPurpose.Text.ToString();
Then I wanted to check if their value is null or just blank space.
int switchCase = 0;
if (string.IsNullOrWhiteSpace(name) && name.Length > 0 ||
string.IsNullOrWhiteSpace(address) && address.Length > 0 ||
string.IsNullOrWhiteSpace(destination) && destination.Length > 0 ||
string.IsNullOrWhiteSpace(position) && position.Length > 0 ||
string.IsNullOrWhiteSpace(station) && station.Length > 0 ||
string.IsNullOrWhiteSpace(purpose) && purpose.Length > 0
)
{
switchCase = 2;
}
else
{
switchCase = 1;
}
then for my trap is this
switch (switchCase)
{
case 1:
MessageBox.Show("Profile created!");
break;
case 2:
MessageBox.Show("Please complete the form to procede.");
break;
}
The problem is if I just load up the page and didnt typed anything it is going to the case 1 which means that all of my textboxes and combobox have a value. Then if I key in just space in all of the textboxes it reads the condition and go to case 2 what is going on?
EDIT:
My first code is like this and it is working well
if (name == "" || address == "" || destination == "" || position == "" || station == "" || purpose == "")
Although it is prone to the blank space.
Your code here:
string.IsNullOrWhiteSpace(name) && name.Length > 0 is checking if the string is empty, entirely whitespace, or null. Then you add a length check, effectively changing it into 'only accept strings which contain whitespace, but not blank.
Removing the length check should work as expected.
In addition, you can clean up the code a bit, to reduce repetitiveness:
if (new[] { name, address, destination, position, station, purpose }
.Any(str => string.IsNullOrEmpty(str)))
{
....
}
I ran into little problem today as I was working on my game...
The problem is I have this condition running on 1 milisecond timer :
if (jump == true &&
jumped == 0 &&
(Player.Location == new Point(Player.Location.X, Block1.Location.Y - Player.Height) ||
Player.Top == this.Height - Player.Height))
{
do something...
}
The "Block1" is one object in the game (Picturebox) and I need like 10 or 20 or even 100 more of these blocks with the same condition, so how could I simplify it? It would be 50 or even more lines of one condition. Basically I would like to know if there is a way of mixing all "Blocks" (pictureboxes) into a group (named Blocks) or something I could still access with Blocks.Location.Y etc
There are too many conditions in this single check, IMO. I'd break them down and then work on each condition in a stand-alone manner which will make things easier to debug as well as to read/comprehend in the future.
// Too much going on here; let's refactor.
if (jump == true
&& jumped == 0
&& (Player.Location == new Point(Player.Location.X, Block1.Location.Y - Player.Height)
|| Player.Top == this.Height - Player.Height))
{
//do something...
}
Instead of creating a large if statement, pull the conditions into a single method:
// first refactor
private bool IsValidForSomeAction()
{
if(!jump)
{
return false;
}
if(jumped != 0)
{
return false;
}
if(Player.Top == this.Height - Player.Height)
{
return true;
}
if (Player.Location == new Point(Player.Location.X, Block1.Location.Y - Player.Height))
{
return true;
}
return false;
}
After the first refator, it becomes clear that there is no need to create a new Point for the final comparison:
// second refactor
private bool IsValidForSomeAction()
{
if(!jump)
{
return false;
}
if(jumped != 0)
{
return false;
}
if(Player.Top == this.Height - Player.Height)
{
return true;
}
// only the Y location matters, no need to create a new Point for the comparison.
if (Player.Location.Y == Block1.Location.Y - Player.Height)
{
return true;
}
return false;
}
Now, let's focus on what really matters: if (Player.Location.Y == Block1.Location.Y - Player.Height). The condition boils down to the difference between the Block's Y location and the Player's Height.
Given that there may be 10, 20, 50, or 100+ Blocks to compare, then create a private field containing a collection of all the Blocks.
// override the onload event and find all the picture boxes:
private readonly List<PictureBox> _boxes = new List<PictureBox>();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_boxes.AddRange(this.Controls.OfType<PictureBox>()
}
The _boxes field can then be used for the final validation:
// third refactor
private bool IsValidForSomeAction()
{
if(!jump)
{
return false;
}
if(jumped != 0)
{
return false;
}
if(Player.Top == this.Height - Player.Height)
{
return true;
}
// only the Y location matters, no need to create a new Point for the comparison.
if(_boxes.Any(x => x.Location.Y - Player.Height == Player.Location.Y)
{
return true;
}
return false;
}
I have a list of YES/NO questions and each question has a radio button indicating the answer. When the user selects YES, a panel will be visible and it has textboxes inside it for the additional required input. When the user answers YES, they MUST fill in the textboxes that appear.
Currently I'm hard-coding it this way:
if (txtQ1Specify.Visible == true)
{
if (txtQ1Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ2Specify.Visible == true)
{
if (txtQ2Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ3Specify.Visible == true)
{
if (txtQ3Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ4SpecifyCompany.Visible == true || txtQ4SpecifyRelative.Visible == true)
{
if (txtQ4SpecifyCompany.Text.Length == 0 || txtQ4SpecifyRelative.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ5SpecifyCompany.Visible == true || txtQ5SpecifyRelative.Visible == true)
{
if (txtQ5SpecifyCompany.Text.Length == 0 || txtQ5SpecifyRelative.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ6Specify.Visible == true)
{
if (txtQ6Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
if (txtQ7Specify.Visible == true)
{
if (txtQ7Specify.Text.Length == 0)
{
lblError.Text = "Please answer all questions.";
}
}
After this checking I want to execute some code.
The page looks like this:
How can I check for textbox inputs based in visibility?
You could use LINQ to find out if there are any visible and empty TextBoxes like so:
var query =
from t in Page.Controls.OfType<TextBox>()
where t.Visible && t.Text == ""
select t;
bool hasUnanswered = query.Any();
This can be easily done on the client side .
First you need to identify which all text box are visible . For this you can use jquery Visible Selector
Now If more than one text box is visible . Show some message to the user and focus and highlight the text box which need to be filled .
$(document).ready(function(){
var visibleCount =$('input[type="text"]:visible').length;
if (visibleCount > 0)
{
// Add your logic here
}
});
I managed to do it using a very long if statement. Here goes nothing:
if ((pnlQ1Yes.Visible == true && txtQ1Specify.Text.Length == 0) ||
(pnlQ2Yes.Visible == true && txtQ2Specify.Text.Length == 0) ||
(pnlQ3Yes.Visible == true && txtQ3Specify.Text.Length == 0) ||
(pnlQ4Yes.Visible == true && (txtQ4SpecifyCompany.Text.Length == 0 || txtQ4SpecifyRelative.Text.Length == 0)) ||
(pnlQ5Yes.Visible == true && (txtQ5SpecifyCompany.Text.Length == 0 || txtQ5SpecifyRelative.Text.Length == 0)) ||
(pnlQ6Yes.Visible == true && txtQ6Specify.Text.Length == 0) ||
(pnlQ7Yes.Visible == true && txtQ7Specify.Text.Length == 0))
{
lblError.Text = "Please answer all questions.";
}
else
{
...
}
It first checks if the panel is visible, then it checks the textbox inside the panel for values. Then I repeat this kind of checking for the rest of the panels and textboxes.
I created a textbox dynamically with a TextChangedEventArgs to restrict the textbox to enter only numbers and decimal point.
Following is the code in c#
const char Delete = (char)8;
if (Char.IsDigit(e.KeyChar))
{
e.Handled = false;
}
else if (e.KeyChar == Delete)
{
e.Handled = false;
}
else if (e.KeyChar == '.')
{
if (!(amt.Text.Contains(".")))
e.Handled = false;
else
{
e.Handled = true;
}
}
else
{
e.Handled = true;
}
But I can't use this in wpf.
I tried to change the code with e.key or e.Text. But both these are not available. It is showing the following error Are u missing an assembly or directive.
Please anyone help me.
// one solution for filtering characters in a textbox.
// this is the PreviewKeyDown handler for a textbox named tbNumerical
// Need to add logic for cancelling repeated decimal point and minus sign
// or possible notation like 1.23e2 == 123
private void tbNumerical_PreviewKeyDown(object sender, KeyEventArgs e)
{
System.Windows.Input.Key k = e.Key;
// to see the key enums displayed, use a textbox or label
// someTextBox.Text = k.ToString();
// filter out control keys, not all are used, add more as needed
bool controlKeyIsDown = Keyboard.IsKeyDown(Key.LeftShift);
if (!controlKeyIsDown &&
Key.D0 <= k && k <= Key.D9 ||
Key.NumPad0 <= k && k <= Key.NumPad9 ||
k == Key.OemMinus || k == Key.Subtract ||
k == Key.Decimal || k == Key.OemPeriod) // or OemComma for european decimal point
else
{
e.Handled = true;
// just a little sound effect for wrong key pressed
System.Media.SystemSound ss = System.Media.SystemSounds.Beep;
ss.Play();
}
}
How can I make some cells in DataGridView unselectable?
By 'unselectable' I mean: It cannot be selected in any way and trying to select it won't unselect any other cell.
I don't mean ReadOnly. My cells already have this property as true.
DataGridView.MultiSelect needs to be false.
Thanks to JYL's answer I wrote a code:
private int selectedCellRow = 0;
private int selectedCellColumn = 0;
private void grid_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
if (e.Cell == null || e.StateChanged != DataGridViewElementStates.Selected)
return;
if (e.Cell.RowIndex == 0 || e.Cell.ColumnIndex == 0 || e.Cell.RowIndex == 1 && e.Cell.ColumnIndex == 1)
{
e.Cell.Selected = false;
grid.Rows[selectedCellRow].Cells[selectedCellColumn].Selected = true;
}
else
{
selectedCellRow = e.Cell.RowIndex;
selectedCellColumn = e.Cell.ColumnIndex;
}
//this was only for seeing what is happening
//this.Text = selectedCellRow + " " + selectedCellColumn;
}
But this leads to StackOverflow. What condition and where I need to put to prevent that?
Added and commented the condition you were asking about.
private int selectedCellRow = 0;
private int selectedCellColumn = 0;
private void grid_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
if (e.Cell == null || e.StateChanged != DataGridViewElementStates.Selected)
return;
//if Cell that changed state is to be selected you don't need to process
//as event caused by 'unselectable' will select it again
if (e.Cell.RowIndex == selectedCellRow && e.Cell.ColumnIndex == selectedCellColumn)
return;
//this condition is necessary if you want to reset your DataGridView
if (!e.Cell.Selected)
return;
if (e.Cell.RowIndex == 0 || e.Cell.ColumnIndex == 0 || e.Cell.RowIndex == 1 && e.Cell.ColumnIndex == 1)
{
e.Cell.Selected = false;
grid.Rows[selectedCellRow].Cells[selectedCellColumn].Selected = true;
}
else
{
selectedCellRow = e.Cell.RowIndex;
selectedCellColumn = e.Cell.ColumnIndex;
}
}
You can use the event "CellStateChanged".
private void DataGridViewXYZ_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
if (e.Cell == null
|| e.StateChanged != DataGridViewElementStates.Selected)
return;
if (! [condition here : can this cell be selectable ?])
e.Cell.Selected = false;
}
EDIT : if you leave the MultiSelect property of gridView to True, you can manage yourself a "single select" gridview with unselectable cells : il the cell is selectable, clear the other selection...
I believe this article may prove useful to you:
http://blog.spencen.com/2009/04/25/readonly-rows-and-cells-in-a-datagrid.aspx
The ReadOnly property can be applied to the entire grid, a column, a row, or an individual cell.