get textbox that is in the selected tab - c#

I have a tabcontrol with numerous tabs that all contain a textbox. How can I select the textbox that is in the currently selected tab?
I have this which captures the tabchanged event and tells me which tab is selected, but I cannot figure out how to find the textbox that is in the tab and do
textbox.Select(0, 0);
to select certain text in this textbox...
private void onTabChange(Object sender, TabControlEventArgs e)
{
}

This really sounds like a design mistake. High odds that this TextBox should not be on a tab page at all. If you want to have one text box to be present on every tab page then that's possible, Winforms makes it easy to move controls:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
textBox1.Parent = tabControl1.SelectedTab;
}
If you really meant for any text box to be picked, like the first one in the tab order then:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
var box = tabControl1.SelectedTab.Controls.OfType<TextBox>().Reverse().FirstOrDefault();
if (box != null) {
// etc...
}
}

Use This:
Tab TabView = (Tab)sender;
TextView txt_Tab = (TextView)TabView.FindControl("TextBoxName");

Try this:
TextBox myTB = tabControl2.SelectedTab.Controls[0] as TextBox;
myTB.Select(0, 0);

I think the following links can provide you some hints about your problem
How to access controls that are inside a TabControl tab?
and
How to get control(s) from TabPage in C#?

Related

Getting a control name/Displaying tooltip text

I am developing a form with multiple options that simulates a signup form, and I want to display some tips and descriptions in a RichTextBox located by the options when the user's mouse hover by it's GroupBoxes.
Since I am fairly new to programming, I don't know if getting all the controls names one by one is the optimal, so I want to grab the controls' names inside of the tabControl control that I am using to organize everything.
private void TabControl1_MouseHover(object sender, EventArgs e)
{
foreach(Control c in this.Controls)
{
string name = c.Name;
TooltipText(name);
}
}
And I also have a method where I will write the text that will be displayed in the RichTextBox.
private string TooltipText(string name)
{
if(name == "Name:")
{
return "blabla";
}
else
{
return "none";
}
}
I've tried a generic method to show a message box if the control was detected and, as I suspected, nothing showed up:
private void TooltipText(string name)
{
if(name == "LBL_Name")
{
MessageBox.Show("hey");
return;
}
}
How can I properly detect the Groupboxes or other types of Controls inside of the TabControl control, and also display the text in the box beside it?
You don't have to create your own Tool Tips. The .net WinForms provides a ToolTip class. https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.tooltip?view=netframework-4.8
I added 2 radio buttons to a group box in design view.
Try it and see.
private void Form1_Load(object sender, EventArgs e)
{
ToolTip tip = new ToolTip();
tip.AutoPopDelay = 5000;
tip.InitialDelay = 1000;
tip.ReshowDelay = 500;
tip.SetToolTip(radioButton1, "Choose to Add Onions");
tip.SetToolTip(radioButton2, "Choose to Add Pickles");
}

Event that would listen to any label click in form

Well, I'm making a chess game that's based on labels. I need to listen for label click, so when user clicks on an label, I get the name of label he clicked. I know I can do it for each label, but is there an universal event that would help me do the same thing for all of them in one event / loop?
Suppose you have taken 64 labels.
In windows form, on click event of Label1 you will write following code:
private void Label1_Click(object sender, EventArgs e)
{
var label = sender as Label;
MessageBox.Show(label.Name);
}
For remaining 63 Lables, In Design view, select all 63 lables by using Ctrl key --> Go to Property window --> Under Event option select Click option --> From dropdownlist select 'Label1_Click' option.
Just finish & run the application.
You can use a Panel
structure to group your labels and then call the desired event on that Panel, so it will trigger whenever you click one of it's elements.
Another solution would be to identify your label with the coordinates of the mouse click (the amount of code that requires depends how you placed them of course).
Like mentioned in the comments you can assign one event to all...
List<Label> lbls = this.Controls.OfType<Label>().ToList();
foreach (var lbl in lbls)
{
lbl.Click += lbl_Click;
}
void lbl_Click(object sender, EventArgs e)
{
Label lbl = sender as Label;
MessageBox.Show(lbl.Name);
}
You can assign these methods to every label you need to manage in the VS form designer (you go to events of controls, at the click line and select the method in the list instead of double click on it):
private void Label_Click(object sender, EventArgs e)
{
var nameLabel = ( sender as Label )?.Name ?? "Error";
// ...
}
private void Label_Enter(object sender, EventArgs e)
{
( sender as Label ).Cursor = Cursors.Hand;
}
private void Label_Leave(object sender, EventArgs e)
{
( sender as Label ).Cursor = Cursors.Default;
}
Cursor change added for convenience if you want.
If you want to dynamically assign events, you can use the #caner answer, and you can group all in a panel to parse Panel.Controls and assign event.
You can create a custom label class that inherits from Label. You can then subscribe to the Click event of the base class and do your thing.
public class MyLabel : Label
{
public MyLabel()
: base()
{
Click += ProcessClickEvent;
}
private void ProcessClickEvent(object sender, System.EventArgs e)
{
// Do what you want to do
}
}

