Picker gives me an ArgumentOutOfRangeException, why is this happening? - c#

I need my picker to hide elements if authorization == true.
private async void Picker_Unfocused(object sender, FocusEventArgs e)
{
try
{
await DisplayAlert("try", picker.SelectedIndex.ToString(), "OK");
if (response.domains[picker.SelectedIndex].authorization == true)
{
userNameEntry.IsVisible = false;
passwordEntry.IsVisible = false;
userLabel.IsVisible = false;
}
else
{
userNameEntry.IsVisible = true;
passwordEntry.IsVisible = true;
userLabel.IsVisible = true;
}
}
catch(System.ArgumentOutOfRangeException)
{
await DisplayAlert("catch", picker.SelectedIndex.ToString(), "OK");
}
}
This won't help. I get an ArgumentOutOfRangeException. Does anyone know why? It is important to me that this works.
Edit: Code is now current code. displayalerts just give me 0 if I select the first item, 1 if I select the second, etc. I still don't know what's going on. If the value of SelectedIndex is 0(or whatever I select) I shouldn't get an ArgumentOutOfRangeException, right?

When the picker is not yet initialized or can view a null value and is thus empty, the SelectedIndex property will give you a value of -1. This value is not valid for use in an array.
You should enrich your code to account for this possibility, for instance like this:
if (picker.SelectedIndex > -1 && response.domains[picker.SelectedIndex].authorization == true)
{
userNameEntry.IsVisible = false;
passwordEntry.IsVisible = false;
userLabel.IsVisible = false;
}
else
{
userNameEntry.IsVisible = true;
passwordEntry.IsVisible = true;
userLabel.IsVisible = true;
}

Related

DataGridViewCheckBox CheckedChange Event

I added Checkboxcolumn near my columns.
i googling a lot but i cant find a good answer.
I Want to make Btn_Edit.Visiable=false; if checkbox checked count is up to 1.
after my researches i go to Use CellContentClick event but it isn't work fine.
My Code:
private void GrdVw_Reception_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
if (GrdVw_Reception.CurrentCellAddress.X == 0)
{
int UpTpOne = 0;
bool flag = false;
for (int i = 0; i < GrdVw_Reception.Rows.Count; i++)
{
if (Convert.ToBoolean(GrdVw_Reception.Rows[i].Cells["checkColumn"].Value) == true)
{
UpTpOne = UpTpOne + 1;
if (UpTpOne == 1)
{
flag = true;
}
else
{
flag = false;
}
}
}
if (flag == true)
{
Btn_Edit.Visible = true;
}
else
{
Btn_Edit.Visible = false;
}
}
}
In this step when i run the program and click on checkboxes for first Btn_Edit dont go to true and for secound click Btn_Edit.Visiable go to true.
I want checkedchange event or an event that when i click on checkbox go to event codes in that moment.
(Sorry for bad English)
I proved it myself and had to do this to work:
private void GrdVw_Reception_CellClick(object sender, DataGridViewCellEventArgs e)
{
if(e.ColumnIndex == TheColumnIndexOfTheCheckBoxColumn){
bool currentValue = !(bool)GrdVw_Reception.Rows[e.RowIndex].Cells[0].Value;
GrdVw_Reception.Rows[e.RowIndex].Cells[0].Value = currentValue;
GrdVw_Reception.EndEdit();
MessageBox.Show("Value " + currentValue.ToString());
}
}
When the even click happens the value haven't change, so, i changed it myself and call EndEdit() method of the dataGridView. It works that way.
private void GrdVw_Reception_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
if (GrdVw_Reception.CurrentCellAddress.X == 0)
{
if (Convert.ToBoolean(GrdVw_Reception.Rows[e.RowIndex].Cells[0].Value) == false)
{
GrdVw_Reception.Rows[e.RowIndex].Cells[0].Value = CheckState.Checked;
}
else
{
GrdVw_Reception.Rows[e.RowIndex].Cells[0].Value = CheckState.Unchecked;
}
int UpTpOne = 0;
bool flag = false;
for (int i = 0; i < GrdVw_Reception.Rows.Count; i++)
{
if (Convert.ToBoolean(GrdVw_Reception.Rows[i].Cells["checkColumn"].Value) == true)
{
UpTpOne++;
if (UpTpOne == 1)
{
flag = true;
}
else
{
flag = false;
}
}
}
if (UpTpOne == 0)
{
Btn_DelRecord.Visible = false;
}
else
{
Btn_DelRecord.Visible = true;
}
if (flag == true)
{
Btn_Edit.Visible = true;
}
else
{
Btn_Edit.Visible = false;
}
}
}
In this code i made value of checkbox to true by code and then counting number of checkboxescell that has been checked.
I wanted make visiable of Btn_Edit go to false if the checked boxes number is up to one and.....
You should make checkbox tick to ticked and value of cell to true by code because when you click on gridviewcellcheckbox for first, the row go to selecting and in secound click the value going to be true.
I googled a lot but can't reach any good answer and this code is a total of my thinking if you have any good idea for do it better please call me.

