Looking for a way to display a default value in a list box when launching the program e.g. the second the program is launched a default value is held and displayed in that list box until the user adds their own input?
Any ideas?
You can set a collection of items in the properties window at design time or you can add via code as shown here:
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("Default Value");
}
Related
I'm writing a program in c# (visual studio, windows forms) which involves the user selecting a name from a combo box, and then clicking a button which brings them to a quiz on another form. I want a text file to be created at the end of the quiz showing the name selected and the quiz results, followed by a "~" symbol.
I honestly don't know where to start. I'm using stream reader.
This is the code I used for the combo box and the button that sends you to the next form
private void quizSelectPupilCB_SelectedIndexChanged(object sender, EventArgs e)
{
selectedClass = quizSelectPupilCB.SelectedItem.ToString();
}
private void quizStartBTN_Click(object sender, EventArgs e)
{
Quiz2 formQuiz2 = new Quiz2();
formQuiz2.Show();
this.Hide();
}
How do I use this data on another form?
I don't even know where to start. I tried looking up things like "how to access data from one form and put it in another c#" and similar things but everything I found was either not what I was looking for, or worded in a really confusing way.
As a variant you can save selectedItem to String/StringCollection, then to app settings(Settings.Default). And on another form get this value back.
I am trying to retrieve selected text when a Data-grid View cell is in edit mode. I've attached custom Menu strip with Data-grid View.
Attached is the snapshot of what is done till now.
Now, I need to retrieve the highlighted Text i.e. "together form" (in case of attached snapshot) when any of Context Menu Item is clicked.
private void tagToolStripMenuItem_Click(object sender, EventArgs e)
{
// I want to access highlighted Text here
}
I have exhausted the input parameters : No help from these two parameters.
I have also studied properties of respective Data-grid View but unable to find anything suitable yet.
Any help would be appreciated.
You can get the editing control from EditingControl property if DataGridView and check if it's a TextBox, get its SelectedText:
private void tagToolStripMenuItem_Click(object sender, EventArgs e)
{
if (dataGridView1.EditingControl is TextBox)
{
var textBox = (TextBox)dataGridView1.EditingControl;
MessageBox.Show(textBox.SelectedText);
}
}
The editing control of DataGridViewTextBoxColumn is DataGridViewTextBoxEditingControl which is derived from TextBox.
I am trying to copy values from one textbox to another textbox when user clicks a button. It seems to be a simple solution but for some reason when I click the coppyButton1 on the form, the value from uid1 (TextBox1) not getting copied into uid2(TextBox2). Hoping for feedback.
Code:
private void copyButton1_Click(object sender, EventArgs e)
{
uid2.Text = uid1.Text;
}
You can associate data to the clipboard incredibly easy:
Clipboard.SetText(txtCopyText.Text);
That would take the value of the textbox, then store to the clipboard.
protected void btnCopy_Click(object sender, EventArgs e)
{
// You would want to validate the contents of the textbox before copying.
if(!string.IsNullOrEmpty(txtCopy.Text))
Clipboard.SetText(txtCopy.Text);
}
If you simply want to force the value from one field to another, then the code you have above would force the value to be set. But to apply to the clipboard for copy and paste, you would do the above.
The only reason that code might not work would be if you don't have the textbox instantiated, or those fields are on another form that deviates from your btnCopy. Or you tabbed and allowed intellisense to reverse your copied data, ie one vs two. Your code:
ui2.Text = ui1.Text;
Is the field you thought you were copying from ui1.Text?
Update
To get data from the clipboard, you would do the following:
if(Clipboard.ContainsText(TextDataFormat.Text))
txtPaste.Text = Clipboard.GetText(TextDataFormat.Text);
I have a ComboBox whose DropDownStyle is DropDown, allowing the user to type into it and its AutoCompleteMode is Suggest. The problem is that if the ComboBox is currently open and the user starts typing into it the auxiliary drop-down list appears but clicking on an item from it actually selects the item from the ComboBox's original drop-down list residing under the mouse at the time of click.
I would prefer if while the ComboBox's drop-down list is open the user could not type into it and would like to know if there is a more elegant solution than:
Setting the AutoCompleteMode to None when the ComboBox is open
Possibly changing the DropDownStyle to DropDownList on the OnClick event (haven't tried, but the theory is sound)
Manipulating (or restricting) the entered text while the list is open
Similar
As an option you can handle KeyPress event of the ComboBox and close the dropdown. It keeps the autocomplete menu open, but closes dropdown:
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
this.comboBox1.DroppedDown = false;
}
As another option, you can handle DropDown and DropDownClosed events of the ComboBox and disable autocomplete in DropDown and enable it again in DropDownClosed event:
private void comboBox1_DropDown(object sender, EventArgs e)
{
this.comboBox1.AutoCompleteMode = AutoCompleteMode.None;
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
}
You can create a new class deriving from ComboBox and override corresponding OnXXXX methods and put the logic there. This way you encapsulate the fix in the control class instead of handling events in your form and will have a reusable bug-free control and a more clean code.
ALL,
I have a DataGridView control on my WinForms application with the selection property as "Entire Row" and no multi-selection. I also attach the SelectionChanged delegate to it, where I need to get the currently selected row.
private void order_SelectionChanged(object sender, EventArgs e)
{
ordersItemIndex = order.SelectedRows[0].Index;
}
Problem is, when the program starts, there should be no selection at all and only later user can change the selection with mouse or keyboard. So in my Form_Load() event I have this:
order.ClearSelection();
However, this code path throws an exception on the start-up of the program.
Is there a nice way to tell the program "We are loading the form, don't call the delegate", without any additional variable?
Thank you.
You can add your order_SelectionChanged after you clear selection in Form_Load() (not in InitializeComponents())
But better check in your handler is there any selected rows.
private void order_SelectionChanged(object sender, EventArgs e)
{
if (order.SelectedRows.Count > 0)
ordersItemIndex = order.SelectedRows[0].Index;
}