I made a program like notepad. I want the first line to be the date and time when the note was made. I get this with:
tbEditor.Text = Convert.ToString(DateTime.Now);
But is it possible to disable this first line so the user can't delete it afterwords?
Use a or to display data that doesn't require user intervention. You can use a stack with textblock and then a text box in it to store date and user note respectively. Alternatively, if you want to use a textbox, you can set IsReadOnly property to true OR IsEnabled property to false. But this is not just for the first line, but for the whole textbox. So you would need a separate control to store the Date and Time
Related
Ok, I'm trying to do something a little specific here. I want to get the location of the selected text in a textbox.
To elaborate- I can use location to select text. If I have a textBox1 I could do:
textBox1.SelectionStart = 1;
textBox1.SelectionLength = 4;
That would start at the second letter and select 4 letters.
What I want to do is the opposite: when the user selects text, I want to find out what the start is and what the length is (or what the start is and what the end is. Either will work).
I thought about just searching the string for the selectedtext (textBox1.SelectedText). The problem comes if it is a common word or a string that is used multiple times. For instance.
This is a cat. This is a cat. This is a cat.
If they select the second sentence, using SelectedText to search the string for that specific sentence does me no good. It could be either of the 3.
So, my question is: When the user clicks a button, how do I determine the exact elements that are selected by the user, so that I can later manipulate those specific elements? Important to note the later part- I likely will not only want to manipulate the text when the button is pressed. I will also want to manipulate it later, at a time when the text may no longer be highlighted. This means I'll want to store SOMETHING to tell me what specific parts of the sentence I'm dealing with. If that solution isn't viable, is there a solution you can think of where, in the above "this is a cat" example, the user could select the second sentence, hit a button, and then later I know which sentence was selected when he hit that button?
According to the documentation, SelectionStart and SelectionLength can be both set and read. Just use those.
You dont even need to know the position of selected text to manipulate them, to edit the text that you have selected in the text you can simple set the SelectedText property to the new edited value.
// if textBox1.text = "Hello World World"; with first "World" selected
textBox1.SelectedText = textBox1.SelectedText.Replace("World", "Raj");
// then it becomes "Hello Raj World"
Iam using AutoCompleteMode to my textbox. It will append the Bank names to my textbox. So when I started typing with the first leter all the Bank names with first lettre will come to dropdownlist to my textbox. Now my question is
If user try to entre the data which is not im my dropdownlist, the user should not able to entre the text. So i want user to entr the existing bank names only.
Iam using AutoCompleteCustomSource to the textbox for dropdown.
try something like:
bool foundSome = false;
foreach (var bankName in col)
{
foundSome = bankName.StartsWith(textBox.text);
}
if (foundSome)
//Do some action
You can write this code in 'Validating' to preform for each char inserted in txtbox.
the best way to achieve your requierement is to use 1 textbox and 1 combobox. They both should point to the same collection.
Textbox will behave in autocomplete mode as you described. Once you type, combobox value will be set to first matching value from your collection. If no value matches - combobox value should be set to null or default data.
Combobox will store only corresponding data subset with no possibility to edit the chosen text.
Validation and data retrieval will be done from Combobox value.
Advantages of this approach:
- With large sets of data it will be easier for user to find what he/she needs.
- Lesser code to check if input value belongs to data set or to force belonging.
- No validation is needed.
Possible shortcomings:
- One more control at form.
- Logic to synchronize textbox's text and combobox collection should be implemented.
Have a look at this example first,
click here. I want you to see only the example of how he changed a label into text box after he hits edit.I want to make edit profile page but I don't want to use bind grid view. Is there any possible ways to retrieve some data from table in database to be shown in labels then when uset hits the edit button, the label will change to textbox?
why do you need to show a label ?
you can show it in a text box and set IsReadOnly or ReadOnly to true, set style to flat and remove border(it'll appear like a label), and then on the Edit Action, you can change this ReadOnly/Editable property.
Based on the design requirements, the datagridview can not be edited directly by the user. It is in read-only mode. When the user double-clicks on the cell, the datagridview's read-only property becomes false and the cell accepts keyboard input. However, the raw keyboard input needs to be formatted before it goes in the cell. So, I intercept the KeyPress events as follows:
private void dgw_keyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
At this point the cell is in edited mode and dirty mode. Then I update the Value property in a different method and call dgw.Refresh() which is supposed to display the updated value on the cell. But it won't. it will update only when the current cell is not dirty and is not in edit mode. How can I force the cell display the updated value while it is still in edit mode?
Any ideas?
Use below to refresh the current cell's value, change to suit your EditingControl type
if (dgvMain.EditingControl is TextBox)
{
dgvMain.EditingControl.Text = dgvMain.CurrentCell.Value.ToString();
}
Another method:
Call this method to force a cell to update its display value in edit
mode. This is useful when an external process modifies the cell value
and you want to notify the user of the change, even when a
user-specified change is lost as a result.details
dgvMain.RefreshEdit();
You might be able to do that by implementing the IDataGridViewEditingControl interface. I think that's the way to get the most control over how the cell enters and leaves edit mode. You can find more details in section 5.11 of Mark Rideout's DataGridView FAQ (DOC)
Try DataGridView.EndEdit method.
Commits and ends the edit operation on the current cell.
I solved with the code below.
GrdBudgetTabOver.EndEdit()
This will work,
dgv.EndEdit()
dgv.InvalidateCell(ColIdx, RowIdx)
We have a couple of DateTimePickers on a custom UserControl, on a Form. They are visible, but not enabled (for display purposes only). When the UserControl is loading, the DateTimePickers are assigned values from a DataRow that came from a DataSet which stores a single record returned from a SQL Server stored procedure.
There is an inconsistent behavior in which the users sometimes see today's date instead of the date that was assigned to the DateTimePicker. It doesn't seem to matter whether I assign the date I want to the .Value property or the .Text property:
txtstart.Value = (DateTime) dr["Group_Start_Date"];
txtend.Text = dr["Term_Date"].ToString();
I expect that of the two statements above, the one using the Value property is more appropriate. But, in both cases, today's date is displayed to the user regardless of the values that were in the database. In the case of txtstart.Value, Visual Studio shows me that the value was assigned as expected. So why isn't it displaying that date to the user instead of today's date?
I found the answer. You MUST set the checkbox value to checked to indicate a non-null value.
this.dateSold.Checked = true;
// set to true or false, as desired
this.dateSold.ShowCheckBox = false;
I was having trouble with this too and found that you indeed need to set the .Checked value to True.
I set the Checked property in the properties window and it still didn't work so I just set it in code before assigning the value and that fixed the problem.
It seems to be a problem with having multiple DateTimePickers. I was able to get around the issue (bug?) by programatically creating the DateTimePickers with the values I wanted and adding them to the form.
I ended up going with AB Nolan's suggestion. The reasoning behind a disabled DateTimePicker was never clear to me, so back on 10/23/2009, rather than continue fussing with the control I just displayed the dates I wanted in TextBoxes instead.