How to make a ToolStripMenuItem hold a variable value to be recalled later? - c++-cli

I have a form that has a MenuStrip at the top. I only have one ToolStripMenuItem called Dates. I have written code that executes when an "Add" button is clicked which adds a new menu item underneath "Dates". This menu item is named by concatenating some variables which store user input. Here is everything I have for the "Add" button click event. It all works, but I want display the same info that gets displayed in the label when the click event happens to be saved to the submenu item too. So I want to be able to click that sub-menu item that is created and be able to see what input was saved to it when the Add button is clicked.
private: System::Void btnAdd_Click(System::Object^ sender, System::EventArgs^ e)
{
//add the input to the variables
String^ month = txtMonth->Text;
String^ day = txtDay->Text;
String^ year = txtYear->Text;
double tips = Double::Parse(txtTips->Text);
//make sure the date input is correct
if (txtMonth->Text == "" || txtDay->Text == "" || txtYear->Text == "" || Double::Parse(month) < 1 || Double::Parse(month) > 12
|| Double::Parse(day) < 1 || Double::Parse(day) > 31 /*|| Double::Parse(year)!=*/)
{
//show message box
MessageBox::Show("Please enter the date in the correct format.", "Error", MessageBoxButtons::OK,
MessageBoxIcon::Error);
//clear the boxes
txtMonth->Clear();
txtDay->Clear();
txtYear->Clear();
}
else
{
//for the menu bar
String^ date = month + "/" + day + "/" + year;
datesToolStripMenuItem->DropDownItems->Add(date)->Equals(date);
//print to label
lblPrint->Text = month + "/" + day + "/" + year + " - " + tips.ToString("c");
//clear the boxes
txtMonth->Clear();
txtDay->Clear();
txtYear->Clear();
txtTips->Clear();
}
}
FYI. I haven't written any code to do what I want to do. I just have no idea where to start for that, so I've shared what I have before that.

Related

New line "\n" does not work yet no errors are given

