c# drop down list selected count - c#-2.0

yesterday i asked one question and got the answer from our friend here, and i ran successfully, also it have one problem with it. "Yester day My question is, when we selecting the drop down list then it should be shown by a label as "1" at very first time, again it'll be increas by selection", this is what the answer i got..,
static int count = 0;
private void bind()
{
ArrayList ar = new ArrayList();
ar.Add("first");
ar.Add("Second");
ar.Add("Third");
ar.Add("Four");
ar.Add("Five");
ar.Add("Six");
ar.Add("Seven");
CCddl.DataSource = ar;
CCddl.DataBind();
}
protected void CCddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (count == 0) count = 1;
Label12.Text = count++.ToString();
}
this code worked, but once the running window getting closed, then it lose the continuation, i mean again application getting run it'll shown again "1". But exactly what i want is, the number continuation should be end when the system day has change.

Try using the Application Settings feature. I added two user settings CountDate and Count in the Project->Property's->Settings and changed your SelectedIndexChangedEvent to
protected void CCddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (count == 0) count = 1;
Label12.Text = count++.ToString();
Properties.Settings.Default.CountDate = DateTime.Now.Date;
Properties.Settings.Default.Count = count;
Properties.Settings.Default.Save();
}
And right before you call your Bind Method during your form initialization put something like this.
if(Properties.Settings.Default.CountDate.Date != DateTime.Now.Date)
{
Properties.Settings.Default.Count = 0;
Properties.Settings.Default.CountDate = DateTime.Now.Date;
Properties.Settings.Default.Save();
}
else
count = Properties.Settings.Default.Count;
bind();
Added the Property Settings Image

You should somehow store the value in a database or something. With the date value. Then when the datechanges you just reset the value.

Related

Vaadin get/set cursor position / selection start/end in Textfield

I know there was in Vaadin7 (8) some methods the get / set the cursor position of the textfield from server side.
I'm using Vaadin Flow v21, but there are no such methods. How can I get/set the cursor position from server side synchronously? To set it seens to work fine using some javascript, but I cannot read the actual cursor position, since it is only going async. How to do it sync?
I try to read like this:
public int getSelectionStart() throws InterruptedException
{
Future<Integer> value = UI.getCurrent().getPage().executeJs(
"return $0.shadowRoot.querySelector('[part=\"value\"]').selectionStart;"
, getElement()).toCompletableFuture(Integer.class);
final int[] val = new int[1];
Thread t = new Thread(() ->
{
Integer result = null;
try
{
result = value.get();
}
catch (Exception e)
{
e.printStackTrace();
}
val[0] = result == null ? 0 : result;
});
t.start();
t.join();
return val[0];
};
But above method gives me exception, that the ongoing UI session request has not been closed yet, so basically it cannot execute the javascript, unless I end the request.
This is how I set cursor position, which seems to work since I don't want to get any return value back, so ending the request is going to execute the script and set the proper cursor position.
public void setSelectionStart(int pos)
{
UI.getCurrent().getPage().executeJs("$0.shadowRoot.querySelector('[part=\"value\"]').setSelectionRange($1, $1);", getElement(), pos);
}
Thanks for any help / suggestion!
I hope the documentation for RPC return values could help here: https://vaadin.com/docs/latest/flow/element-api/client-server-rpc/#return-values

C# For each line do this:

I have a background task doing a task, so my GUI doesn't freeze up.
The code is:
private void backgroundWorker1_DoWork_1(object sender, DoWorkEventArgs e)
{
for (int index = 0; index < this.combos.Length; ++index)
{
string[] strArray = Strings.Split(this.combos[index], ":", -1, CompareMethod.Binary);
string username = strArray[0];
string pass = strArray[1];
//do stuff
and it does a task. The problem is, it only does the task for 1 line of text that is loaded, then it stops. I need to to continue for every line of code.
For instance, I user loads up their order numbers and wants to retrieve the status of every single on. The task is working fine when they press start, but only retrieves status for 1 code, not all of them.
This is how I call it:
private void button4_Click_1(object sender, EventArgs e)
{
thread.RunWorkerAsync();
}

Stackoverflow exception in blackberry CheckBoxField

I am implementing a simple app, where in the registration page user can select news categories. Requirements are below
All the categories are the CheckBoxField's. User have to select at least one category.
Select all CheckBox will allow to select all/deselect all categories CheckBox.
If user manually selects all checkbox fields then "Select All" checkbox must be selected.
Approaches: I have created the categories checkbox in a loop.
for(int i=0;i<interests.length;i++){
allFields[i] = new ColorCheckBoxField(interests[i], false, checkBoxStyle | USE_ALL_WIDTH);
allFields[i].setCookie(i+"");
allFields[i].setFont(bodyFont);
allFields[i].setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ColorCheckBoxField tempChoice = (ColorCheckBoxField)field;
int index =Integer.parseInt(tempChoice.getCookie().toString().trim());
//set the selection
if(tempChoice.getChecked()){
parent.selectInterest(index);
}
boolean flag = true;
int[] intrests = parent.getSelectedInterest();
for (int i = 0; i < intrests.length; i++) {
if(intrests[i]==0){
flag = false;
}
}
if(flag==true){
selectAll.setChecked(flag); // select all is Checkbox object
}else{
selectAll.setChecked(false);
}
}
});
vfm.add(allFields[i]);
}
My selectAll checkbox logic is
selectAll = new ColorCheckBoxField("Select All", false, checkBoxStyle | USE_ALL_WIDTH);
selectAll.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ColorCheckBoxField temp = (ColorCheckBoxField) field;
//if (context == FieldChangeListener.PROGRAMMATIC ) {
checkAll(temp.getChecked()); // it loops through all checkbox and set them checked
//}
}
});
innerHfm.add(selectAll);
I understand the problem, its due to infinite loop. I have used "FieldChangeListener.PROGRAMMATIC" but that wont help because i want the field listener to work for both pragmatically and manually. I don't have any option left to fix. Any hack will help me?
That's correct that you have to use FieldChangeListener.PROGRAMMATIC. But you have to use it with interest checkboxes instead of using it for selectAll checkbox.
Please add one defensive check to FieldChangeListener for interest checkboxes:
if ( nonProgrammaticChange(context) ) {
ColorCheckBoxField tempChoice = (ColorCheckBoxField)field;
int index = Integer.parseInt(tempChoice.getCookie().toString().trim());
...
}
Where nonProgrammaticChange is:
private boolean nonProgrammaticChange (int context) {
return (context & FieldChangeListener.PROGRAMMATIC) != FieldChangeListener.PROGRAMMATIC;
}
I see bug in your code - you don't clear interest in parent if checkbox is unchecked.
Minor improvements as for me - use Vector where you'll store indexes of selected checkboxes. This will allow to replace this code:
boolean flag = true;
int[] intrests = parent.getSelectedInterest();
for ( int i = 0; i < intrests.length; i++ ) {
if( intrests[i] == 0 ) {
flag = false;
}
}
To this code:
selectedInterestIndexes.size() == interests.length
And probably this will give you less iteration in other places.
As well I would work more on removal of duplicates and code readability.

Distinguish events from different button fields

I have a for loop that creates ButtonFields with identical text values. I want to get a distinct event from each of those buttons, which tells me which index of the for loop created the button. I don't want to create an anonymous class for each ButtonField.
If they are going one by one (that I'm assuming from your post) you could remember index of the first one in use next code in your fieldChanged method:
if (field instanceof ButtonField) {
int buttonIndex = field.getManager().getFieldIndex(field) - zeroButtonInex;
}
Don't forget to assign FieldChangeListener to each of these buttons.
Or sure you could make your new class from ButtonField (could by anonymous) where you could save index and have getter for it.
You have add the buttons to an array. I will give you an idea to try this:
private ButtonField buttonsObj[];
In your code before the for loop you know the number of buttons, so you can initialize the array length.
int size = 10;
buttonsObj = new ButtonFields[size];
for(int i = 0; i < size; i++)
{
buttonsObj[i] = new ButtonFields["btn"];
buttonsObj[i].setChangeListener(this);
add(buttonsObj[i]);
}
public void fieldChanged(Field field, int context) {
for(int i=0;i<size;i++) {
if(field == buttonsObj[i]) {
// you can trigger your event
}
}
}

how to select all items at a time when cliking on select all menuitem for a listfield checkbox in blackberry

I want to select all items in a listfield at a time when cliking on the select all menu item in the application.i tried with my code like this..
selectall = new MenuItem("Selectall", 200, 10){
public void run(){
int elementLength = _elements.length;
for(int count = 0; count < elementLength; ++count)
{
_listData.addElement(new ChecklistData(_elements[count], true));
listField.insert(count);
}
}
};
but iam getting the result with old list also.below the old list this new list is coming with checked.how to solve that.please give your ideas.and where iam doing the mistake.thanks in advance..
Your use of listField.insert means that you're adding new ChecklistData objects to the list. That's why you're getting a completely new list underneath your previous one. Instead of adding to _listData, go through it and set the ChecklistData to be checked.
It looks like that is a custom class, so I don't know what it will take for you to do that. If you used CheckboxFields in _listData, you could do it like this:
for (Enumeration e = _listdata.elements() ; e.hasMoreElements() ;) {
CheckboxField c = (CheckboxField)e.nextElement();
c.setChecked(true);
}
Thanks for your all suggestions. i solved my problem now..the solution is..
selectall = new MenuItem("Selectall", 200, 10){
public void run(){
int elementLength = _elements.length;
for(int count = 0; count < elementLength; ++count)
{
_listData.setElementAt(new ChecklistData(_elements[count], true), count);
}
}
};

Resources