Get text from combobox in selected tab - c#

In all of these tabs, I have a combobox with different functions as strings. I want the text under Preview (it's a richtextbox with "Nothing is selected." as a default string) to change whenever I select an item in each of the different tabs' comboboxes. Any idea how I can do that?

You could set every TextChanged event of all of your combobox to the same event handler
comboBox1.TextChanged += CommonComboTextChanged;
comboBox2.TextChanged += CommonComboTextChanged;
comboBox3.TextChanged += CommonComboTextChanged;
comboBox4.TextChanged += CommonComboTextChanged;
private void CommonComboTextChanged(object sender, EventArgs e)
{
ComboBox cbo = sender as ComboBox;
richTextBox.Text = cbo.Text;
}
However, if you change the DropDownStyle of your combos to ComboBoxStyle.DropDownList then you could use the SelectedIndexChanged event that will be triggered only when your user changes the item selected by using the DropDown List.
comboBox1.SelectedIndexChanged += CommonComboIndexChanged;
comboBox2.SelectedIndexChanged += CommonComboIndexChanged;;
comboBox3.SelectedIndexChanged += CommonComboIndexChanged;;
comboBox4.SelectedIndexChanged += CommonComboIndexChanged;;
private void CommonComboIndexChanged;(object sender, EventArgs e)
{
ComboBox cbo = sender as ComboBox;
richTextBox.Text = cbo.Text;
}
Finally to set the content of the RTB to the one of the combo in the current tab page you need to handle the TabChanged event of your tabControl
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
switch(e.TabPageIndex)
{
case 0:
richTextBox.Text = comboBox1.Text;
break;
// so on for the other page and combos
}
}
Or if your comboboxes share a common initial part of their names
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
var result = e.TabPage.Controls.OfType<ComboBox>()
.Where(x => x.Name.StartsWith("cboFunction"));
if(result != null)
{
ComboBox b = result.ToList().First();
richTextBox.Text = comboBox1.Text;
}
}

Related

How to design a keypad in C#?

I am new to C# and I am planing to design my own keypad but I don't know how/where to start. as shown in photo, I have 4 textBoxes the keypad buttons.
The first problem came into my mind was: how can I detect the cursor location (which textBox is the cursor in?).
So for example if I had only one textbox then it is easy I could write inside button1 : textBox1.text = "1" and inside button2 : textBox1.text = "2" and inside button_A : textBox1.text = "A".... and so on but I have 4 textBoxes and it is confusing.
Can you please provide me with an idea or what to write inside each button to print its value in the textbox which the cursor is in.
Thank you professionals.
Firstly, have a textbox that represents the one that is selected (outside of subroutines but inside the class):
TextBox SelectedTextBox = null;
And then make the "Click" event of each TextBox look like this:
private void textBoxNUM_Click(object sender, EventArgs e)
{
SelectedTextBox = sender as TextBox;
}
And then make the "Click" event of each Button look like this:
private void buttonNUM_Click(object sender, EventArgs e)
{
if (SelectedTextBox != null)
{
SelectedTextBox.Text = buttonNUM.Text;//Or set it to the actual value, whatever.
}
}
Or if that one doesn't work, this should.
private void buttonNUM_Click(object sender, EventArgs e)
{
if (SelectedTextBox != null)
{
(SelectedTextBox as TextBox).Text = buttonNUM.Text;//Or set it to the actual value, whatever.
}
}
To check if a textbox is focused you can do
if(textbox1.Focused)
{
//Print the value of the button to textbox 1
}
else if (textbox2.Focused)
{
//Print the value to textbox 2
}
UPDATE:
Since the textbox will lose focus when you click the button, you should have a temporary textbox (ie lastTextboxThatWasFocused) which is saved to everytime a textbox gains focus. Write an OnFocused Method and do something like
public void Textbox1OnFocused(/*Sender Event Args*/)
{
lastTextboxThatWasFocused=textbox1;
}
Then on button click you can do
if(lastTextboxThatWasFocused.Equals(textbox1))
{
//ETC.
}
You can give something along these lines a try. Create a generic click handler for the buttons and then assign the value to a textbox the text from the button, which happens to be the value. You can check which box was the last one focused in the TextBoxes' Click event. Create a global variable to store which one and use it in the below method.
private TextBox SelectedTextBox { get; set; }
private void NumericButton_Click(object sender, EventArgs e)
{
var clickedBox = sender as Button;
if (clickedBox != null)
{
this.SelectedTextBox.Text += clickedBox.Text;
}
}
private void TextBox_Click(object sender, EventArgs e)
{
var thisBox = sender as TextBox;
if (thisBox == null)
{
return;
}
this.SelectedTextBox = thisBox;
}
Try this code:
TextBox LastTxtBox;
private void textBox_Enter(object sender, EventArgs e)
{
LastTxtBox = sender as TextBox;
}
private void button_Click(object sender, EventArgs e)
{
LastTxtBox.Text = this.ActiveControl.Text;
}
Add textBox_Enter function to all textboxes enter event.
Add button_Click to all buttons click event.
Button Enter Event
Control _activeControl;
private void NumberPadButton_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (_activeControl is TextBox || _activeControl is RichTextBox)
{
_activeControl.Text += btn.Text;
if (!_activeControl.Focused) _activeControl.Focus();
}
}
TextBox or RihTextBox Enter Event
private void TextBoxEnter_Click(object sender, EventArgs e)
{
_activeControl = (Control)sender;
}

