Please suggest me any idea to pass sub string value of grid view in text box.
frm.txtcustcode.text = dg.cells["custcode"].value.ToString().Trim();
I need to pass (0000001234) "01234" to txtcustcode.Text;
Below code might work fine:
frm.txtcustcode.Text= dg.Cells["CustomerCode"].Value.ToString().Trim().Substring(5,5);
I hope I understand your question correctly.
The code :
dg.cells["custcode"].value.ToString().Trim();
is a value in a grid and the value you get by using the code is : 0000001234
You would like to display this value in a textbox on the form as a value : 01234
If this is the case you could do the following:
//If the value of custcode always starts with 0's but wont contain 0's in the number.
String sCustCode = "0000001234";
sCustCode = sCustCode.Replace("0", "");
sCustCode = "0" + sCustCode;
MessageBox.Show(sCustCode); //Displays 01234
//Or if your CustCode can contain a 0 and always starts with 0's.
sCustCode = "0000001234";
int num = Int32.Parse(sCustCode); //Depending on the number format use the correct cast.
sCustCode = "0" + num.ToString();
MessageBox.Show(sCustCode); //Displays 01234
//Or you dont want to use a Cast to get your code in the fear of dataloss for some reason.
sCustCode = "0000001234";
int index = 0;
int position = 0;
foreach(char myChar in sCustCode)
{
if (myChar != '0')
{
position = index;
break;
}
index++;
}
if (position == 0)
{
//No starting 0 found
}
else
{
sCustCode = sCustCode.Substring(position - 1, sCustCode.Length - position +1);
}
MessageBox.Show(sCustCode); //Displays 01234
I am sure there are better ways of doing this, it depends on the purpose.
Hope this can help you in the correct direction.
Related
I'm getting an error in this code:
void main() {
List<String> wave(String str) {
List<String> results = [];
String newStr;
int i = 0;
for (String ltr in str.split('')) {
newStr = str;
if (ltr != ' ') {
newStr[i] = ltr.toUpperCase();
results.add(newStr);
}
i++;
}
return results;
}
print(wave(' gap '));
}
the error is at the line:
newStr[i] = ltr.toUpperCase;
Despite when I try print(newStr[i]); I don't get an error and the code is executed correctly!
In Dart String operation, operator[] returns a string. Which means, array[index] is used for getting the string in the index position. That is why you're getting that error, because you can't set at specific index using this operator[] in dart. See the documentation for details.
To replace at the specific index in dart, you can use replaceFirst(Pattern from, String to, [int startIndex = 0]) as the other answer mentioned. Or, you can use substring(int start, [int? end]) as follows:
if (ltr != ' ' && i < newStr.length) {
newStr = newStr.substring(0, i) + ltr.toUpperCase() + newStr.substring(i+1);
results.add(newStr);
}
To make the code bug free, I've added the checking of the value of i in it. You should add the checking to avoid out of bound access.
try to replace
newStr[i] = ltr.toUpperCase();
to
newStr = newStr.replaceFirst(ltr,ltr.toUpperCase(),i);
So the result will be [ Gap , gAp , gaP ]
Honestly, I don't know how char is defined in Dart, but I think accessing index of String is kind of getter, thus cannot be set to a new value.
Been doing this code in "Include" file. But I am encountering the error "not all control paths return a value. What should I do?
double CalculateTakeProfit (double entryPrice, int takeProfitPips, double GetPipValue)
{
if (bIsBuyPosition == True)
{
double result = 0;
entryPrice = Ask;
result = (entryPrice + takeProfitPips * GetPipValue());
return result;
}
else if (bIsBuyPosition == False)
{
double result = 0;
entryPrice = Bid;
result = (entryPrice - takeProfitPips * GetPipValue());
return result;
}
}
Your if... else is wrong and you are also not using the variables passed to the function. You are instead referencing another function or overwriting them. Mixing variable types in a calculation can also lead to undesirable results (takeProfitPips should be of type double). You can also cut a few lines of your code down as follows
double CalculateTakeProfit(double entryPrice, double takeProfitPips, double GetPipValue)
{
if(bIsBuyPosition) return(entryPrice+takeProfitPips*GetPipValue);
else return(entryPrice-takeProfitPips*GetPipValue);
}
For my math question app I have two random numbers generated then I have 4 buttons as answers. I want to check the answer if the user pushes the right button but it seems not to work.
num1 and num2 are the labels which the random numbers are generated in so technically
num1.text = "(randomnum1)"
and num2.text = "(randomnum2)" Thanks.
I have the following code under button1 IBaction
var sum = (num1) + (num2)
if btn1.titleLabel = (sum){
check.text = "right"
}
Maybe you should do some convert before you add num1 and num2. (convert strings to integers before adding them up, also, you should convert string to integer fisrt when comparing sum and btn.titleLabel)
you have two options:
if btn1.titleLabel.toInt()! == sum
or
if btn1.titleLabel == String(sum)
consider the difference between = (assign) and == (is equal)
to assign a number to a label use
num1.text = "\(randomnum1)"
I recommend to use backing Int variables like randomnum1 to hold the random numbers which can used for the math for example
var randomnum1 = 0, randomnum2 = 0 // instance variables of the class
randomnum1 = randomFuntion()
randomnum2 = randomFuntion()
num1.text = String(randomnum1)
num2.text = String(randomnum2)
now the labels contain the string values of the random numbers, but you can do the math with the associated variables.
var sum = randomnum1 + randomnum2
after that you can check the result as mentioned above
if btn1.titleLabel == String(sum) {
check.text = "right"
}
I have the following code to work out if a number is prime;
#IBAction func isPrimeBtn(sender: AnyObject) {
if(isPrimeTxt.text.isEmpty){
isPrimeResultLbl.text = "Please Enter A Value";
}else{
var value = isPrimeTxt.text.toInt();
println(value);
if(value == 0){
println("value was 0");
isPrimeResultLbl.text = "Its Not";
}else{
for(var i = 3; i * i < value; i += 2){
println("value was 0");
if(value! % i == 0){
isPrimeResultLbl.text = "Its Not";
}
}
isPrimeResultLbl.text = "Its Prime!!";
}
}
}
When i print the value using println(value) I get Optional 22, if I check the variable is 0 it is true.
What am I doing incorrectly?
Thanks
There's a few problems here. Firstly, you correctly use value! in your modulus test, but in the first test and in the for loop you use value. I suggest you use an if let block instead, so value is not an optional ever:
if let value = isPrimeTxt.text.toInt() {
// now you know it's an integer
// ... do your test
} else {
// some error message
}
However, even if you do this, your logic is wrong on three counts:
1) isPrimeResultLbl.text = "Its Prime!!" is always executed after your for loop
2) it's incorrect for everything less than 10 because your for (var i = 3; i * i < value; i += 2) { will never execute if 3 * 3 >= value.
3) you need to start checking divisors at 2, not 3. 16 will appear prime using your algorithm (though 4 & 8 won't, as they're under 9!)
What happens if prime.text is "abc"? What happens is that the conversion toInt() will fail. Because it can fail, toInt() doesn't return an integer, it returns an optional integer. The result is either an integer or nothing. When you call println, you don't print an integer, you print an optional integer. And that's what println prints - it says "optional 22". It might have said something like "empty".
If you print value! instead of value, you tell Swift that you are 100% sure that the optional integer value is actually there. If it isn't, the expression value! will crash. If it is there, println will print 22. So the exclamation mark turns an optional either into a proper value, or into a crash.
What I'm trying to do is to make it where the user can type in a textbox and then click on a button and it will search the richtextbox for what they are looking for and if it found something it will change the label.
(Instances)
`
Button = btn_Search
Textbox = InputBox
RichTextBox = rtb
Label = Results`
Use this method, to find any Text inside your RichTextBox.
public int FindMyText(string searchText, int searchStart, int searchEnd)
{
// Initialize the return value to false by default.
int returnValue = -1;
// Ensure that a search string and a valid starting point are specified.
if (searchText.Length > 0 && searchStart >= 0)
{
// Ensure that a valid ending value is provided.
if (searchEnd > searchStart || searchEnd == -1)
{
// Obtain the location of the search string in richTextBox1.
int indexToText = richTextBox1.Find(searchText, searchStart, searchEnd, RichTextBoxFinds.MatchCase);
// Determine whether the text was found in richTextBox1.
if(indexToText >= 0)
{
// Return the index to the specified search text.
returnValue = indexToText;
}
}
}
return returnValue;
}
call this method like this:
var res= FindMyText("hello",0. richTextBox1.Text.Length);
now if res>-1, that means positive match, then you can set your labels i.e.
if(res>-1){
lbl1.Text = "hello found";
}
source here and here
Another method of searching text that is far more clean is as below,but first you need to add
System.Text.RegularExpressions namespace to your project;
private void SearchButton_Click(object sender, EventArgs e)
{
if (textBox1.TextLength >= 1)
{
string word = textBox1.Text;//The text you want to search.
Regex searchterm = new Regex(word);//A Regular Expression is most efficient way of working with text.
MatchCollection matches = searchterm.Matches(richTextBox1.Text);
if (matches.Count >= 1)
{
Results=matches.Count.ToString();//Your label to display match instances.
richTextBox1.SelectAll();
richTextBox1.SelectionBackColor = Color.White;
richTextBox1.DeselectAll();
foreach (Match match in matches)
{
richTextBox1.Select(match.Index, match.Length);
richTextBox1.SelectionBackColor = Color.Orange;
richTextBox1.DeselectAll();
}
}
}
}
This should do the job,furthermore if you want to specify additional search options,replace the line with Regex searchterm with anyone below,
Case Insensitive
Regex searchterm = new Regex(word,RegexOptions.IgnoreCase);
Whole Word Search
Regex searchterm = new Regex(#"\b"+word+"\b");
Case Insensitive and Whole Word Search
Regex searchterm = new Regex(#"\b"+word+"\b",RegexOptions.IgnoreCase);
and one more thing,Regex searches are case-sensitive by default.