TreeListView and hierarchical checkboxes

I am using TreeListView with:
this.tlv.CheckBoxes = true;
this.tlv.TriStateCheckBoxes = true;
this.tlv.HierarchicalCheckboxes = true;
Hierarchical with tristate works well, except one: the user can set the CheckState.Indeterminate by clicking the mouse, and I don't need it. For this I use 2 delegate that are not working correctly. How to make that work?
this.tvl.CheckStateGetter = delegate(object rowObject)
{
if (((ModelData)rowObject).IsChecked == true)
{
return CheckState.Checked;
}
else
{
if (((ModelData)rowObject).IsChecked == false)
{
return CheckState.Unchecked;
}
else
{
return CheckState.Indeterminate;
}
}
};
this.tvl.CheckStatePutter = delegate(object rowObject, CheckState newValue)
{
if (((ModelData)rowObject).Child.Count > 0)
{
if ((((ModelData)rowObject).Child.Where(x => x.IsChecked != null).Any(x => (bool)x.IsChecked) &&
((ModelData)rowObject).Child.Where(x => x.IsChecked != null).Any(x => !(bool)x.IsChecked)) ||
(((ModelData)rowObject).Child.Any(x => x.IsChecked == null)))
{
((ModelData)rowObject).IsChecked = null;
return CheckState.Indeterminate;
}
else
{
if (((ModelData)rowObject).Child.Where(x => x.IsChecked != null).All(x => (bool)x.IsChecked))
{
((ModelData)rowObject).IsChecked = true;
return CheckState.Checked;
}
else
{
((ModelData)rowObject).IsChecked = false;
return CheckState.Unchecked;
}
}
}
else
{
((ModelData)rowObject).IsChecked = (newValue == CheckState.Checked) ? true : false;
return newValue;
}
};
According to the documentation, "CheckStateGetters" are not allowed in the treelistview.
From the webpage:
One major problem is that we don’t know the checkedness of all the
subitems. When an ObjectListView has a CheckStateGetter installed, the
only way we can know if an item is checked is by calling the
CheckStateGetter on that item. We can’t reason about what is checked
or unchecked – we always have to ask. In our disk browser example, we
would have to ask all 700,000 items if it was checked. That’s never
going to work, so with hierarchical checkboxes, we don’t allow
CheckStateGetters to be installed.
http://objectlistview.sourceforge.net/cs/blog7.html

Is there something wrong with my If Statements?

