How to copy selectedtext from activecontrol textbox - c#

i want to ask that how can i copy the selected text of active control textbox.
i tried this but it gives me the whole text , not the selected text :
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
string str = ActiveControl.Text;
}
and i didn't want to copy a text from a specific textbox , such as :
string str = textbox1.SelectedText;
i want to copy the Selected Text of any active control textbox with context Menu Strip
thank you.

TextBox activeTxtBox = ActiveControl as TextBox;
if(activeTxtBox!= null)
{
string str = activeTxtBox.SelectedText;
}

Related

C# how to filter items on ListView using textBox

I want to filter the items that I added to my Listview, using my textbox_TextChanged Can you please show me the Codes, I am using Visual Studio 2013. tia
e.g.
First I am adding a Destination/Regularfare/Discountedfare/Baggagefare to my ListView. and my problem is Searching I want to search the Destinations using a TextBox.
There are no Codes inside my TextBox Search. that one I need.
And here's my Codes for adding an item to my ListView.
public void add(String destination, String Regulare, String Discounted, String Baggage) {
String [] rows = { destination, Regulare, Discounted, Baggage};
ListViewItem item = new ListViewItem(rows);
listView1.Items.Add(item);
}
private void btnAdd_Click(object sender, EventArgs e) {
add(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
textBox1.Text = "";
textBox2.Text = "0";
textBox3.Text = "0";
textBox4.Text = "0";
MessageBox.Show("Record Added!","Saved",MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
tbDestination.Focus();
}
Like the other posters were saying, we don't know what your tech stack is or what problem you're trying to solve. Have you tried binding your list to a data source? Let's say you have a text box and a submit button for filtering. The user enters "California" into the text box and presses submit. You'll have a click handler in the code behind that invokes a DataBind() on your list. The method for your data bind will get the value of the textbox/hidden field and use it to return a conditional data structure that is then used to populate your list. You could even have a clear button that resets the filtering using the same method.
But it's been 16 days now, so you've probably already solved the problem. What did you end up doing?
Hı , Try this two way
First :
private void searchBox_TextChanged(object sender, EventArgs e)
{
// Call FindItemWithText with the contents of the textbox.
ListViewItem foundItem =
textListView.FindItemWithText(searchBox.Text, false, 0, true);
if (foundItem != null)
{
textListView.TopItem = foundItem;
}
}
Second :
void yourListView_MouseDown(object sender, MouseEventArgs e)
{
// Find the an item above where the user clicked.
ListViewItem foundItem =
iconListView.FindNearestItem(SearchDirectionHint.Up, e.X, e.Y);
if (foundItem != null)
previousItemBox.Text = foundItem.Text;
else
previousItemBox.Text = "No item found";
}

How to add textbox text to label and handling event handler

I have 4 text boxes and 3 different labels in different panels. my job is when i click any label that 4 text box texts should be assigned to respective label.
my code is as folows
string srlnumber;
string micr;
string accnumber;
string tc;
string compltedata;
private void lblMicr1_Click(object sender, EventArgs e)
{
srlnumber = txtSerialnumber.Text;
micr = txtMicr.Text;
accnumber = txtAccounno.Text;
tc = txtTC.Text;
compltedata = "C" + srlnumber +"C"+ micr +"A"+ accnumber +""+ tc;
lblMicr1.Text = compltedata;
}
my requirement is when i click label and then if i enter values to the text boxes it has to be assigned to the label. but it is not happening.
can anyone please help me
I assume that lblMicr1 is the label whose data you want to assign to
textbox1.
protected void lblMicr1_Click(object sender, EventArgs e)
{
lblMicr1.Text = compltedata
TextBox1.Text = lblMicr1.Text;
}
Or
this.MyTextBox.Text = this.MyLabel.Text;
use it , remember to declare label first and then textbox , so that
textbox can use label data.

Auto Complete Not Working. in VS C#

I'm working on my dataGridView and I'm trying to make one of my cell to be auto Complete, but its not working.
C# code:
private void dataGridRequest_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox prodCode = e.Control as TextBox;
if (prodCode != null)
{
prodCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
prodCode.AutoCompleteCustomSource = itemList;
prodCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
Check 2 things:
Check if itemList is empty
Disable the multiline option in the textbox (here MSDN mentions it doesn't work on multiline TextBox Controls)

How call the Text_Changed Event for multiple text box controls on a form in C# winforms

I have form that has about 20 TextBox controls and I would like to fire the Text_Changed event with out adding the event for each individual text box. Is there a way to loop through the text boxes to fire this event? What I am trying to do is clear a label control when the text changes. Instead of displaying a message box, for error descriptions, I use a label control to display the message. I also set it up where if a text box has invalid data, I select all text and give focus to that TextBox so when user re-enters information the label control clears the message.
Edit:
To clear up some confusion, here is some of my code from my validation method
if (txtBentLeadsQty.Text == "")
{
//ValidData = true;
BentLeadsCount = 0;
}
else
{
if (int.TryParse(txtBentLeadsQty.Text.Trim(), out BentLeadsCount))
ValidData = true;
else
{
ValidData = false;
lblError.Text = "Bent Lead Qty must be a numeric value";
txtBentLeadsQty.SelectAll();
txtBentLeadsQty.Focus();
}
}
I already have a way to check for numeric values, and I put code in to select all text entered and gave focus if the values are not numeric, I just want to have a way to clear the Label control when the the text is changes like if the user hit backspace or starts typing that why if the error occurs, I highlight all the text in that TextBox if it is not valid. I can do this if I put code in every text boxes TextChanged event, but to save coding I was wondering if there is way to clear the label control if any of the text boxes throws an error from my validation method instead of adding individual events for 20 text boxes.
Note: Not all text boxes will have data entered, these are quantity text boxes I put code in to assign a 0 to the variable if the TextBox in null.
You can use the following code:
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if ((ctrl as TextBox) != null)
{
(ctrl as TextBox).TextChanged += Form1_TextChanged;
}
}
}
private void Form1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show((sender as TextBox).Name);
}
I'm assuming you want to add the same handler to all textboxes in the form dynamically, i.e. without having to add them for each text box in the visual editor (or code).
If so, this might be what you need:
// Iterate over all controls in the current form:
foreach (var ctl in Controls)
{
// Check if the current control is a textbox
// (will be null if it is of another type)
var txtBox = ctl as TextBox;
if (txtBox != null)
{
txtBox.TextChanged += YourMethod();
}
}
Sounds like you want to programmatically fire the method on each text box.
First, create an array of around 20 text boxes.
var textBoxes = new []{textBox0, textBox1, textBox2};
Loop through the each box and call the text changed method
foreach(var textBox in textBoxes)
{
TextChangedMethod(textBox);
}
If the method you are calling was generated by Visual Studio, it will take a second parameter for EventArgs. You can simply pass a null value for that.
TextChangedMethod(textBox, null);
Create a method, something like this:
public void TextChanged(object sender, EventArgs e)
{
//Text-changed code here.
}
At this point you can click on each text box on your form and add your new method to the event you want to occur. Click a text box on your form, and click the thunderbolt icon in the properties menu and scroll down to "TextChanged" event. Change that drop down on that event to your new method.
OR
Add the event at run time after you initialize the forms components:
textBox1.TextChanged += new EventHandler(TextChanged);
textBox2.TextChanged += new EventHandler(TextChanged);
textBox3.TextChanged += new EventHandler(TextChanged);
This could be done easier if you add all the text boxes to an array, and loop through them with a foreach to add the event to each one. You could also just grab all the text boxes from the form and loop the same way, though I don't know if you have other controls/text boxes that would make you avoid this method.
Use a foreach statement.
Example
List<TextBox> TextblockCollection = null;//You have to add them all individually to the list
foreach (var text in TextblockCollection)
{
//Change the text to the same thing, firing the method
text.Text = text.Text
}
So you want to check when any of the text boxes are changed, then check if the new input in the changed textbox is a number (I'm assuming an integer) and then display a message in a label if it's not a number.
public MainForm()
{
InitializeComponent();
foreach (Control control in this.Controls)
{
if (typeof(control)==typeof(TextBox))
{
(control as TextBox).TextChanged += CommonHandler_TextChanged;
}
}
}
private void CommonHandler_TextChanged(object sender, EventArgs e)
{
int number;
string input=(sender as TextBox).Text;
bool isnumber=false;
isnumber = Int32.TryParse(input, number);
if(isnumber==false)
{
yourLabel.Text = "This textbox contains an incorrect number: "
+(sender as TextBox).Name;
}
else{ /*use the number*/ }
}

Flickring issue in Textbox when using auto complete in c#

i am using custom auto source to the text box. But the problem is, when i am entering key , if the suggestion list is high then the textbox flickers before showing suggestion.
private void txtSearch_TextChanged(object sender, EventArgs e)
{
if (txtSearch.Text != "")
{
string templateSearchTxt = txtSearch.Text;
foreach (String template in templateName) // templateName contains list of string
{
if (template.ToUpper().StartsWith(templateSearchTxt.ToUpper()))
{
suggestion.Add(template);
}
}
}
}
I have declared following code on form load event
suggestion = new AutoCompleteStringCollection();
txtSearch.AutoCompleteCustomSource = suggestion;
txtSearch.AutoCompleteMode = AutoCompleteMode.Suggest;
txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource;
I will seriously encourage you to use a Combobox with its AutoCompleteMode set to Suggest and attach the autocomplete list to it (as its AutoCompleteSource). It'll performt way better than your textchanged event listener.

Categories

Resources