I managed to create textboxes dynamically in C#. When a textbox has text, can I make it disappear when I click on it?
I need to put a word inside textboxes that is the result of a select in Oracle.
Assign a value to the Text property of the TextBox. You could subscribe then to the GotFocus event, and set the text value to an empty string.
// Holds a value determining if this is the first time the box has been clicked
// So that the text value is not always wiped out.
bool hasBeenClicked = false;
private void TextBox_Focus(object sender, RoutedEventArgs e)
{
if (!hasBeenClicked)
{
TextBox box = sender as TextBox;
box.Text = String.Empty;
hasBeenClicked = true;
}
}
javascript is good for the delete.
onclick="$(this).val('');"
alternatively you can use HTML5 placeholder
Related
I have five RadioButton and five ComboBox controls.
Each RadioButton is connected to a ComboBox.
When I activate one RadioButton, the corresponding ComboBox, it gets enabled.
Now when I choose another RadioButton, the information in the previously selected ComboBox should clear but does not!
I have tried with ComboBox.Clear() as well as ComboBox.Reset(), but it doesn't work.
Here is my code for one of the ComboBox and RadioButton
if (radioButtondinner.Checked == true)
{
comboBoxdinner.DataSource = DList.Dwork();
comboBoxdinner.DisplayMember = "dinner";
}
As I said in comment: you can use one Combobox and only to change data sources when you check other RadioButton that should work sure
But If you want to have more Combobox then just type in else statements
comboBox.DataSource = null;
// create a check change event and use this.
private void radioButtondinner_CheckedChanged(object sender, EventArgs e)
{
if (!radioButtondinner.Checked)
{
// if you want to clear only the text or selected item text
comboBoxdinner.Text = String.Empty;
// if you want to clear the entire data source
comboBoxdinner.DataSource = null;
}
}
I am currently creating an online shop on winform in c#.
At the moment I am creating a 'shopping basket' related textbox where if a user clicks on a particular radio button the textbox shows the description of the product in the text box.
I have grouped my radio buttons in a group box and would like to know whether there is anything equivalent to a 'SelectedIndex' command for all radio buttons? Thanks.
Simply subscribe all radio buttons to the same event. Then you can act on which is checked and act accordingly instead of having duplicate code for each button.
Below is a simple example that set a text box's Text property to display which is checked.
Form class
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButtons_CheckedChanged(object sender, EventArgs e)
{
//Do whatever you need to do here. I'm simple setting some text based off
//the name of the checked radio button.
System.Windows.Forms.RadioButton rb = (sender as System.Windows.Forms.RadioButton);
textBox1.Text = $"{rb.Name} is checked!";
}
}
In the .designer.cs file
//Note that the EventHandler for each is the same.
this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
If you want to be able to select more than one radiobutton at a time, I suggest you to use checkboxes instead of radiobuttons. You can assign all their events to the same single event and control which of the checkboxes are checked.
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkBoxControl = (CheckBox) sender; // You can use this variable to see which one of the checkbox is checked.
}
I have a specific problem with a combobox in visual studio.
I use it to let the user type text in the textbox part of the combobox which immediately starts a SQL requst.
The result should be displayed in the dropdownlist part of the combobox.
(DropDownStyle is set to DropDown)
private void UpdateParent(object sender, EventArgs e)
{
ParentListChange(); //Update the listitems
//prevent from opening at the beginning
if (!ParentSelect.Text.Trim().Equals(""))
{
//my problem
ParentSelect.DroppedDown = true;
Cursor.Current = Cursors.Default;
}
}
But as soon as the drop down opens the first item gets selected and the whole text of it will paste into the textbox.
So if you start to write more then one letter in a row the first one "disappears" because the second typed letter replaces the selected text.
I know there is a similar post but the answeres did not help since they would be to slow (user would have to wait for about a second to type on):
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
var savedText = comboBox1.Text;
comboBox1.DroppedDown = true;
comboBox1.Text = savedText;
comboBox1.Select(savedText.Length, 0);
}
Or would not worke with opening the drop down list, which is essential:
comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
Is there a way to just disable the "select first item" thing?
One way to achieve is to have custom user control.
You can have a TextBox overlapping your ComboBox.
So, user will be interacting with Textbox only, and as per text entered in TextBox(using appropriate Event - key press will be fine), you can query in database and load ComboBox with the result of query.
i cant edit the text that's in the text box. i need the textbox to show text at the start which then can be edited.
imageName shows up in the text box but i cant edit it to something else.
private void image_NameTextBox_TextChanged(object sender, EventArgs e)
{
string imageName = (dictionaryDataSet1.Tables[0].Rows[selectedIndex]["Image Name"].ToString());
image_NameTextBox.Text = imageName;
}
Looks like you defined a TextChanged-Event to your TextBox. Every time you edit your Text you are calling your handler:
private void image_NameTextBox_TextChanged(object sender, EventArgs e)
{
string imageName = (dictionaryDataSet1.Tables[0].Rows[selectedIndex]["Image Name"].ToString());
image_NameTextBox.Text = imageName;
}
The result is: You are setting the Text back to the old value.
Remove the event from your TextBox and fill your Text in a different methode.
You have your code in the TextChanged event which means everytime you try and edit the text it just sets it right back to the same thing. Move your code outside of the TextChanged event (somewhere it is still functional) and then try editing your text.
I want to use a ComboBox with the DropDownList style (the one that makes it look like a button so you can't enter a value) to insert a value into a text box. I want the combobox to have a text label called 'Wildcards' and as I select a wildcard from the list the selected value is inserted in to a text box and the combobox text remains 'Wildcard'. My first problem is I can't seem to set a text value when the combobox is in DropDownList style. Using the properties pallet doesn't work the text value is simply cleared when you click off, adding comboBox.Text = "Wildcards"; to form_load doesn't work either. Can anyone help?
The code you specify:
comboBox.Text = "Wildcards";
...should work. The only reason it would not is that the text you specify is not an item within the comboBox's item list. When using the DropDownList style, you can only set Text to values that actually appear in the list.
If it is the case that you are trying to set the text to Wildcards and that item does not appear in the list, and an alternative solution is not acceptable, you may have to be a bit dirty with the code and add an item temporarily that is removed when the drop-down list is expanded.
For example, if you have a form containing a combobox named "comboBox1" with some items and a button named "button1" you could do something like this:
private void button1_Click(object sender, EventArgs e)
{
if (!comboBox1.Items.Contains("Wildcards"))
{
comboBox1.Items.Add("Wildcards");
}
comboBox1.Text = "Wildcards";
}
private void comboBox1_DropDown(object sender, EventArgs e)
{
if (comboBox1.Items.Contains("Wildcards"))
comboBox1.Items.Remove("Wildcards");
}
That's pretty quick and dirty but by capturing the DropDownClosed event too you could clean it up a bit, adding the "Wildcards" item back as needed.
You can select one of items on formload or in form constructor:
public MyForm()
{
InitializeComponent();
comboBox.SelectedIndex = 0;
}
or
private void MyForm_Load(object sender, EventArgs e)
{
comboBox.SelectedIndex = 0;
}
Try this
comboBox1.SelectedValue = "Wildcards";
This may be a possible solution:
comboBox1.SelectedValue = comboBox1.Items.FindByText("Wildcards").Value;