Select text from multiple textboxes simultaneously - c#

i have 10 textboxes and i need to select text from each one of them.
The problem is that i cant select text from multiple textboxes.
Is there any solution for this problem my code is.
private void Form1_Load(object sender, EventArgs e)
{
createTextBoxes(10);
((TextBox)textBoxes[0]).Select(1, 4);
((TextBox)textBoxes[1]).Select(1, 4); // <- it will not select text
((TextBox)textBoxes[2]).Select(1, 4); // same here
}
Control[] textBoxes;
private void createTextBoxes(int cnt)
{
textBoxes = new Control[cnt];
for (int i = 0; i < cnt; i++)
{
TextBox tb = new TextBox();
tb.Name = i.ToString();
tb.Location = new Point(5, 5 + 14 * i);
tb.Size = new Size(600, 20);
tb.BorderStyle = BorderStyle.None;
tb.Text = "sample text" + i.ToString();
textBoxes[i] = tb;
this.Controls.Add(tb);
}
}

Set the HideSelection property of the texboxes to false. They will maintain selection after losing focus.

Only one control can have a "Focus" at a time... you can't select (ie:highlight) text of multiple controls.
I also just tested by adding a button to the form and posted your 3 "select" snippets there too... nothing showed highlighted. However, when I did a TAB through each control, the first 3 respectfully showed the highlighted section. When I tabbed through the rest, the entire field of the rest of the textboxes were fully selected.
Or are you really trying to accomplish something else...

The text is selected you just can't see it cause of focus.
I ran your code and after doing so tabbed through the controls. The first 3 are selected as specified.

This is possibly not working because even though you've added the TextBox instances to the Form, they have not yet been displayed. Until they are displayed and initially rendered it's likely not possible to initiate a selection on them.

Actually it does, the problem is that the other 2 of your textboxes ([1] and [2]) don't have focus. Only one control can have focus at a time. If you hit tab to give focus to the next TextBox, you'll see the text selected.

Related

Using tag in datagridview

I want to do something like this with windows forms:
Something like tags, but I only want label without colors and "x" value, I just want label with click event, how can I do it without using Telerik? it's not possible to do in datagrid view or something like that? Regards
Tag is a very common .Net property, so the question is bit unclear. But looking at the image and taking a wild guess on what you may want..:
If you want to have the ability to add Labels, let's call them TagLabels during runtime you may want to use a FlowLayoutPanel as their container. It will allow adding more and will take care of the layout no matter what sizes they have.
Example:
To create them we can use a TextBox, which we add to the FLP first. Then we code its PreviewKeyDown event and let the user create a new TagLabel by pressing enter..:
private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter && textBox1.Text != "")
{
Label lbl = new Label {
Text = " " + textBox1.Text, /* some room for the image */
BorderStyle = BorderStyle.Fixed3D,
TextAlign = ContentAlignment.MiddleCenter,
AutoSize = true,
Margin = new Padding(2),
ImageIndex = flowLayoutPanel1.Controls.Count %
imageList1.Images.Count,
ImageList = imageList1,
ImageAlign = ContentAlignment.MiddleLeft,
MinimumSize = new Size(100, 20),
BackColor = Color.LightGoldenrodYellow,
Name = "TagLabel" + (flowLayoutPanel1.Controls.Count)
};
lbl.MouseClick +=lbl_MouseClick ;
flowLayoutPanel1.Controls.Add(lbl);
flowLayoutPanel1.Controls.SetChildIndex(lbl,
flowLayoutPanel1.Controls.Count - 2);
textBox1.Text = "";
}
else
if (e.KeyCode == Keys.Escape)
{
textBox1.Text = "";
}
}
The Click event should be generic for all labels; so we first cast sender to Label and can then code the processing..:
private void lbl_MouseClick(object sender, MouseEventArgs e)
{
Label lbl = sender as Label;
//...
MessageBox.Show(lbl.Name + " : Ouch! You clicked on " + lbl.Text.Trim());
}
This is just a basic piece of code. You can style the labels any way you want and of course also include code for deleting, maybe with a context menu. If those labels shall carry more responsibility, you can and should create a class, probably a Label subclass to hold further data and methods..
I'm also using an ImageList to display images to the left. Do change these details to suit your needs!
Note that there is no reasonable way to add the FLP to an ordinary DataGridView. You may be able to workaround but depending on your needs it may be best to keep them separate. DGVs have Cells and while these can hold special controls this is complicated and will always be restricted by the cells' i.e. the Columns' and the Rows' Size. As an alternative you can check out this example to see how you can insert virtual space to a row to hold a control but neither DGV nor its Cells are containers.

Displaying number of textboxes based on columns inside datagridview

I'm trying to create a number of textboxes based on the row's cell value inside a datagridview and fill the textboxes with the each of the value. I have it so it grabs all the data so far (used MessageBox.Show(textBox.text)) and it shows as expected. However, when I try to create the textboxes, it only creates one (that I can see on the windows form application).
Here is my code for the click event:
qbcDataGridView.RowHeaderMouseClick += (object sender, DataGridViewCellMouseEventArgs e) =>
{
id = Convert.ToInt32(qbcDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString());
// get all the columns and assign them to textboxes text
for (int i = 0; i < qbcDataGridView.Rows[e.RowIndex].Cells.Count; i++)
{
var textBox = new TextBox
{
Name = qbcDataGridView.Rows[e.RowIndex].Cells[i].Value.ToString(),
Font = new Font(qbcDataGridView.Font.FontFamily, 12),
Text = qbcDataGridView.Rows[e.RowIndex].Cells[i].Value.ToString(),
Location = new Point(80, 50)
};
Controls.Add(textBox);
// used for testing
MessageBox.Show(textBox.Text);
}
};
Here is a screenshot(s) of the program in action:
Main screen -
Screen when datagridview clicked -
(I commented out the MessageBox.Show(textBox.Text) but it displays 3, jimmy, test, test, test, member)
I don't know if its the for loop overwriting itself but that seems unlikely as it is showing all the data via the message box. I'm leaning towards the positioning but am not entirely sure.
Any help would be appreciated.
Thanks!
when I try to create the textboxes, it only creates one (that I can see on the windows form application).
Its because the textboxes are overlapped. The only one that's visible is the ID cell (because its z-order is the highest).
Simply space them out so they dont sit on top of each other:
Location = new Point(80 + (i * 50), 50)

