Get values from one textbox and put them in another textboxes - c#

I have values from a textbox :
"r, 00.00m,0000521135Hz,0000000000c,0000000.000s, 025.1C"
and I want to make each value show in another textboxes like this:
textbox 1:
a: "00.00"
textbox 2:
b: "0000521135"
textbox 3:
c: "0000000.000"
textbox 4:
d: "025.1"
I can do this in arduino using parseInt(),
I wonder how to do this in c#, any help?

you can use a string.split() function to extract value from first textbox.
string baseStr = "r, 00.00m,0000521135Hz,0000000000c,0000000.000s, 025.1C";
List<string> colStr= test.Split(new char[','], StringSplitOptions.RemoveEmptyEntries);
and then remove alphabet using regular expression
using System.Text.RegularExpressions;
...
Textbox1.Text = Regex.Replace(colStr[1], "[A-Za-z]", "");
Textbox2.Text = Regex.Replace(colStr[2], "[A-Za-z]", ""));
...

This will give you the idea how to put the data in the textboxes. I have done it for a string variable.
string s1 = "r, 00.00m,0000521135Hz,0000000000c,0000000.000s, 025.1C";
string[] spliteds1 = s1.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
string txt1 = "";
foreach(string elem in spliteds1)
{
if(Regex.Replace(elem, "[^0-9.]", "") != "")
{
txt1 = txt1 + Regex.Replace(elem, "[^0-9.]", "") + ",";
}
}
This code will put the in txt1 with comma seperator. You can run your loop for textboxes.
Hope this helps

Related

Multiline Hex to Decimal converter

I'm trying to make a hex to decimal converter for large amounts of hex numbers.
{
textBox1.Text = textBox1.Text.Replace("-", "");
textBox1.Text = textBox1.Text.Replace(" ", "");
if (textBox1.TextLength > 0)
{
textBox2.Text = Int32.Parse(textBox1.Text, System.Globalization.NumberStyles.HexNumber).ToString();
}
textBox3.Text = textBox2.Text.ToString().Replace(Environment.NewLine, ", ");
}
works fine on single line.
Error when trying to convert multiple lines
System.FormatException: Input string was not in a correct format.
If you're using a multiline textbox, try replacing the newline characters of its' value as well, like this:
textBox1.Text = textBox1.Text.Replace("\r\n", ""); // For Windows
textBox1.Text = textBox1.Text.Replace("\n", ""); // For Unix/Linux

how to find correct textbox from name?

I have 30 TextBox in my form. And i want to select correct one and write value there.
My textboxes names are :
tb_0_X, tb_0_Y,tb_1_X, tb_1_Y,tb_2_X, tb_2_Y, .... goes like this..
And i can create my textbox name :
string tbName = pointLoc.ToString();
string tbFirst = "tb_";
string tbLastX = "_X";
string tbLastY = "_Y";
string tbX = tbFirst + tbName + tbLastX;
string tbY = tbFirst + tbName + tbLastY;
Instead of writing all textbox such as :
tb_0_X.text = "";
tb_0_Y.text = "";
...
..
.
.
..
I want to write tbX or tbY but it is not possible to write ..
tbX.text = "someString";
how can i handle this issue ,?
EDIT :
To be more clearly ..
string tbName comes from radioButton. So i need to find which textbox i should change from tbX or tbY..
therefore i need to do something like tbX.text = "someString";
string tbX = "textBox1"; // or whatever you want to call it
TextBox tb = this.Controls.Find(tbX, false).FirstOrDefault() as TextBox;
if (tb != null)
{
tb.Text = "Test";
}
The this keyword obviously represents the form the textbox is on.

How to separate one string into 2 strings

