Here is code that i used to populate a look up edit:
Letsap.DAL.PersonDataSet dsPerson = new Letsap.DAL.PersonDataSet();
lookUpEdit3.Properties.DataSource = dsPerson.GetPersonsID();
lookUpEdit3.Properties.ValueMember = "Person_ID";
lookUpEdit3.Properties.DisplayMember = "Person_Name";
lookUpEdit3.EditValue = 0;
But I would like to check if the user selected a value, if they did, it must move on to the next screen.
How do I do the check?
You can do it like this. Assuming you have a button with a click event attached, on which you want to check wheter you want to continue:
private void ButtonClick(object sender, EventArgs e)
{
if (lookUpEdit3.ItemIndex >= 0)
{
// show next screen
}
}
Related
Currently I have it so that when you select the text box it will highlight the text in it but what I want it to do is only do this for the first time that it is selected so that it will not delete the text that the user is typing each time. Here is what I am using to highlight the text:
private void txtName_Focus(object sender, EventArgs e)
{
bool isFirstTime = true;
if (isFirstTime == true){
txtName.SelectionStart = 0;
txtName.SelectionLength = txtName.Text.Length;
}
isFirstTime = false;
}
bool isFirstTime = true; this is your problem. It is being initialized to true every time the focus event is being called. Move bool isFirstTime; to be a member of your class and initialize it to true once in the declaration, constructor or the form load event
Maybe something like this:
bool txtNameWasFocused=false;
private void txtName_Focus(object sender, EventArgs e)
{
if(!txtNameWasFocused){
txtNameWasFocused=true;
txtName.SelectionStart = 0;
txtName.SelectionLength = txtName.Text.Length;
}
}
If you need this in many places, you might think of a derived text box with this special behaviour...
I want to make an activity with few edittext fields and a button which should be disabled until the most important of these fields has been filled. This is the code I am using but the button is staying disabled the whole time:
doneButton implementation
if((isEmpty(inputType)) || (isEmpty(inputAmount)) || (isEmpty(inputSupplier)))
doneButton.Enabled = false;
else
doneButton.Enabled = true;
This is the code for the isEmpty() method, which is checking if the edittext is empty or not:
private Boolean isEmpty(EditText etText) {
return etText.Text.ToString().Length == 0;
}
Thanks in advance ! :)
Why not use the TextChanged event:
EditText input = FindViewById<EditText>(Resource.Id.editText1);
input.TextChanged += input_TextChanged;
and then define the event handler for it?
private void input_TextChanged(object sender, TextChangedEventArgs e)
{
Console.WriteLine("input text changed");
// if text bigger than 0, enable the button, otherwise disable it
}
Much cleaner IMHO.
You will need to add this implementation(your code to check if the button stays disabled) inside the TextWatcher#afterTextChanged method , which you would add as a TextChanged listener. You will need to do this for all the edit texts you think are important.
Something like this:
inputType.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
if((isEmpty(inputType)) || (isEmpty(inputAmount)) || (isEmpty(inputSupplier)))
doneButton.setEnabled(false);
else
doneButton.setEnabled(true);
}
});
Similar listeners over inputAmount and inputSupplier should do the task.
The question is: On which event did you attach this code?
You can, for example, create a TextWatcher object and attach it to the relevant text fields. Something like:
inputType.addTextChangedListener(watcher);
In this watcher you would do the checks you have written and do:
doneButton.setEnabled(true/false);
I solved it like this:
inputType.AfterTextChanged += new EventHandler<AfterTextChangedEventArgs> (OnTextChange);
inputAmount.AfterTextChanged += new EventHandler<AfterTextChangedEventArgs> (OnTextChange);
inputSupplier.AfterTextChanged += new EventHandler<AfterTextChangedEventArgs> (OnTextChange);
where the OnTextChange() method is this:
public void OnTextChange(object sender, EventArgs e)
{
if((isEmpty(inputType)) || (isEmpty(inputAmount)) || (isEmpty(inputSupplier)))
doneButton.Enabled = false;
else
doneButton.Enabled = true;
}
I'm trying to persist CSS user preference to my database according to which of six buttons is selected by the user.
On order to do this, I am trying to assign an integer value to each button click event; whichever is clicked will pass the corresponding integer as a parameter to my data access object to update the database able.
My method reads such:
protected void SetCSS(object sender, EventArgs e)
{
Users setCss = new Users();
if (IsPostBack)
{
if (sender.ToString() == "Blue")
{
setCss.StylePreference = 0;
}
else if (sender.ToString() == "Khaki")
{
setCss.StylePreference = 1;
}
else if (sender.ToString() == "Night")
{
setCss.StylePreference = 2;
}
else if (sender.ToString() == "Pink")
{
setCss.StylePreference = 3;
}
else if (sender.ToString() == "White")
{
setCss.StylePreference = 4;
}
else if (sender.ToString() == "Yellow")
{
setCss.StylePreference = 5;
}
setCss.UserLoginName = Session["eMail"].ToString(); // current user
setCss.SetStylePreference(setCss.UserLoginName, setCss.StylePreference);
}
In each button's click event:
protected void btnBlue_Click(object sender, EventArgs e)
{
SetCSS(btnBlue, null);
}
protected void btnKhaki_Click(object sender, EventArgs e)
{
SetCSS(btnKhaki, null);
}
etc...
I put a watch on the sender object and, when the Pink button is selected, the value assigned to the sender reads
{Text="Pink"}
However, as I step through the if statement in the SetCSS method, when I come to the
else if (sender.ToString() == "Pink")
the condition is not met and, rather than setting the style preference to 3 as it should, the program passes on to the end of the statement, finishing by always assigning a value of 0 to the property.
What am I doing wrong?
Would really appreciate help...
You need to use sender.Id.ToString()
Calling sender.ToString() on an ASP.NET button will return "System.Web.Ui.Button" or something similar.
Paste the code related to how you setup the button and I'll clarify my answer more, as you could need either Id or Text depending on how you're setting the the name on your button.
Realistically, you can refactor this code to be a lot simpler.
You should map the Click event on all of your buttons to SetCSS(). Having a lot of scattered methods to only wrap the call is useless.
Change the if / else block to check for sender.Text
if (sender.Text.ToString() == "Blue")
{
setCss.StylePreference = 0;
}
and do the same for the rest of the statements.
I like to have a context menu only show up if an item is actually selected in a listbox in a winforms c# application.
Currently, I am able to select an item if it is right clicked properly, and I can disable the right click menu if nothing is selected, however, I don't want the menu to even show up.
how can this be accomplished?
private void genPassMenu_Opening(object sender, CancelEventArgs e)
{
genPassMenu.Enabled = lstPasswords.SelectedIndex > 0;
genPassMenu.Visible = lstPasswords.SelectedIndex > 0;
}
I tried both of those situations on their own, and it only works for enabled.
Perhaps Opening isn't the correct event to choose?
Tx
Try this:
private void genPassMenu_Opening(object sender, CancelEventArgs e)
{
//if (lstPasswords.SelectedIndex == -1) e.Cancel = true;
e.Cancel = (lstPasswords.SelectedIndex == -1);
}
Easy,
private void genPassMenu_Opening(object sender, CancelEventArgs e)
{
e.Cancel = (lstPasswords.SelectedIndex == 0);
}
I typically set the properties of each context menu item according to its appropriateness for the particular GUI element that is selected. Perhaps by setting the visible attribute on each menu item, rather than the whole menu, you can get the results that you want.
private void genPassMenu_Opening(object sender, CancelEventArgs e)
{
//genPassMenu.Enabled = lstPasswords.SelectedIndex > 0;
//genPassMenu.Visible = lstPasswords.SelectedIndex > 0;
e.Cancel = (lstPasswords.SelectedIndex <= 0);
}
I saw when the above did hte opposite I reversed the code slightly. For some reason having the equality also didn't work.
I have a combobox at the top of a form that loads editable data into fields below. If the user has made changes, but not saved, and tries to select a different option from the combobox, I want to warn them and give them a chance to cancel or save.
I am in need of a "BeforeValueChange" event with a cancelable event argument.
Any advice on how to accomplish?
Save the ComboBox's SelectedIndex when to box if first entered, and then restore it's value when you need to cancel the change.
cbx_Example.Enter += cbx_Example_Enter;
cbx_Example.SelectionChangeCommitted += cbx_Example_SelectionChangeCommitted;
...
private int prevExampleIndex = 0;
private void cbx_Example_Enter(object sender, EventArgs e)
{
prevExampleIndex = cbx_Example.SelectedIndex;
}
private void cbx_Example_SelectionChangeCommitted(object sender, EventArgs e)
{
// some custom flag to determine Edit mode
if (mode == FormModes.EDIT)
{
cbx_Example.SelectedIndex = prevExampleIndex;
}
}
Here is the simplest fix:-
bool isSelectionHandled = true;
void CmbBx_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (isSelectionHandled)
{
MessageBoxResult result = MessageBox.Show("Do you wish to continue selection change?", this.Title, MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.No)
{
ComboBox combo = (ComboBox)sender;
isSelectionHandled = false;
if (e.RemovedItems.Count > 0)
combo.SelectedItem = e.RemovedItems[0];
return;
}
}
isSelectionHandled = true;
}
Save the current value on the Enter event.
Implement the BeforeValueChange logic in the ValueChanged event, before the actual ValueChanged logic. If the user cancels, set the stored value and don't continue in the method (return).
If you're going to use this system a lot, I'd suggest inheriting ComboBox and implementing your BeforeValuechange event there.
The Validating event can be used for this scenario
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx
You don't get an appropriate event by default. You could cache the previous value and set it back to that if the user wants to cancel.
How about using the Validating / Validated events?
It works well, if the event happening on LostFocus instead of Change is ok with you.
Otherwise, how about
public void Combobox_ValueChanged(object sender, EventArgs e) {
if (!AskUserIfHeIsSureHeWantsToChangeTheValue())
{
// Set previous value
return;
}
// perform rest of onChange code
}
You could use a message filter to intercept clicks and key presses, which would allow you to prevent the combo box's normal behaviour. But I think you'd be better off disabling the combo box when the user makes a change, and require them to either save or revert their changes.
You can't really prevent it, but you can change it back to the old value if certain requirements aren't met:
private SomeObject = selectedSomeObject=null;
private void cbxTemplates_SelectionChangeCommitted(object sender, EventArgs e)
{
if (!(sender is ComboBox cb)) return;
if (!(cb.SelectedItem is SomeObject tem)) return;
if (MessageBox.Show("You sure?", "??.",
MessageBoxButtons.OKCancel) != DialogResult.OK)
cb.SelectedItem = selectedSomeObject;
else
{
selectedSomeObject = tem;
}
}