I have a textbox in my form for user to key in an item code.
When the focus of the textbox is lost, it will look into the database to check if the item code exists or not.
However, I am getting infinite loop when I try to lose focus by clicking on other textboxes.
private void txtICode_LostFocus(object sender, RoutedEventArgs e)
{
if (txtICode.IsFocused != true)
{
if (NewData)
{
if (txtICode.Text != null)
{
if (txtICode.Text != "")
{
Item temp = new Item();
Item[] list = temp.Query(new object[] { Item.DataEnum.Item_Code }, new string[] { txtICode.Text });
if (list.Length > 0)
{
System.Windows.Forms.MessageBox.Show("This item code is already being used.", "Invalid information");
txtICode.Focus();
return;
}
}
}
}
}
}
The txtICode.IsFocused is set to true every time after the end of the method and the loop just continues forever.
I tried removing txtICode.Focus(); but it makes no difference.
Is there anything wrong with my code?
I am using .Net 3.5 and WPF for my form.
You do not have to restore focus to TextBox in the LostFocus event.
remove these 2 lines :
txtICode.Focus();
return;
You could implement code more clean & readable way :
private void txtICode_LostFocus(object sender, RoutedEventArgs e)
{
if (!NewData)
return;
if (String.IsNullOrEmpty(txtICode.Text))
return;
Item temp = new Item();
Item[] list = temp.Query(new object[] { Item.DataEnum.Item_Code }, new string[] { txtICode.Text });
if (list.Length > 0)
{
System.Windows.Forms.MessageBox.Show("This item code is already being used.", "Invalid information");
}
}
private void txtICode_LostFocus(object sender, RoutedEventArgs e)
{
string inputText = txtICode.Text;
if (string.IsNullOrEmpty(inputText) || !NewData)
{
return;
}
Item temp = new Item();
Item[] list = temp.Query(new object[] { Item.DataEnum.Item_Code },
new string[] { inputText });
if (list != null && list.Length > 0)
{
MessageBox.Show("This item code is already being used.", "Invalidinformation");
txtICode.Focus();
return;
}
}
You can use BeginInvoke Method to execute asynchronously:
private void txtICode_LostFocus(object sender, RoutedEventArgs e)
{
txtICode.Dispatcher.BeginInvoke(() => {
if (txtICode.IsFocused != true)
{
if (NewData)
{
if (txtICode.Text != null)
{
if (txtICode.Text != "")
{
Item temp = new Item();
Item[] list = temp.Query(new object[] { Item.DataEnum.Item_Code }, new string[] { txtICode.Text });
if (list.Length > 0)
{
System.Windows.Forms.MessageBox.Show("This item code is already being used.", "Invalid information");
txtICode.Focus();
return;
}
}
}
}
});
}
Related
There are 4 values in my dropdown list, and if there is an "x" value in it, I want to hide that value. How can I do this using the If command.
protected void dr_alans_DataBound(object sender, EventArgs e)
{
if (dd_mainAlan.SelectedItem.Text == "Yönetici")
{
if (dr_alans.Items.FindByValue("22").Value=="22")
{
dr_alans.Items.FindByValue("22").Enabled = false;
}
else if (dr_alans.Items.FindByValue("23").Value == "23")
{
dr_alans.Items.FindByValue("23").Enabled = false;
}
}
}
If it is ASP.NET Forms, you should be able to do it like this maybe. This should hide the items with an x in the Value.
foreach (var itm in dd_mainAlan.Items)
{
var it = (ListItem)itm;
it.Enabled = !it.Value.Contains("x");
}
I'm trying to list all items in a directory which has been successful. I need to check if the item's status is "Unchecked", and if it is to give me the name of it in a variable.
TL;DR: If item is unchecked, write item in variable.
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (checkedListBox1.GetItemChecked(e.Index) == true)
{
if (checkedListBox1.CheckedItems.Count != 0)
{
// If so, loop through all checked items and print results.
string s = "";
for (int x = 0; x < checkedListBox1.CheckedItems.Count; x++)
{
if (checkedListBox1.CheckedItems[x] == 0)
{
s = checkedListBox1.CheckedItems[x].ToString();
}
}
MessageBox.Show(s);
}
}
}
This is my current code.
To get a list of all unchecked items:
private void button1_Click_1(object sender, EventArgs e)
{
List<String> items = new List<String>();
for (int i=0; i<checkedListBox1.Items.Count; i++)
{
if (!checkedListBox1.GetItemChecked(i))
{
items.Add(checkedListBox1.Items[i].ToString());
}
}
// ... do something with "items" ...
foreach(String item in items)
{
Console.WriteLine(item);
}
}
Code:-
public partial class Players : Form
{
public TextBox[] spelers = new TextBox[7];
List<string> spelersSpel = new List<string>();
public Form1 game = new Form1();
public Players()
{
InitializeComponent();
spelers[0] = inputSpeler1;
spelers[1] = inputSpeler2;
spelers[2] = inputSpeler3;
spelers[3] = inputSpeler4;
spelers[4] = inputSpeler5;
spelers[5] = inputSpeler6;
}
private void btnSpelers_Click(object sender, EventArgs e)
{
for (int i = 0; i < spelers.Length; i++)
{
if (spelers[i] != null)
spelersSpel.Add(spelers[i].Text);
}
foreach (TextBox item in spelers)
{
if (item != null)
spelersSpel.Add(item.Text);
}
MessageBox.Show(spelersSpel.Count.ToString());
game.ShowDialog();
}
}
I've got 6 textboxes that I've put into an array. Then I want to put this array into a form. Because I don't know how much input I get from the textboxes I don't use an array. But when I want to see how big my list is. It's saying 6. If I only type text in the first textbox the list should be 1.
What am I doing wrong?
You can get the input count by this way:
spelersSpel.Clear();
foreach (TextBox item in spelers)
{
if (item != null && item.Text.Trim() != "") //check if null or empty
spelersSpel.Add(item.Text);
}
Because (spelers[i] != null) and (item != null) does not check whether the textboxes are blank; It means the array element is null or not.
If you want to check whether your textbox is blank, you should find property Text;
if (item.Text != "")
I'm not able to use my IDE and it is not sure, but try this.
public partial class Players : Form
{
public TextBox[] spelers = new TextBox[6]; // [7];
List<string> spelersSpel = new List<string>();
public Form1 game = new Form1();
public Players()
{
InitializeComponent();
spelers[0] = inputSpeler1;
spelers[1] = inputSpeler2;
spelers[2] = inputSpeler3;
spelers[3] = inputSpeler4;
spelers[4] = inputSpeler5;
spelers[5] = inputSpeler6;
}
private void btnSpelers_Click(object sender, EventArgs e)
{
/*
// 1.
for (int i = 0; i < spelers.Length; i++)
{
if (spelers[i] != null) {
throw new Exception("NullTextBoxException");
}
else if (spelers[i].Text == "") {
// skip when the textbox is empty
continue;
}
spelersSpel.Add(spelers[i].Text);
}
*/
foreach (TextBox item in spelers)
{
if (item != null) {
// throw exception when referring textbox is null
throw new Exception("NullTextBoxException");
}
else if (item.Text == "") {
// skip when the textbox is empty
continue;
}
spelersSpel.Add(item.Text);
}
MessageBox.Show(spelersSpel.Count.ToString());
game.ShowDialog();
}
}
You are confusing the TextBox object with its Text property.
If I understand correctly you want the List<string> to contain only the text for the textboxes that are not empty, so your code should be something like this
spelersSpel.Clear();
foreach (TextBox item in spelers)
{
if (item != null && !string.IsNullOrWhiteSpace(item.Text))
spelersSpel.Add(item.Text);
}
Notice that you declare the array to contain 7 TextBoxes (index from 0 to 6) but you add only 6 textboxes to it, so you need always this check for NULL because, when you loop over your elements, you have a NULL entry in your array at index 6. So, perhaps, if you have only 6 textboxes it is better to declare the array with
public TextBox[] spelers = new TextBox[6];
Next, in the click event, you execute two loops to fill the list, this of course will duplicate the list content.
However, I think that a better approach is to use directly a List<TextBox>, work with it and get rid of the array and the List<string>
public partial class Players : Form
{
public List<TextBox> spelers;
public Form1 game = new Form1();
public Players()
{
InitializeComponent();
spelers = new List<TextBox>()
{
inputSpeler1, inputSpeler2, inputSpeler3,
inputSpeler4, inputSpeler5, inputSpeler6
}
}
private void btnSpelers_Click(object sender, EventArgs e)
{
MessageBox.Show(spelers
.Count(x => !string.IsNullOrWhiteSpace(x.Text))
.ToString());
game.ShowDialog();
}
}
I have a dialog that is used to select which forms to show. Originally it was just being selected from a combo box, but now we need to select multiple, so we changed it to a list box.
Here is the method that we used for the combo box:
if (view.ShowDialog() == DialogResult.OK)
{
if (view.FormType == "Form1")
return new Form1_Controller();
else if (view.FormType == "Form2")
return new Form2_Controller();
else if (view.FormType == "Form3")
return new Form3_Controller();
else return null;
}
else
{
return null;
}
How can we encapsulate this in a loop that will return a controller for each selection?
For example, I have tried something like
foreach (ListBoxItem listItem in view.ListBox1)
{
//do if (view.FormType == "Form1")
}
But I don't know the right syntax to use.
To open all forms simultaneously, I would try a different approach something like this
ArrayList controllersSelected = new ArrayList();
foreach (var item in view.ListBox1.SelectedItems)
GetSelectedItem(item.Value, out controllersSelected);
//Your logic to display selected forms simultaneously
DisplaySimultaneousForms(controllersSelected);
private void GetSelectedItem(formName, out ArrayList list)
{
if (view.FormType == "Form1")
list.Add(new Form1_Controller());
else if (view.FormType == "Form2")
list.Add(new Form2_Controller());
else if (view.FormType == "Form3")
list.Add(new Form3_Controller());
}
use SelectedItems:
foreach (var item view.ListBox1.SelectedItems)
SelectForm(item.ToString());
void SelectForm(string value)
{
if(value == "Form1")
return new Form1_Controller();
...
}
You can use below code :
foreach (var item in view.ListBox1.SelectedItems)
{
ShowForm(item.Value);
}
private void ShowForm(formName)
{
if (view.FormType == "Form1")
return new Form1_Controller();
else if (view.FormType == "Form2")
return new Form2_Controller();
else if (view.FormType == "Form3")
return new Form3_Controller();
else return null;
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i <= listBox1.SelectedItems.Count - 1; i++)
{
switch (listBox1 .Items [i].ToString ())
{
case "FirstForm":
Form2 frm2 = new Form2();
frm2.Show();
break;
case "SecondForm":
Form3 frm3 = new Form3();
frm3.Show();
break;
default:
break;
}
}
}
when i try to update the content of the datagridview combo box it throws
Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function
at line ,node.Cells[(int)Parameters.eColumn.valueBySelectionColumn] = cboCell;
How can i solve this problem??
THX
private void treeGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
try
{
if (e.Control.GetType() == new DataGridViewComboBoxEditingControl().GetType())
{
if ((cboEditor != null) && (cboEditor_EventHandler != null))
{
cboEditor.SelectedIndexChanged -= cboEditor_EventHandler;
}
cboEditor = (DataGridViewComboBoxEditingControl)e.Control;
cboEditor.SelectedIndexChanged += cboEditor_EventHandler;
if (this.treeGridView1.SelectedCells.Count > 0 &&
this.treeGridView1.SelectedCells[0].ColumnIndex == (int)Parameters.eColumn.valueBySelectionColumn)
{
TreeGridNode node = GetCurrentNode();
object cellValue = node.Parent.Cells[(int)Parameters.eColumn.sectionTypeColumn].Value;
Parameters.eSection section = (Parameters.eSection)dicSection[cellValue.ToString()];
this.treeGridView1.Focus();
switch (section)
{
case Parameters.eSection.UNIX_Script:
DataGridViewComboBoxCell cboCell = Parameters.ValidateChoice(Parameters.eSection.UNIX_Script,
node.Cells[(int)Parameters.eColumn.parameterTypeColumn].Value,
ref cboEditor);
if (cboCell != null)
{
***node.Cells[(int)Parameters.eColumn.valueBySelectionColumn] = cboCell;***
node.Cells[(int)Parameters.eColumn.valueBySelectionColumn].Style.BackColor =
node.Cells[(int)Parameters.eColumn.sequenceColumn].Style.BackColor;
}
break;
}
}
}
}
}