Set character at the cursor location Editext - android-edittext

My layout has an editext field and some button to set text to it. I do not know why some buttons can set text at the current cursor position and others don't.
In details, number "0, 1, 2" can be inserted in to current cursor position but the plus char "+".
My main.xml
<RelativeLayout
android:id="#+id/displayLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="45"
android:padding="10dp">
<EditText
android:id="#+id/result"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="#ffff"
android:ellipsize="end"
android:inputType="number" />
</RelativeLayout>
To set text on the current cursor position I used:
int start =editText.getSelectionStart(); //get the the cursor position
String s = "Some string";
editText.getText().insert(start, s); //get the text and insert the String
And the Onclick method
#Override
public void onClick(View v) {
EditText result = (EditText) getActivity().findViewById(R.id.result);
int start = result.getSelectionStart();
switch (v.getId()) {
case R.id.digit0:
result.getText().insert(start, "0");
break;
case R.id.digit1:
result.getText().insert(start, "1");
break;
case R.id.digit2:
result.getText().insert(start, "2");
break;
case R.id.plus:
result.getText().insert(start, "+");
break;
}
}
I hope some one help me to figure out the problem. Thank you and I'm really appreciate for your help.

+ is not a valid character if you set your EditText to android:inputType="number". Are you trying to enter a phone number? You could try android:inputType="phone"

Related

Compose - TextField - SelectAllOnFocus

If we want our TextField to have an equivalent behaviour to EditText:selectAllOnFocus = "true" we can do something like create a TextFieldValue and set the selection from zero to lenght like TextRange(0, text.length)
This works and when user focus the TextField the whole text get selected, the problem is that when we create a TextFieldValue we need to set selection, the default value is Zero.
If the user wants to drag the cursor he just cant. Is there a way for now to create a selectAllOnFocus behaviour that allows the user to drag the cursor all over the text if he wants to in compose?
Yes I think one way is to follow the result selection coming from the TextField onValueChange callback:
var editableText by rememberSaveable { mutableStateOf("some text") }
val textRangeStart = rememberSaveable { mutableStateOf(0) }
val textRangeEnd = rememberSaveable { mutableStateOf(0) }
val textField = remember(editableText, textRangeEnd.value, textRangeStart.value) {
mutableStateOf(
TextFieldValue(
text = editableText ?: "",
selection = TextRange(textRangeStart.value, textRangeEnd.value)
)
)
}
TextField(
modifier = Modifier.fillMaxSize(),
value = textField.value,
placeholder = { Text("") },
onValueChange = {
textEditorViewModel.editableText.value = it.text
textRangeStart.value = it.selection.start
textRangeEnd.value = it.selection.end
}
)
}

MT4 Expert Advisor EA issue with updating price for offline chart

The following script uses a timer and works well in normal charts to comment current candle closing price every 1 second on chart. However, on offline chart, it only loads the closing price once and does not update it every second. Here is the code:
void OnTimer()
{
int m=TimeSeconds(TimeLocal());
double CloseValue = Close[0]; //Current Candle Close Value
string CloseValueString = DoubleToString(CloseValue,5); //Current price
Comment(
"Current value :",CloseValueString,"\n",
"Candle time :",m
);
}
Solved by adding the Refreshrates() function which updates data for the offline chart at the end of my function:
void OnTimer()
{
int m=TimeSeconds(TimeLocal());
double CloseValue = Close[0]; //Current Candle Close Value
string CloseValueString = DoubleToString(CloseValue,5); //Current price
Comment(
"Current value :",CloseValueString,"\n",
"Candle time :",m
);
RefreshRates();
}

Passing substring value of gridview to textbox

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.

Avoiding formatting of DOORS object text using DXL

I am writing DXL script in which few object text has borders(like one complete row of table is copied).
I have to emphasize the "shall" word in shall.
But using findPlainText() method, it changes the formatting of object text which have borders.
Initially the objects before scripts run is:
After running the script to make "shall" word Bold, i wrote DXL script:
void Change_Shall(Object o, string objText)
{
int off=0
int len=0
string StartUpperText = ""
string FontText = ""
string StartText = ""
string FindText = ""
bool IsChanged = false
string OriginalObjText = objText
string UpperFontObjText = upper(objText)
while (findPlainText(UpperFontObjText, "SHALL", off, len, true, false))
{
StartUpperText = UpperFontObjText[0:off-1]
UpperFontObjText = UpperFontObjText[off+len:]
FindText = OriginalObjText[off:off+len-1]
StartText = OriginalObjText[0:off-1]
OriginalObjText = OriginalObjText[off+len:]
if(FontText == "")
FontText = StartText "{\\b " FindText "}"
else
FontText = FontText StartText "{\\b " FindText "}"
//print FindText "\t\t" UpperFontObjText "\n"
IsChanged = true
off = 0
len = 0
}
if(IsChanged == true)
o."Object Text" = richText FontText OriginalObjText
}
The object text with border after this script runs get changes like
How can formatting of object text with borders be avoided and border is preserved in the object text.
I'm not sure how you're getting "Object Text" from Object o, but my guess is you're using o."Object Text" "" to cast it as a string. Is that right?
If so, then that is stripping all the rich text (including your borders) before you do anything to it. Try using string objText = richTextWithOle o."Object Text", or string objText = richText o."Object Text", and then try removing the unnecessary parameter to your function string objText, as a reference to this already comes along with Object o
I'm assuming based on how your table looks that it is a RichText table, in which case I believe your code will still work, it's just that I suspect you're starting with a string stripped of richText and adding richtext to it. Sometimes Word OLE objects with tables can look like that too, in which case you'd have to use COM to manipulate the OLE.
Hope this helps.

Find Function with RichTextBox with Labels

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.

Resources