Using Method to Read/Write value in Textbox in C# [duplicate] - c#

This question already has answers here:
Find control by name from Windows Forms controls
(3 answers)
Closed 3 years ago.
I have form1 and class1.
I would like to use a method to read/write the text in a textbox in form1 at class1.
This is the segment of the program. I currently able to interact between those two. The only problem is I do not how to convert the string to a form which can be read.
In form1:
public string readTxt(string txtbox_name)
{
string txtbox_val;
txtbox_val = (txtbox_name).Text; //problem here
return txtbox_val;
}
public void writeTxt(string txtbox_name,string txtbox_val)
{
(txtbox_name).Text = txtbox_val; //problem here
}
For example i need to read/write value in textbox1.
In class1:
string text1 = (readTxt(textbox1)); //for get the text in textbox1
string text2 = "Hello"
writeTxt(textbox1,text2); //for overwrite the text in textbox1 to "Hello"
Sorry for the question quite confusing due to not good in ask question.

For winforms, you could use Control.ControlCollection.Find(String, Boolean)
Searches for controls by their Name property and builds an array of
all the controls that match.
and Enumerable.OfType(IEnumerable)
Filters the elements of an IEnumerable based on a specified type.
public string ReadText(string txtbox_name)
{
// insert false if you expect not to be in a sub control
return Controls.Find(txtbox_name,true)
.OfType<TextBox>()
.FirstOrDefault()?.Text
}
However i fear your problem is actually, how can i get text from my TextBox.
var text = MyControl.Text;
Update
How about if i would like to write into Textbox.
var textBox = Controls.Find(txtbox_name,true)
.OfType<TextBox>()
.FirstOrDefault();
if(textBox != null)
textBox.Text = "yehaa";
Clarification
The difference between calling Find, and the other approaches, is that if your Textbox is buried in a Panel or other SubControls the other approaches wont find it. Other than that, they are exactly the same. Find works on the second parameter flag, and I'd an option to search subcontrols
searchAllChildren
true to search all child controls; otherwise, false.

You could Search for all TextBox with the name and get the Text using the Controls Property.
For Example,
public string ReadText(string txtbox_name)
{
return Controls.OfType<TextBox>().Single(x => x.Equals(txtbox_name)).Text;
}

Related

C# formatting TextBox but only displaying

I can't find how to do it.
I would like the number to be displayed three decimal places.
But to be remembered the whole number.
Example: full number: 8658,645851243511447358350.
Only display in TextBox: 8658,646
How should I do it?
The point is that in the program code read number from TextBox was whole: 8658,645851243511447358350.
I apologize if there is this solution somewhere. C# WinForms.
if this helps, you can use the "tag" property to store the full value, i.e.
TextBox.Text = "8658,646"
TextBox.Tag = "8658,645851243511447358350"
I got a workaround and that is to create your own class and that add additional property i.e., FullValue and keep track of both formatted and full value.
public class CustomTextBox:TextBox{
public string FullValue{get;set;}
}
or you can do
public class CustomTextBox:TextBox{
private string _FullValue;
public string FullValue{get=>_FullValue;set{base.Text=value;_FullValue=value;}}
}

Split spinner data when item selected

I am using spinner.Add("heading"+23) for adding item to spinner, and it shows heading23 in spinner list. How may I get data back in two variables (one for heading and one for int value).
If I understand it correctly. You want to split strings like "heading23" back into "heading" and "23".
If so, you can leverage Regular Expression. In C#, there is a class Regex for that you can use:
string str = "test123";
Regex reg = new Regex(#"\d+");//"\d" means digits "+" means match one or more
Match match=reg.Match(str);
if (match.Success)
{
string numberStr =match.Value;//value="123"
int number = int.Parse(numberStr);//number=123
var title = str.Replace(numberStr, "");//title="test"
}
When using java, spinner uses adapter. You can pass custom objects to that adapter. If I recall correctly, toString method is called for each item.
class MySpinnerDataItem {
String title;
int number;
}

On inserting to a database with predefined value C#

OK I have a text box with a set value, I only want this set value sent if the text box is not filled in on completion of form.
code
dontaion_euro.Text = "99.00";
problem is when I click send it only sends the data 99.00 I only want this sent if blank.
how could I achieve this?
Step1 : you need to Trim the Textbox value to remove the white spaces.
Step2 : you can compare the Textbox value with Empty String to check whether Textbox value is Empty or not
Try this:
if(donation_euro.Text.Trim().Equals(""))
{
donation_euro.Text="99.00";
}
If I understand correctly, you can use String.IsNullOrEmpty method like;
Indicates whether the specified string is null or an Empty string.
if(string.IsNullOrEmpty(dontaion_euro.Text))
{
dontaion_euro.Text = "99.00";
}
else
{
//Your textbox is not null or empty
}

C# get string name's name [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get variable name using reflection?
How to get the string name's name not the value of the string
string MyString = "";
Response.Write(MyString.GetType().Name);//Couldn't get the string name not the value of the string
The result should display back the string name "MyString"
I've found some suggested codes and rewrite it to make it shorter but still didn't like it much.
static string VariableName<T>(T item) where T : class
{
return typeof(T).GetProperties()[0].Name;
}
Response.Write(VariableName(new { MyString }));
I am looking another way to make it shorter like below but didn't how to use convert the current class so i can use them in the same line
Response.Write( typeof(new { MyString }).GetProperties()[0].Name);
While Marcelo's answer (and the linked to "duplicate" question) will explain how to do what you're after, a quick explanation why your code doesn't work.
GetType() does exactly what it says: it gets the Type of the object on which it is called. In your case, it will return System.String, which is the Type of the object referenced by your variable MyString. Thus your code will actually print the .Name of that Type, and not the name of the variable (which is what you actually want.)

textbox Insert and remove function in vb.net or c#

How to update the text from the TextBox control?
Consider a TextBox that already contains the string "Wel"
To insert text in the TextBox, I use:
TextBox1.Text = TextBox1.Text.Insert(3, "come")
And to remove characters from the TextBox:
TextBox1.Text = TextBox1.Text.Remove(3, 4)
But I need to be able to do this:
TextBox1.Text.Insert(3, "come");
TextBox1.Text.Remove(3, 4);
However, this code doesn't update the TextBox.
It this possible?
Can this be accomplished via the append method?
Text property of TextBox is of type string which is immutable it's not possible to change the existing string. Insert() or Remove() returns a new instance of string with the modification and you will have to assign this new instance back to TextBox's Text property.
There is TextBox.AppendText() that you might be interested in. It appends text to the end of the string but you cannot do anything like Insert() or Remove() with it though.
EDIT:
for your keypress, you could do something like this
char charToReplace = (char) (e.KeyChar + 1); // substitute replacement char
textBox1.SelectedText = new string(charToReplace,1);
e.Handled = true;
'string' is a immutable type, so each time string value changes new memory is allocated. So if you want to insert or remove text from TextBox, you have to assign it back to TextBox.Text property. However, if you just want to append text to the TextBox.Text, you can do
textBox.AppendText("Hello");

Categories