How to make visible another combo box on selection of a value in current combo box?

I have a combobox(CB1) and it contains items like 1,2,3 and i want to make another combobox (CB2) visible when i select the value 3 from CB1. Which property should i user. I am working on a windows based application and I am using C# as the code behind language. An example would be great to solve the problem.
The combo box CBFormat consists of a list of items as follows:
var allWiegandFormat = WiegandConfigManager.RetrieveAllWiegandFormats();
var allWiegandList = new List<IWiegand>(allWiegandFormat);
CBFormat.Items.Add(allWiegandList[0].Id);
CBFormat.Items.Add(allWiegandList[3].Id);
CBFormat.Items.Add(allWiegandList[4].Id);
CBFormat.Items.Add(allWiegandList[5].Id);
CBProxCardMode.Items.Add(ProxCardMode.Three);
CBProxCardMode.Items.Add(ProxCardMode.Five);
Now I want to show the Combo box of CBPorxCardMode when i select the second item from CBFormat combo box.
Try this
Private void CB1_SelectedIndexChanged(object sender, System.EventArgs e)
{
Combobox CB = (ComboBox) sender;
if(CB.SelectedIndex != -1)
{
int x = Convert.ToInt32(CB.Text)
if(x == 3)
{
CB2.Visible = True;
}
}
}
Use SelectionChangeCommitted event and subscribe your CB1 to it:
// In form load or form initialization
cb1.SelectionChangeCommitted += ComboBoxSelectionChangeCommitted;
// Event
private void ComboBoxSelectionChangeCommitted(object sender, EventArgs e)
{
cb2.Visible = cb1.SelectedItem != null && cb1.Text == "3";
}
If it is Winforms You can use Something Like this
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add(1);
comboBox1.Items.Add(2);
comboBox1.Items.Add(3);
comboBox2.Visible = false;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "3")
{
comboBox2.Visible = true;
}
else
{
comboBox2.Visible = false;
}
}
Hope this helps.,
Start with the CB2 Visible property set to False and add a event handler code for the SelectedIndexChanged on the CB1 through the WinForms designer
private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
if(comboBox.SelectedItem != null)
{
int id = Convert.ToInt32(comboBox.SelectedItem)
cbo2.Visible = (id == 3)
}
}
This is supposing the ID, that you are adding at the first combo, is an Integer value as it seems.
Also rembember that SelectedIndexChanged event will be called even if you change the SelectedItem programmatically and not just when the user changes the value. Also, if the user change again the selection moving away from the ID==3 the method will set again the Cbo2 not visible.

how to create events on dynamic `textboxes`