I have a program that loads a checked list box, then the user selects the items they want and selects a button to say they are done. The checked items are read in a contiguous string with a newline "\n" at the end of each string added. My problem is everything works ok except the newline "\n", and I don't know why.
Code
private: System::Void bntSelected_Click(System::Object^ sender, System::EventArgs^ e) {
int numSelected = this->checkedListBox1->CheckedItems->Count;
if (numSelected != 0)
{
// Set input focus to the list box.
//SetFocus(lstReceiver);
String^ listBoxStr = "";
// If so, loop through all checked items and print results.
for (int x = 0; x < numSelected; x++)
{
listBoxStr = listBoxStr + (x + 1).ToString() + " = " + this->checkedListBox1->CheckedItems[x]->ToString() + "\n";
}
lstReceiver->Items->Add(listBoxStr);
}
}
The string is being formed correctly, but the ListBox control doesn't show newlines in its list items - each item is pushed onto a single line. You can see this by debugging, or by adding a Label to the form with AutoSize set to false - the label will show the newline(s) in the string properly.
Related (C#): How to put a \n(new line) inside a list box?
Instead of ā€˜\nā€™, try ā€˜\r\nā€™. This may be a windows thing.

for loop stops after 2nd case in Selenium

I am working on a script that should just refresh trades every 15 minutes. It worked well, but then I found a problem. When it finds the trade that has to be refreshed.
else if (text[2] == "hours" || text[2] == "hour" || text[2] == "days" ||
text[2] == "day" || text[2] == "month" || text[2] == "months" || x >= 15)
It goes into the trade, edits it and saves as it should. But right after that it stops working. It says the program is still running, but nothing happens anymore. I have no idea why it would not work as it works perfectly for this case:
if (text[2] == "seconds")
continue;
I can't find a reason for loop to stop working to be honest. Would appreciate any ideas or tips.
Code:
private void CheckItems(IWebDriver driver)
{
IList<IWebElement>
trades = driver.FindElements(By.ClassName("rlg-trade-display-header")); // add all trades to list
for (var i = 0; i <= trades.Count; i++) // loop to check all trades
{
driver.Navigate().GoToUrl(tradeUrl);
if (i == 0)
Console.WriteLine("Active Trades: " + trades.Count);
var text = driver
.FindElement(By.XPath(
"/html/body/main/div/div/div/div[4]/div[" + (i + 1) + "]/div[1]/div/div/span"))
.Text.Split(); // reads text from trade and adjust it for the program
Console.WriteLine("Trade " + (i + 1) + " was last updated " + text[1] + " " + text[2] + " ago.");
var x = int.Parse(text[1]); // convert string to int to check time later
if (text[2] == "seconds")
continue;
if (text[2] == "hours" || text[2] == "hour" || text[2] == "days" || text[2] == "day" ||
text[2] == "month" || text[2] == "months" || x >= 15)
{
var element1 =
driver.FindElement(By.XPath("/html/body/main/div/div/div/div[4]/div[" + (i + 1) +
"]/div[1]/a/div"));
var actions1 = new Actions(driver);
actions1.MoveToElement(element1).Click().Perform(); // scrolls down to choosen element and clicks it
driver.FindElement(By.XPath("/html/body/main/div/div/div/div[2]/a[1]"))
.Click(); // click choosen element
var element = driver.FindElement(By.Name("btnSubmit"));
var actions = new Actions(driver);
actions.MoveToElement(element).Click().Perform(); // scrolls down to choosen element and clicks it
Thread.Sleep(2000);
Console.ReadLine();
}
}
}
private static void Main(string[] args)
{
var bot = new Program();
bot.setup(bot.driver);
while (true)
{
bot.CheckItems(bot.driver);
Thread.Sleep(15000); // wait 15 second before next loop
}
}
What I found out is that when I put a Console.ReadLine() in main after bot.CheckItems(bot.driver); it will now refresh all the trades and print everything for one whole for loop and then it stops and doesn't continue. And if I keep it like in code I given higher it works perfectly if all trades are in no need for refresh but if even one is it will refresh it and stop working. Any ideas what might be a reason for that problem? And why Console.ReadLine() matters?

Java: How edit button positions in JOptionPane.showInputDialog?

How do you edit the button positions in the JOptionPane.showInputDialog pop up?
Are there arguments that you can put into the parameters?
String inputValue = JOptionPane.showInputDialog(frame.getFrame(), "Please enter a value from 1 to 9");
if (inputValue == null) { // Cancel button
return 0;
}
Apparently I just had to add extra arguments to make it a plain message, but how do you reposition the buttons?
String inputValue = JOptionPane.showInputDialog(frame.getFrame(), "Please enter a value from 1 to 9","", JOptionPane.PLAIN_MESSAGE);

PL/SQL - Aggregate values & Write to table

I am just starting to dabble in PL/SQL so this question may be very straightforward. Here is the scenario:
I have several checkboxes which carry a weighted numeric value. For example:
Checkbox I --> Value '5'
Checkbox II --> Value '10'
Checkbox III --> Value '15'
etc.
The form would have 15 checkboxes in total and the end-user can select anywhere from 0 to all 15. As they select the checkboxes, the total weight would get calculated and a final numeric value would be displayed. For example. checking off 3 Checkbox I & 2 Checkbox III would = 45 points.
Now the total value of 45 would equal to a separate value. Example:
At 0 points, value = 'Okay'
1-15 points, value = 'Error'
16-30 points, value = 'Warning'
31+ points, value = 'Critical'
The form itself is built within Oracle APEX and I can do it using Dynamic Actions but using PL/SQL may be a better solution.
In summary, I'd like the hidden field to first calculate the total from the checked checkboxes and then use that total to figure out the value of either Okay, Error, Warning, or Critical.
Any assistance is much appreciated!
In my experience, it is better if we're going to use javascript on your case since we have to manipulate DOM and events of the checkboxes. If you want to display/change item values and get values at runtime, then javascript is better in doing that than PLSQL unless you want to submit your page every time you check/uncheck a box in your page which is not advisable.
Here is my solution for your question.
First, create a Display Only item on your page. This is where the values "Okay", "Error", "Warning", or "Critical" will appear. And its very important to set it's default value to 0. Then inside your page's "Function and Global Declaration" part, put the following functions:
function getcheck(checkbox_id,displayOnly_id){
var chkboxName = document.getElementById(checkbox_id + "_0").getAttribute("name");
var chks = document.getElementsByName(chkboxName);
var howmanychecks = chks.length;
var currentSum=0;
var v_remarks = "";
for(x=0;x<howmanychecks;x++){
chks[x].setAttribute("onchange","checkIfchecked(this,\'" + displayOnly_id + "\')");
if(chks[x].checked){
currentSum = currentSum + Number(chks[x].value);
}
}
if(currentSum==0){
v_remarks = "Okay";
}
else if(currentSum>0 && currentSum<=15){
v_remarks = "Error";
}
else if(currentSum>15 && currentSum<=30){
v_remarks = "Warning";
}
else{
v_remarks = "Critical";
}
document.getElementById(displayOnly_id).value = currentSum;
document.getElementById(displayOnly_id + "_DISPLAY").innerHTML = currentSum + ": " + v_remarks;
}
function checkIfchecked(p_element, displayOnly_id){
var v_difference;
var v_sum = Number($v(displayOnly_id));
var displayOnly_display = displayOnly_id + "_DISPLAY";
var v_remarks = "";
if(p_element.checked){
v_sum = v_sum + Number(p_element.value);
$("#" + displayOnly_id).val(v_sum);
}
else{
v_difference=Number($("#" + displayOnly_id).val())-Number(p_element.value);
if(v_difference<0){
v_difference=0;
}
$("#" + displayOnly_id).val(v_difference);
}
if($("#" + displayOnly_id).val()==0){
v_remarks = "Okay";
}
else if($("#" + displayOnly_id).val()>0 && $("#" + displayOnly_id).val()<=15){
v_remarks = "Error";
}
else if($("#" + displayOnly_id).val()>15 && $("#" + displayOnly_id).val()<=30){
v_remarks = "Warning";
}
else{
v_remarks = "Critical";
}
document.getElementById(displayOnly_display).innerHTML=$("#" + displayOnly_id).val() + ": " + v_remarks;
}
The above functions will get the sum of the values of those boxes that are checked. A value of a box will be taken out of the current sum if it is unchecked as well. It will also display the remarks for the current checked points whether if it is "Okay", "Error", "Warning", or "Critical".
In your "Execute when Page Loads" part of your page, add the following line:
getcheck(nameofyourcheckboxitem,nameofyourdisplayonlyitem);
where nameofyourcheckboxitem is the name of your Check Box and nameofyourdisplayonlyitem is the name of the Display Only item you have just created.
Here's a sample line on how to use the function that I've given you:
getcheck("P1_MYCHECKBOX","P1_MYDISPLAYONLY");

JOptionPane input problems

Ok, so I understand that if you want to test what the user clicks in a JOptionPane, you would do something like this:
final int option = JOptionPane.showConfirmDialog(null, "That file name does not exist.", "File Name", JOptionPane.OK_CANCEL_OPTION);
if(option == JOptionPane.CANCEL_OPTION)
{
g++;
}
However, what if I wanted to set a String = to a JOptionPane like this?
String fileName = JOptionPane.showInputDialog("File Name") + ".txt";
How am I supposed to compare what the user clicked then?
you can use as you mentioned
String fileName = JOptionPane.showInputDialog("File Name");
Default input dialog has OK button and cancel buttons. Therefore if you press cancel button
your string value will be null
Otherwise it is the value you entered on textfield
so you can have a if(fileName == null)
to check whether user clicked cancel or OK button
String fileName = JOptionPane.showInputDialog("Enter file name");
if (fileName == null) {
System.out.println("User pressed cancel");
//your logic
}else{
System.out.println(fileName);
//your logic
}
When you're using showInputDialog() you want to get user input from a textfield, not depend on what the user clicked.
You can use either a JOptionPane with an input field
String s = JOptionPane.showInputDialog(null, "Enter a message:);
Or you can use a combobox style JOptionPane
Object[] possibilities = {"ham", "spam", "yam"};
String s = (String)JOptionPane.showInputDialog(
null,
"Complete the sentence:\n"
+ "\"Green eggs and...\"",
"Customized Dialog",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
"ham");
Edit:
String s = JOptionPane.showInputDialog(null, "Enter a message:);
if (s != null){ // OK is pressed with a value in the field
// do something with s
}