How to add custom control at Run time in c#

i have one list box which has button, Textbox, Label. During runtime i will drag and drop the item from the list box, according to the selection the dynamic control will be created. (For example, If i select and drag Button from list box and drop it on the Windows Form, the button will be created). As same as i have created a CustomControl for Button. how can i add it in my list box at runtime? i mean while i drag and drop the button from the listbox the custom button should be generated. How to Do it?
Did You try this ?
var list = new ListBox();
list.Controls.Add(new Button());
If You need to dynamically create a class at runtime - take a look at this SF article How to dynamically create a class in C#?
For drag drop, you will need to set up 3 events:
A mouse down event on the list box to trigger the dragging:
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
listBox1.DoDragDrop(listBox1.SelectedItem, DragDropEffects.Copy);
}
A drag enter event on your form (or panel in this example):
private void panel1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
e.Effect = DragDropEffects.Copy;
}
}
And finally the drop event on the form/panel:
private void panel1_DragDrop(object sender, DragEventArgs e)
{
Control newControl = null;
// you would really use a better design pattern to do this, but
// for demo purposes I'm using a switch statement
string selectedItem = e.Data.GetData(DataFormats.Text) as string;
switch (selectedItem)
{
case "My Custom Control":
newControl = new CustomControl();
newControl.Location = panel1.PointToClient(new Point(e.X, e.Y));
newControl.Size = new System.Drawing.Size(75, 23);
break;
}
if (newControl != null) panel1.Controls.Add(newControl);
}
For this to work you must set "AllowDrop" to true on the target form/panel.
Use #Marty's answer to add the custom control to the listbox. Override the ToString() for a better description. There are so many ways of doing it. The important part is deciding what data type the list items will be and making sure that the correct type name is used in the e.Data.GetDataPresent method. e.Data.GetFormats() can help determine what name to use.

Designing a dynamic panel

