I am trying to implement Notepad application clone in C#. Now I have a smaller problem in implementation. In a notepad application for implementing find next option, I created a new form which will have a reference to the main form. When Find Next button is clicked on the find form, I will use the richtextbox content in the mainform, current cursor position,direction of search, search string and matchcase to calculate selectionstart for the richtextbox and then update the selection. Now the calculation of selectionstart is working fine. I can get the length of selection from the search string and set selection. I am trying to set selection on the main form by using the reference I passed to the find form. When I do that, richtextbox won't highlight unless I BringToFront the mainForm or close the find form window. So my question is how to achieve selection of contents of richtextbox from another form(child form) just as implemented in windows notepad?
Code snippets:
FormFind.cs
private void btnFindNext_Click(object sender, EventArgs e)
{
qry.SearchString = txtFind.Text;
qry.Direction = oUp.Checked ? "UP" : "DOWN";
qry.MatchCase = chkMatchCase.Checked ? true : false;
qry.Content = mainForm.GetContent();
qry.Position = mainForm.GetCurrentCursorPosition();
FindNextResult result=editOperation.FindNext(qry);
if (result.SearchStatus)
mainForm.SetSelection(result.SelectionStart,
txtFind.Text.Length);
}
MainForm.cs
private void findEditMenu_Click(object sender, EventArgs e)
{
if (formFind == null)
formFind = new FormFind(this);
formFind.Show();
}
public string GetContent()
{
return txtArea.Text.ToString();
}
internal void SetSelection(int selectionStart, int length)
{
txtArea.SelectionStart=selectionStart;
txtArea.SelectionLength = length;
this.BringToFront();
}
internal int GetCurrentCursorPosition()
{
return txtArea.SelectionStart;
}
Related
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");
}
Using VS 2015 and C#...
I have this simple modal Form with just a MaskedTextBox control on it.
Each time after the first that ModalForm is shown with .ShowDialog(), the PromptChar in the control is gone.
To reproduce this issue:
public ModalForm()
{
InitializeComponent();
maskedTextBox1.Mask = "00/00/0000"; // happens with any
maskedTextBox1.TextMaskFormat = MaskFormat.IncludeLiterals;
}
Code for main Form:
public partial class Form1 : Form
{
private ModalForm modalForm = new ModalForm();
private void button1_Click(object sender, EventArgs e)
{
modalForm.ShowDialog();
}
}
The control's prompt appears again when its content changes, but at first view isn't present.
Setting the TextMaskFormat property to IncludePromptAndLiterals could be a solution, but then, .Text has to be cleaned up.
Is there another way to handle this?. Has become necessary for me, that all MaskedTextBox controls must always show its default prompt.
Try this on Form's Shown event,
private void ModalForm_Shown(object sender, EventArgs e){
if (!maskedTextBox1.MaskCompleted) // if there is missing parts it will return false, every false means prompts need in control
{
string tempText = maskedTextBox1.MaskedTextProvider.ToDisplayString(); // get the last value with prompts
maskedTextBox1.Text = "";
maskedTextBox1.Text = tempText; // then set the last value.
}
}
Hope helps,
Similar to Notepad's method 'Find' where you can find a certain word and the main form will highlight the word while keeping the Find dialog box focused.
Here's where I'm at so far:
[Main.cs] (Windows Form)
private new Find FindForm;
private delegate void FindNextCallback(int s, int l);
private cmdFind_Click(object sender, EventArgs e)
{
FindForm = new Find(txtInput.Text);
FindForm.FindNext += new FindEventHandler(FoundNext);
FindForm.Show();
}
private void FoundNext(object sender, FindEventArgs e)
{
Invoke(new FindNextCallback(SelectFoundText), new object[] { e.Start, e.Length });
}
private void SelectFoundText(int s, int l) { txtInput.Select(s, l); }
[Find.cs] (Windows Form)
private static FindEventArgs FindArgs;
public event FindEventHandler FindNext;
private void cmdFindNext_Click(object sender, EventArgs e)
{
FindArgs = new FindEventArgs(startingPosition, selectionLength);
FindNext?.Invoke(null, FindArgs);
}
My problem is that whenever the user clicks "Find Next" the Main form doesn't do anything until I actually close the Find form or manually Main.Focus().
Also, fairly new with creating my own events so any tips on proper ways, cleaner ways, shorter ways to write my code would be most appreciated.
The TextBox in parent form is updating but it doesn't show its selected text as highlighted, You can set HideSelection property of text box to false to make it show selected text as highlighted always, even when the control doesn't have focus.
You can use this property to keep text highlighted in a text box
control while another form or a dialog box has focus, such as a
spelling checker dialog box.
I was wondering how I would make an image appear inside a messagebox that I set up so that whenever the mouse enters a label, it displays the messagebox. What would the code be for the image insertion?
Quick and dirty way to achieve this is to create another windows form that will have same buttons as message box but that will also have an image.
Create public Boolean property in this form that will be named something like OKButtonClicked that will tell you whether OK or Cancel was clicked
Set ControlBox property to False so that minimize, maximize and close buttons are not shown
Here is a code behind for this form
public partial class MazeForm : Form
{
public MazeForm()
{
InitializeComponent();
}
private bool okButton = false;
public bool OKButtonClicked
{
get { return okButton; }
}
private void btnOK_Click(object sender, EventArgs e)
{
okButton = true;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
okButton = false;
this.Close();
}
}
Finally in your main form you can do something like this
MazeForm m = new MazeForm();
m.ShowDialog();
bool okButtonClicked = m.OKButtonClicked;
Note that this is something I quickly created in 15 min and that it probably needs more work but it will get you in the right direction.
I am building a WinForms program that connects to a DB. On one form I want to display a list of elements recovered from the DB. The elements have to be clickable (radio buttons are an option here), and must have a hover option, as I want some info to appear in a textbox when mouse is hovered over a specific item.
I cannot find an adequate ToolBox control for this. Has anyone got some suggestions? I am using VS2010.
Thanks.
There is no such ready-to-use control in .net framework instead you have to design/create your own using Window custom controls.
Using a standard ListBox, you can just track the mouse position with the MouseMove event.
Example:
int _HoverIndex = -1;
private void listBox1_MouseMove(object sender, MouseEventArgs e) {
int index = listBox1.IndexFromPoint(e.Location);
if (index != _HoverIndex) {
_HoverIndex = index;
if (_HoverIndex == -1)
textBox1.Text = string.Empty;
else
textBox1.Text = listBox1.Items[_HoverIndex].ToString();
}
}
private void listBox1_MouseLeave(object sender, EventArgs e) {
_HoverIndex = -1;
textBox1.Text = string.Empty;
}