I managed to create textboxes that are created at runtime on every button click. I want the text from textboxes to disappear when I click on them. I know how to create events, but not for dynamically created textboxes.
How would I wire this up to my new textboxes?
private void buttonClear_Text(object sender, EventArgs e)
{
myText.Text = "";
}
This is how you assign the event handler for every newly created textbox :
myTextbox.Click += new System.EventHandler(buttonClear_Text);
The sender parameter here should be the textbox which sent the even you will need to cast it to the correct control type and set the text as normal
if (sender is TextBox) {
((TextBox)sender).Text = "";
}
To register the event to the textbox
myText.Click += new System.EventHandler(buttonClear_Text);
Your question isn't very clear, but I suspect you just need to use the sender parameter:
private void buttonClear_Text(object sender, EventArgs e)
{
TextBox textBox = (TextBox) sender;
textBox.Text = "";
}
(The name of the method isn't particularly clear here, but as the question isn't either, I wasn't able to suggest a better one...)
when you create the textBoxObj:
RoutedEventHandler reh = new RoutedEventHandler(buttonClear_Text);
textBoxObj.Click += reh;
and I think (not 100% sure) you have to change the listener to
private void buttonClear_Text(object sender, RoutedEventArgs e)
{
...
}
I guess the OP wants to clear all the text from the created textBoxes
private void buttonClear_Text(object sender, EventArgs e)
{
ClearSpace(this);
}
public static void ClearSpace(Control control)
{
foreach (var c in control.Controls.OfType<TextBox>())
{
(c).Clear();
if (c.HasChildren)
ClearSpace(c);
}
}
This should do the job :
private void button2_Click(object sender, EventArgs e)
{
Button btn = new Button();
this.Controls.Add(btn);
// adtionally set the button location & position
//register the click handler
btn.Click += OnClickOfDynamicButton;
}
private void OnClickOfDynamicButton(object sender, EventArgs eventArgs)
{
//since you dont not need to know which of the created button is click, you just need the text to be ""
((Button) sender).Text = string.Empty;
}

Get name of control when mouse hovers over it C#

I am adding ComboBoxes dynamically at runtime as shown below.
The problem that I am having is that i do not know which of the comboboxes the user is using.
For eg. The user decides to add 5 comboBoxes to the form, and then goes to the first comboBox,and selects a value, I need to retrieve the value of that comboBox.
What the below code is doing - My approach
I am adding a comboBox to a FlowlayoutPanel and the retrieve its name based on the mouse co-ordinates.... this by the way is not working... and I have no idea what to do.
Any help is greatly appreciated.
public partial class Form1 : Form
{
int count = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
count += 1;
ComboBox cb = new ComboBox();
cb.Name = count.ToString();
cb.MouseHover += new EventHandler(doStuff);
Label lb = new Label();
lb.Text = count.ToString();
flowLayoutPanel1.Controls.Add(cb);
flowLayoutPanel1.Controls.Add(lb);
}
public void doStuff(object sender, EventArgs e)
{
label1.Text = flowLayoutPanel1.GetChildAtPoint(Cursor.Position).Name;
}
}
}
You could try:
cb.SelectionChangeCommitted += selectionChangedHandler
...
void selectionChangedHandler(object sender, EventArgs e) {
ComboBox cb = (ComboBox)sender;
label1.Text = cb.Name;
// Do whatever else is needed with the combo box
}
The SelectionChangeCommitted event is "raised only when the user changes the combo box selection", which sounds like what you're after.
The combobox that raised the event in your doStuff-eventhandler is in the sender-parameter. Try casting it to a checkbox likte this:
ComboBox boxThatRaisedTheEvent = (ComboBox)sender;
string text = ((ComboBox)this.GetChildAtPoint(pt)).Text;
public void DoStuff(object sender, EventArgs e)
{
var comboBox = sender as ComboBox;
var name = (comboBox != null ? comboBox.Name : null);
}
this code casts the 'sender' parameter to a ComboBox object and if the cast is done correctly assings the ComboBox name to the string 'name', otherwise 'name' is null.
Tip: The C# coding style suggests that method names should start with capitalized letter.
You can try something like:
flowLayoutPanel1.Controls.OfType<ComboBox>().FirstOrDefault(cb => cb.Name.Equals(NAME_OF_COMBOBOX))
Or better:
ComboBox box = (ComboBox)sender;

Datagridview ComboBoxCell set default value?

I have a datagridview with lots of data, and when I add a new line, the first column's last row creates a new ComboBoxCell which contain four items. But I can't set the default value ("DropDown") for the combobox. Every time I must manually select "DropDown". What is the solution?
DataGridViewComboBoxCell dgvCell = new DataGridViewComboBoxCell();
dgv[1, dgv.Rows.Count - 1] = dgvCell;
string[] controltype = {"DropDown", "CheckBoxList", "ListControl", "Tree" };
dgvCell.DataSource = controltype;
private void dataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells[4].Value = "DropDown";
}
it,s easy,If you have a ComboBox Column in your DataGrid View and you want to know what is the selected index of the combo box, then you need to do this:
1. Handle the EditingControlShowing event of DataGrid View. In this event handler, check if the current column is of our interest. Then we create a temporary ComboBox object and get the selected index:
Code
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
// Check box column
ComboBox comboBox = e.Control as ComboBox;
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
}
void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = ((ComboBox)sender).SelectedIndex;
MessageBox.Show("Selected Index = " + selectedIndex);
}
try :
if(!isPostBack)
{
dgvCell.SelectedItem=controltype[0].toString();
}

Categories