I am new to C# and I want to design a GUI for a image processing application in c#. I have a very basic rudimentary layout designed as shown below
Here, the image plane is fixed and it will show a live stream video. I have designed all the buttons frame and the side panel. But I do not know how to dynamically change the side panel for each button I click. For example, If I click button1_1, I want some things in the side panel and for button1_2, some other things in it. How do I go about doing it.
EDIT:
Thanks for the answers. I see tab controls is an option. But I want a new panel evertime a click a button. which can further open forms. Is it possible?
OK, let's see. It's easy to do with "TabControl" or array of "Panel"s.
1.Do it with TabControl.
You can design GUI in TabControl in multiple subTabs(if you don't know how please ask.). Then you change it in button click event, to make subTab you wanna show(which means make it visiable and not visiable for other subTabs.)
2.Do it with array of panel.
You can use panel[] panels. In button click event, you hide other panels and show the one you want.
Hope answer helps you!
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedIndex = 0;
tabControl1.TabPages[0].Text = "First";
}
private void button2_Click(object sender, EventArgs e)
{
tabControl1.SelectedIndex = 1;
}
private void button3_Click(object sender, EventArgs e)
{
tabControl1.SelectedIndex = 2;
}
you may want to add split container in your form.
Create UserControl for each buttons.
Code for the button click event
//Button1Click Event
private void button1_Click(object sender, EventArgs e)
{
UserControl1 m_UserControl = new UserControl1();
splitContainer1.Panel2.Controls.Clear();
splitContainer1.Panel2.Controls.Add(m_UserControl);
}
//Button2Click Event
private void button2_Click(object sender, EventArgs e)
{
UserControl2 m_Usercontrol2 = new UserControl2();
splitContainer1.Panel2.Controls.Clear();
splitContainer1.Panel2.Controls.Add(m_Usercontrol2);
}
you can do this if you want to change what usercontrol display in a panel at run time.
Correct me if i misunderstood your question.
In WinForms, you could use a tab control and just change the selected tabs index when a button is pressed. More specifically, when its click event is fired. Here is a good tutorial on using the TabControl and here is a tutorial on wiring up click events.
EDIT:
This is a better tutorial.
Since you can't hide the tabs of a tabcontrol without using WPF, you may need to use something else, if you don't like the way they look. A good workaround if you only have a couple of buttons and thus views, would be to use panels. When button one is clicked show panel one and hide panel two, etc. Here would be the code:
private void button1_Click(object sender, EventArgs e)
{
pane2.visible = false;
pane1.visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
pane1.visible = false;
pane2.visible = true;
}
Hope this helps you!

How to select all text in Winforms NumericUpDown upon tab in?

When the user tabs into my NumericUpDown I would like all text to be selected. Is this possible?
private void NumericUpDown1_Enter(object sender, EventArgs e)
{
NumericUpDown1.Select(0, NumericUpDown1.Text.Length);
}
(Note that the Text property is hidden in Intellisense, but it's there)
I wanted to add to this for future people who have been search for Tab and Click.
Jon B answer works perfect for Tab but I needed to modify to include click
Below will select the text if you tab in or click in. If you click and you enter the box then it will select the text. If you are already focused on the box then the click will do what it normally does.
bool selectByMouse = false;
private void quickBoxs_Enter(object sender, EventArgs e)
{
NumericUpDown curBox = sender as NumericUpDown;
curBox.Select();
curBox.Select(0, curBox.Text.Length);
if (MouseButtons == MouseButtons.Left)
{
selectByMouse = true;
}
}
private void quickBoxs_MouseDown(object sender, MouseEventArgs e)
{
NumericUpDown curBox = sender as NumericUpDown;
if (selectByMouse)
{
curBox.Select(0, curBox.Text.Length);
selectByMouse = false;
}
}
You can use this for multiple numericUpDown controls. Just need to set the Enter and MouseDown Events
I was looking around i had the same issue and this Works for me, first select the Item and the second one selects the Text, hope it helps in future
myNumericUpDown.Select();
myNumericUpDown.Select(0, myNumericUpDown.Value.ToString().Length);
I created an extension method to accomplish this:
VB:
<Extension()>
Public Sub SelectAll(myNumericUpDown As NumericUpDown)
myNumericUpDown.Select(0, myNumericUpDown.Text.Length)
End Sub
C#:
public static void SelectAll(this NumericUpDown numericUpDown)
numericUpDown.Select(0, myNumericUpDown.Text.Length)
End Sub
I had multiple numericupdown box's and wanted to achieve this for all. I created:
private void num_Enter(object sender, EventArgs e)
{
NumericUpDown box = sender as NumericUpDown;
box.Select();
box.Select(0, num_Shortage.Value.ToString().Length);
}
Then by associating this function with the Enter Event for each box (which I didn't do), my goal was achieved. Took me a while to figure out as I am a beginner. Hope this helps someone else out
For selecting all text by mouse click or by Tab button I use:
public frmMain() {
InitializeComponent();
numericUpDown1.Enter += numericUpDown_SelectAll;
numericUpDown1.MouseUp += numericUpDown_SelectAll;
}
private void numericUpDown_SelectAll(object sender, EventArgs e) {
NumericUpDown box = sender as NumericUpDown;
box.Select(0, box.Value.ToString().Length);
}
Try
myNumericUpDown.Select(0, myNumericUpDown.Value.ToString().Length);

Categories

Resources