Is too many controls on the designer a bad idea?

I am working on a winform app that requires about 65 radio buttons. I was reading on this forum someone saying that they would not put that many buttons on the designer. Instead have the button show at run time. The problem I had with that is the button were not lined up. Even inside a Panel but they were perfectly lined up when a dragged 65 radio buttons on the designer. Is it a bad Idea to have that many buttons on the design? if so why? Thank you.
I think 65 radio buttons is too much and it doesn't matter if you add them using designer or at run-time and you can use a ComboBox instead.
But if you think you need to use those radio buttons at run-time and the problem for you is aligning them in a column (as you said in comments) so:
you can use a Panel and add radio buttons to the panel and set the Dock of radio buttons to be Top.
Also you can use a FlowLayoutPanel and set a break after adding each radio button.
Also you can use TableLayoutPanel with one column and add radio buttons to the rows.
Example - Using Panel
Add a panel to your form and set it's AutoScroll property true. Then write such code to add radio buttons dynamically to the panel:
//You can have radio texts in a list.
//You can load them from database, file or somewhere else, or define them for example here
var list = new List<string>
{
"First radio text", "Second radio text", "Third radio text", "And so on"
};
this.panel1.SuspendLayout();
for (var i = 0; i < list.Count; i++)
{
var r = new RadioButton();
r.Text = list[i];
r.Tag = i;
r.Name = string.Format("r{0}", i);
r.Dock = DockStyle.Top;
r.CheckedChanged += r_CheckedChanged;
this.panel1.Controls.Add(r);
r.BringToFront();
}
this.panel1.ResumeLayout(true);
And here is the hanlder for CheckedChanged event:
void r_CheckedChanged(object sender, EventArgs e)
{
var radio = (RadioButton)sender;
//Use radio here
//for example int radioIndex = (int)radio.Tag;
//for example if(radio.Checked) ...
}
To access to radio buttons with name, you can use:
//find radio number 26
var radio = (RadioButton)this.panel1.Controls["r25"];
Also you can store radio buttons in a List<RadioButton> when you are adding them to the panel and access them later.

Combobox SelectedItem

I'm new to C# and I have a question which I couldn't find somehwere else.
I created a ComboBox like this:
ComboBox lijst = new ComboBox();
Also I added some items and a location:
lijst.Location = new Point(400, 25);
lijst.Text = "Basis";
lijst.Items.Add("Basis");
lijst.Items.Add("Zuilen");
lijst.Items.Add("Vuur");
lijst.Items.Add("Zigzag");
The thing is you can select a item now and press a button which triggers the next method.
private void bereken(object sender, System.EventArgs e)
{
string nr = Convert.ToString(lijst.SelectedIndex);
Label tekstuitvoer = new Label();
tekstuitvoer.Location = new Point(100, 100);
tekstuitvoer.Size = new Size(70, 20);
tekstuitvoer.Text = nr;
this.Controls.Add(tekstuitvoer);
}
But here is my problem. Once you press the button, the outcome of tekstuitover.Text will always be the same. So if you press the button while "Vuur" is selected then the outcome will always be 2. It doesnt matter if you change the combobox item afterwards. The output will always be 2. But when I restart the program and select the fourth option instead of the third, now the outcome always will be 3.
Can you help me with this problem? Is there a way to reset string nr?
Thanks in advance. I hope you guys can help me!
Took me a while to figure out what you were saying there.
Add a SelectedIndexChanged event handler to your combobox, and trigger your label code from there, you can get rid of the button.
However as things stand you are going to create a new label component every time it changes.
Simpler to just add one at design time and set the text property in the event.

How to add items (comboBox) to a tabpage - C#

its my first time using tabs and I've ran into a problem. I'm trying to make it so whenever I press a button, combo boxes will appear inside the tab that is open, and then if the button is pressed again it will add another combo box underneath the first one.
Here is how I did it:
private void buttonLevel4Add_Click(object sender, EventArgs e)
{
for (int i = 0; i < 8; i++)
{
comboBoxModuleSelect.Add(new ComboBox());
System.Drawing.Point p = new System.Drawing.Point(176, 114 + i * 25);
(comboBoxModuleSelect[i] as ComboBox).Location = p;
(comboBoxModuleSelect[i] as ComboBox).Size = new System.Drawing.Size(183, 20);
this.Controls.Add(comboBoxModuleSelect[i] as ComboBox);
}
}
But the problem is that the combo box won't be created in the tab page but underneath the tab page (i.e. on the form). Does a tab page not replace the form area? Please if someone could help I would appreciate it. Thanks
EDIT:
Oh God, another noob moment for me. I changed this line:
this.Controls.Add(comboBoxModuleSelect[i] as ComboBox);
to:
tabpage.Controls.Add(comboBoxModuleSelect[i] as ComboBox);
really sorry, I guess it just helps me to ask the question and think about it.
this.Controls.Add(comboBoxModuleSelect[i] as ComboBox);
to:
tabpage.Controls.Add(comboBoxModuleSelect[i] as ComboBox);

Categories