This function checks if the values in the text boxes are parsable or not. This method is called upon in the method below this.
private bool CheckForInvalidEntries()
{
bool ParseIsSuccessfull; int result; //These 2 variables are for trying to parse the entries in the Stat text boxes
bool ContainsInvalidEntry = false;
if ((ParseIsSuccessfull = int.TryParse(P1STRtextbox.Text, out result)) == false)
{
ContainsInvalidEntry = true;
}
else if ((ParseIsSuccessfull = int.TryParse(P1DEXtextbox.Text, out result)) == false)
{
ContainsInvalidEntry = true;
}
else if ((ParseIsSuccessfull = int.TryParse(P1VIGtextbox.Text, out result)) == false)
{
ContainsInvalidEntry = true;
}
else if ((ParseIsSuccessfull = int.TryParse(P1RMtextbox.Text, out result)) == false)
{
ContainsInvalidEntry = true;
}
else if ((ParseIsSuccessfull = int.TryParse(P1BMtextbox.Text, out result)) == false)
{
ContainsInvalidEntry = true;
}
else ContainsInvalidEntry = false;
return ContainsInvalidEntry;
}
This function is the event where if the process stat points button is clicked
private void p1ProcessPointsBtn_Click(object sender, EventArgs e)
{
bool EntriesAreInvalid = new bool();
EntriesAreInvalid = CheckForInvalidEntries();
if (EntriesAreInvalid == true)
{
P1STRtextbox_TextChanged(sender, e);
P1DEXtextbox_TextChanged(sender, e);
P1VIGtextbox_TextChanged(sender, e);
P1RMtextbox_TextChanged(sender, e);
P1BMtextbox_TextChanged(sender, e);
}
else
{
MessageBox.Show("Success");
}
FUNCTIONALITY: When the user presses the "Process Stat Points" button, the program checks whether the entries in the 5 text boxes are able to be parsed(in the CheckForInvalidEntries method). It then returns a bool value to the EntriesAreInvalid variable(in the p1ProcessPointsBtn_Click method). If the entries are not parsable, do action A, if the entries are parsable, do action B.
PROBLEM: If the numbers are parsable in all the text boxes, I don't get a result. Im only getting results if the text boxes are not parsable. I think it has something to do with the if statements within the "CheckForInvalidEntries" method. What can I do to fix my problem. Your time and effort is greatly appreciated!
TryParse sets Result to zero if the conversion fails. Since you keep calling TryParse you keep resetting Result.
If you only want to check for parsing errors, this ought to work:
ContainsInvalidEntry = false;
ContainsInvalidEntry |= !int.TryParse(P1STRtextbox.Text, out result));
ContainsInvalidEntry |= !int.TryParse(P1DEXtextbox.Text, out result));
ContainsInvalidEntry |= !int.TryParse(P1VIGtextbox.Text, out result));
ContainsInvalidEntry |= !int.TryParse(P1RMtextbox.Text, out result));
ContainsInvalidEntry |= !int.TryParse(P1BMtextbox.Text, out result));
return ContainsInvalidEntry;
Aside: Comparing boolean values to true and false is a bit (Pardon the pun.) strange. if ( ( ParsedOkay == ( false ) ) ) may be valid, but if ( !ParsedOkay ) is more common.
i was too confused by your code. But i think this will help you. If i got you right you wanted to check if all the text in the textboxes is parsable to an int. And if it is so you wanted to print out "succes";
private bool isParsable(TextBox t) //takes a TextBox as paramater and returns true if
{ // its parsable
int i = 4;
if (int.TryParse(t.Text, out i) == true)
return true;
else
return false;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if(isParsable(tbox1) == true && isParsable(tbox2) == true) //if every textbox
{ //is parsable print
tblock1.Text = "succes";
}
else
{
tblock1.Text = "error";
}
}
First of all you don't need to write your if statements like this, it's too messy and unnecessary. Complex code always prone to error,
Instead of this:
if ((ParseIsSuccessfull = int.TryParse(P1STRtextbox.Text, out result)) == false)
You could write:
if(!int.TryParse(P1STRtextbox.Text, out result))
Because TryParse already returning a bool result.If you want check whether it's false just put negation operator (!) beginning of your statement. Also you can write a simple method to check whether your texts are parsable or not:
static bool CheckForParse(params string[] values)
{
int x;
if(values.Lenght > 0)
{
for(int i=0; i<values.Lenght;i++)
{
if(!int.TryParse(values[i], x)) return false;
}
return true;
} else { return false }
return false;
}
And you can call it like this:
bool result = CheckForParse(P1STRtextbox.Text,
P1DEXtextbox.Text,
P1VIGtextbox.Text,
P1RMtextbox.Text,
P1BMtextbox.Text);
if(result)
{
P1STRtextbox_TextChanged(sender, e);
P1DEXtextbox_TextChanged(sender, e);
...
}

c# event fires windows form incorrectly