I am using C#.NET and Windows CE Compact Framework. I have a code where in it should separate one string into two textboxes.
textbox1 = ID
textbox2 = quantity
string BarcodeValue= "+0000010901321 JN061704Z00";
textbox1.text = BarcodeValue.Remove(0, BarcodeValue.Length - BarcodeValue.IndexOf(' ') + 2);
//Output: JN061704Z00
textbox2.text = BarcodeValue.Remove(10, 0).TrimStart('+').TrimStart('0');
//Output: should be 1090 but I am getting a wrong output: 10901321 JN061704Z00
//Please take note that 1090 can be any number, can be 999990 or 90 or 1
Can somebody help me with this? :((
THANKS!!
Use Split method:
string BarcodeValue = "+0000010901321 JN061704Z00";
var splitted = BarcodeValue.Split(' '); //splits string by space
textbox1.text = splitted[1];
textbox2.text = splitted[0].Remove(10).TrimStart('+').TrimStart('0');
you probably should check if splitted length is 2 before accessing it to avoid IndexOutOfBound exception.
use Split()
string BarcodeValue= "+0000010901321 JN061704Z00";
string[] tmp = BarcodeValue.Split(' ');
textbox1.text = tmp[1];
textbox2.text= tmp[0].SubString(0,tmp[0].Length-4).TrimStart('+').TrimStart('0');
static void Main(string[] args)
{
string BarcodeValue = "+0000010901321 JN061704Z00";
var text1 = BarcodeValue.Split(' ')[1];
var text2 = BarcodeValue.Split(' ')[0].Remove(10).Trim('+');
Console.WriteLine(text1);
Console.WriteLine(Int32.Parse(text2));
}
Result:
JN061704Z00
1090
a slightly better version of the code posted above.
string BarcodeValue= "+0000010901321 JN061704Z00";
if(BarcodeValue.Contains(' '))
{
var splitted = BarcodeValue.Split(' ');
textbox1.text = splitted[1];
textbox2.text = splitted[0].TrimStart('+').TrimStart('0');
}
The Remove(10,0) removes zero characters. You want Remove(10) to remove everything after position 10.
See MSDN for the two versions.
Alternatively, use Substring(0,10) to get the first 10 characters.
This works only if the barcodeValue length is always const.
string[] s1 = BarcodeValue.Split(' ');
textBox1.Text = s1[0];
textBox2.Text = s1[1];
string _s = s1[0].Remove(0, 6).Remove(3, 4);
textBox3.Text = _s;
string BarcodeValue = "+0000010901321 JN061704Z00";
var splittedString = BarcodeValue.Split(' ');
TextBox1.Text = splittedString [0].Remove(10).TrimStart('+').TrimStart('0');
TextBox2.Text = splittedString [1];
Output-
TextBox1.Text = 1090
TextBox2.Text = JN061704Z00

Passing string value after apply split function in asp.net c#

I have fail to pass the string value to label or text box after split function. So my question is how to store the string value in label after applying the split function.
string strData2 = "samsung,apple,htc";
char[] separator2 = new char[] { ',' };
string[] strSplitArr = strData2.Split(separator2);
foreach (string arrStr in strSplitArr)
{
Response.Write(arrStr + "<br/>");
}
(e.g. label.text = ""+ the split string value )
Thanks
You can use String.Join:
label.Text = String.Join("," , strSplitArr);
Concatenates all the elements of a string array, using the specified
separator between each element.
Try:
label.text = label.text + arrStr;
to list one after other with break, do
label.text = label.text + arrStr + "</br>";
Do you want to join these in one string again? Use String.Join:
label.Text += String.Join(' ',strSplitArr);
source: http://msdn.microsoft.com/pl-pl/library/57a79xd0.aspx

How to capture exact td value in jquery independent of browser

I have li inside it i contain td.
var values = '';
$("#list4 li.add table .mytd").each(function () {
values = $(this).html() + '|' + values;
});
document.getElementById("MainContent_uscRetailParameters_hdRetailCustomerGroup").value = values;
__doPostBack('<%=txtRCCode.ClientID%>', '');
when I capture in hidden field it come like this
[alot of spaces]CHARMINSENSITIVE-1
with lot of spaces how can i retrieve the exact value in all browser. this space not found in Internet explorer. but in firefox it comes with spaces how could i capture the exact td value.
string lvValues = hdProductGroup.Value;
//string trim = lvValues.Replace(" ", "");
string trim = lvValues.Replace("\r", "");
trim = trim.Replace("\r", "");
trim = trim.Replace("\n", "");
trim = trim.Replace("\t", "");
string str = trim;
string[] list = str.Split('|');
You could trim it in Jquery? Try something like the following:
values = $.trim($(this).html() + '|' + values);
Or at the server using something like:
value = value.Trim();

Categories

Resources