I'm trying to understand what's happening here. I have a CheckedListBox which contains some ticked and some un-ticked items. I'm trying to find a way of determining the delta in the selection of controls. I've tried some cumbersome like this - but only works part of the time, I'm sure there's a more elegant solution. A maybe related problem is the myCheckBox_ItemCheck event fires on form load - before I have a chance to perform an ItemCheck. Here's what I have so far:
void clbProgs_ItemCheck(object sender, ItemCheckEventArgs e)
{
// i know its awful
System.Windows.Forms.CheckedListBox cb = (System.Windows.Forms.CheckedListBox)sender;
string sCurrent = e.CurrentValue.ToString();
int sIndex = e.Index;
AbstractLink lk = (AbstractLink)cb.Items[sIndex];
List<ILink> _links = clbProgs.DataSource as List<ILink>;
foreach (AbstractLink lkCurrent in _links)
{
if (!lkCurrent.IsActive)
{
if (!_groupValues.ContainsKey(lkCurrent.Linkid))
{
_groupValues.Add(lkCurrent.Linkid, lkCurrent);
}
}
}
if (_groupValues.ContainsKey(lk.Linkid))
{
AbstractLink lkDirty = (AbstractLink)lk.Clone();
CheckState newValue = (CheckState)e.NewValue;
if (newValue == CheckState.Checked)
{
lkDirty.IsActive = true;
}
else if (newValue == CheckState.Unchecked)
{
lkDirty.IsActive = false;
}
if (_dirtyGroups.ContainsKey(lk.Linkid))
{
_dirtyGroups[lk.Linkid] = lkDirty;
}
else
{
CheckState oldValue = (CheckState)e.NewValue;
if (oldValue == CheckState.Checked)
{
lkDirty.IsActive = true;
}
else if (oldValue == CheckState.Unchecked)
{
lkDirty.IsActive = false;
}
_dirtyGroups.Add(lk.Linkid, lk);
}
}
else
{
if (!lk.IsActive)
{
_dirtyGroups.Add(lk.Linkid, lk);
}
else
{
_groupValues.Add(lk.Linkid, lk);
}
}
}
Then onclick of a save button - I check whats changed before sending to database:
private void btSave_Click(object sender, EventArgs e)
{
List<AbstractLink> originalList = new List<AbstractLink>(_groupValues.Values);
List<AbstractLink> changedList = new List<AbstractLink>(_dirtyGroups.Values);
IEnumerable<AbstractLink> dupes = originalList.ToArray<AbstractLink>().Intersect(changedList.ToArray<AbstractLink>());
foreach (ILink t in dupes)
{
MessageBox.Show("Changed");
}
if (dupes.Count() == 0)
{
MessageBox.Show("No Change");
}
}
For further info. The definition of type AbstractLink uses:
public bool Equals(ILink other)
{
if (Object.ReferenceEquals(other, null)) return false;
if (Object.ReferenceEquals(this, other)) return true;
return IsActive.Equals(other.IsActive) && Linkid.Equals(other.Linkid);
}
There's little point that I see to do this in the ItemCheck event. Just calculate the delta when you save. Cuts out a bunch of code and trouble with spurious events.

Check if flowLayoutPanel is empty in c#

I want to make an error label come up when my flowLayoutPanel is empty, but i don't know how to check that the flowLayoutPanel is empty. This is my current code:
private void flowLayoutPanel1_ControlRemoved(object sender, ControlEventArgs e)
{
if (flowLayoutPanel1.Controls == null)
{
customtoolwarning.Visible = true;
}
else
{
customtoolwarning.Visible = false;
}
}
Please Help,
Thanks
private void flowLayoutPanel1_ControlRemoved(object sender, ControlEventArgs e)
{
if (flowLayoutPanel1.Controls.Count > 0)
{
customtoolwarning.Visible = true;
}
else
{
customtoolwarning.Visible = false;
}
}
The problem you're running into is you're checking Controls for null to determine if it's empty. The Controls property won't ever be null but instead will be non-null and have 0 length when empty. For example
if (flowLayoutPanel1.Controls.Count == 0) {
// It's empty
}
lblNoContacts.Visible = (flowLayoutPanel.Controls.Count == 0) ? true : false;

Categories

Resources