Custom caret in datagridview - c#

I am working on c# datagridview project where I have to change the default caret to dos like caret. I have search the internet buy unable to find any solution . Any suggestions from the respective members.

To change the cursor using the Designer:
1: Go to the designer and right-click your control.
2: In the right-click-menu go to Properties
3: In properties scroll down to Cursor and select the cursor you want.
Or to change the cursor to a custom cursor:
The function to change the cursor:
public static Cursor ActuallyLoadCursor(String path)
{
return new Cursor(LoadCursorFromFile(path));
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr LoadCursorFromFile(string fileName);
code from: Custom cursor in C# Winforms
Call the function:
yourdatagrid.Cursor = ActuallyLoadCursor("PathToYourCursor.cur");
Also take a look at this folder to find a cursor:
C:\Windows\Cursors

Related

How to remove black outlines on buttons after clicking on form in windows forms

i am making a flat GUI in windows forms.. i know i cant just ignore and leave tab indexing and i have included it. when i am using tab in my program to select control,a black outline appears on controls(as in this pic). what i want is to remove this outlining when i mouse-click anywhere because it seems Ugly if i am not using my keyboard.
how can i remove this outline on flat button
can you suggest me how can i achieve this target?
Try hiding the focus cue by creating your own button:
public class ButtonEx : Button {
protected override bool ShowFocusCues {
get {
return false;
}
}
}

How to Disable Flashing Text Line?

I have a textbox control and I've simulated it to be like a label by turning the readonly and border property to true and none respectively so it can highlighted. Everything works fine but when I click on the textbox, it shows the flashing text symbol (it's a flashing l that indicating that you can type here, don't know what's it called).
If I turn enabled property to false, it disappears but it can't be highlighted and the text is grayed out.
Is there a way to fix this? Like another type of control?
The little "flashing text line" is called a caret. It is like a cursor (which is the thing you move with the mouse), except that it appears in text.
And what you describe in the question is normal behavior. You see the exact same thing in the Windows shell. Right-click on any icon in any location (e.g., the desktop), and open its "Properties" window. Then click on any one of the property labels (e.g., the path under "Location"). You'll see the same thing.
There's a caret there, flashing just beyond the letter "i", and you can see even see that the selection is highlighted. This is by design, and the selection highlighting is an important clue.
The reason why you would use a read-only textbox instead of a label is to allow the user to select text out of the control. If you don't want to allow this, then just use a label.
If you absolutely must use a read-only textbox and still want to hide the caret, then you can p/invoke the HideCaret function. By calling this function as soon as your textbox control gets the focus, it won't ever show the caret. However, every call to HideCaret must be paired with a call to ShowCaret, so you'll need to call ShowCaret when your textbox control loses the focus.
You could wire up handlers to the GotFocus and LostFocus events for your particular textbox control and place the code in there. But it is cleaner to do this in a derived control class, especially if you want to have multiple textboxes that behave the same way. Something like this (code untested):
internal class NativeMethods
{
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShowCaret(IntPtr hwnd);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool HideCaret(IntPtr hwnd);
}
public class ReadOnlyTextBox : System.Windows.Forms.TextBox
{
public ReadOnlyTextBox()
{
this.ReadOnly = true;
}
protected override void OnGotFocus(EventArgs e)
{
NativeMethods.HideCaret(this.Handle);
}
protected override void OnLostFocus(EventArgs e)
{
NativeMethods.ShowCaret(this.Handle);
}
}
Text symbol will flash i don't know weather you can disable flashing better you can use label i think that is good check the visibility property of text box may be it will be false that's why you cant able to see.
through code also you can enable visibility
textboxname.visible= true;
tweak this css properity for your project.
input[disabled] {
background-color: #b5f0fa;
border: 1px solid;
color: Black;
font-weight: bold;
}

TextBox with vertical scrollbar on the left side

I'm developing a Windows Forms application in C#, which has a multiline TextBox control on a form.
Due to specific (irrelevant) reasons, this TextBox needs a vertical scrollbar on the left side of the TextBox control. I've off course searched for a solution, but I couldn't find any... so my questions are:
1) Is there a way to make the automatic vertical scrollbar of a TextBox control (or a usercontrol derived from TextBox or TextBoxBase) appear on the left instead of the right? This is the preferred method, since all scolling is then still handled by the control. Since chancing the RightToLeft property for such a TextBox actually moves the scrollbar to the left, I feel there must be a hack to be exploited here.
or
2) Is there a message that I can intercept with my IMessageFilter implementation when the TextBox is scrolled, even though it doesn't have scrollbars? I.e. a user can scroll using the arrow keys and the textbox will move lines up and down, but I can't find any messages fired when that occurs.
Maybe another idea of how to accomplish this?
Edit to add: The text needs to be aligned to the right horizontally! Otherwise I would have solved it already.
New edit as of 11/03/2014: Okay, after BenVlodgi's comment I started having doubts about my own sanity. So I created a test project and now I remember why setting RightToLeft to Yes was not working.
The image below shows a regular TextBox on the left with that setting. The scrollbar is on the left and the text on the right, but the text is not shown properly. The period at the end of the sentence is moved in front of the sentence.
The second TextBox control is the one suggested in LarsTech's answer, which functions correctly and does not move any punctuation.
Therefore, I accept and reward the bounty to LarsTech's answer.
I took some of the example code from Rachel Gallen's link and made this version of the TextBox:
public class TextBoxWithScrollLeft : TextBox {
private const int GWL_EXSTYLE = -20;
private const int WS_EX_LEFTSCROLLBAR = 16384;
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
int style = GetWindowLong(Handle, GWL_EXSTYLE);
style = style | WS_EX_LEFTSCROLLBAR;
SetWindowLong(Handle, GWL_EXSTYLE, style);
}
}
I've never done it before, but the results seem to have worked:
By setting the RightToLeft property true. But it said the content would also be from right to left, so I don't know if that would solve your problem...But that's a way to set the scrollbar on the left hand side.
http://bytes.com/topic/c-sharp/answers/255138-scrollbar-position

Maintain TextBox scroll position while adding line

In my WinForm application I have a multiline TextBox control (uiResults) which is used for reporting progress while processing a large number of items. Using AppendText works great for automatically scrolling to the bottom at every update, but if the user scrolls back to read some older data I need to turn off the autoscroll. I would rather stay away from P/Invoke calls if possible.
Is it possible to detect if the user has scrolled back without using P/Invoke? For now, I just check SelectionStart which works but requires the user to move the caret from the end of the textbox to stop the autoscroll:
if(uiResults.SelectionStart == uiResults.Text.Length)
{
uiResults.AppendText(result + Environment.NewLine);
}
My main problem is that when appending a string using the Text property, the textbox is scrolled to the beginning. I tried to solve this by storing the caret position and resetting and scrolling to it after the update, but this causes the current line to move to the bottom (of course, since ScrollToCaret scrolls no more than the necessary distance to bring the caret into view).
[Continued from above]
else
{
int pos = uiResults.SelectionStart;
int len = uiResults.SelectionLength;
uiResults.Text += result + Environment.NewLine;
uiResults.SelectionStart = pos;
uiResults.SelectionLength = len;
uiResults.ScrollToCaret();
}
Auto-scrolling text box uses more memory than expected
The code in the question implements exactly what you are looking for. Text is added, but scrolling only occurs if the scroll bar is at the very bottom.
I have had the same problem.
And finally, I made an easy way.
(Sorry, I'm not good at English.)
key point is get the first displayed char index using GetCharIndexFromPosition method.
//Get current infomation
int selStart = textBox.SelectionStart;
int selLen = textBox.SelectionLength;
int firstDispIndex = textBox.GetCharIndexFromPosition(new Point(3, 3));
//Append Text
textBox.AppendText(...);
//Scroll to original displayed position
textBox.Select(firstDispIndex, 0);
text.ScrolltoCaret();
//Restore original Selection
textBox.Select(selStart, selLen);
And, if textbox is flicking, use this extention.
Call textBox.Suspend() before adding text, and call textBox.Resume() after adding text.
namespace System.Windows.Forms
{
public static class ControlExtensions
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool LockWindowUpdate(IntPtr hWndLock);
public static void Suspend(this Control control)
{
LockWindowUpdate(control.Handle);
}
public static void Resume(this Control control)
{
LockWindowUpdate(IntPtr.Zero);
}
}
}
Hope this will help you.
Thank you~
Are you open to another approach, because you are bound to get into trouble this way and the solutions will get complex (pinvoke etc. that you want to avoid). For eg. suppose you find a way to "detect if the user has scrolled back" and you stop scrolling to bottom. But after reading the line user might want the scroll to bottom feature to resume. So why not give user a way to control Auto-Scrolling. Here's how I would do it...
Use a RichTextBox to show data and a Checkbox to control AutoScrolling, then your code might be something like this:
richTextBox1.AppendText(result + Environment.NewLine);
if (checkBoxAutoScroll.Checked)
{
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.ScrollToCaret();
}
RichTextBox by default will not automatically scroll to bottom on AppendText, so the first line would always be visible (and not the newly appended line). But if user checks this checkbox called AutoScroll, our code will then scroll the richtextbox to the bottom where new lines are shown. If user wants to manually scroll to read a line he will first need to uncheck the checkbox.

How to paste text using windows paste command to other application in c#?

How calling interop to paste text using windows pastse command to other application in c#?
calling interop?
i mean how to programing c# same right click to past text
This can be a bit tricky in some scenarios, but it's actually quite simple and easy to do. Below is an example on how to get some text using a text box, (called uxData in this case), open Notepad from code, and to paste the text from the clipboard to Notepad.
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
[DllImport("user32.dll", SetLastError = true)]
private static extern bool BringWindowToTop(IntPtr hWnd);
private void OnClicked_PasteToNotepad(object sender, EventArgs e) {
// Let's start Notepad
Process process = new Process();
process.StartInfo.FileName = "C:\\Windows\\Notepad.exe";
process.Start();
// Give the process some time to startup
Thread.Sleep(10000);
// Copy the text in the datafield to Clipboard
Clipboard.SetText(uxData.Text, TextDataFormat.Text);
// Get the Notepad Handle
IntPtr hWnd = process.Handle;
// Activate the Notepad Window
BringWindowToTop(hWnd);
// Use SendKeys to Paste
SendKeys.Send("^V");
}
}
Now, say you want to paste into a specific field. This is where you will need to use FindWindow and FindWindowEx, to get the handle of the field you want to paste into. Here are the steps once you have copied your data to the clipboard.
Get the process handle
Bring the process into focus (Activate it)
Find the handle of the field you wish to paste into
Set focus to that field
Use SendKeys to paste from clipboard
You can use Clipboard class in System.Windows.Forms to inspect types of data the clipboard contains and fetch this data if needed. Clipboard in Windows holds the data "to be pasted", which can be a bitmap, text, HTML, RTF etc.
It's not quite clear what you mean by "paste". Is that "paste" supposed to happen when a button is clicked, a key is pressed or something else? Text box controls (richbox, combobox etc.) typically respond to Ctrl-V (standard Paste keystroke) and will auto-insert the text in the appropriate format (plain, RTF) from the clipboard, so you don't have to do anything manually.
In all other cases you need to decide what data you're interested in and extract it from the clipboard using appropriate methods.
This is how to obtain data from the windows clipboard using P/Invoke. That's about all I can give you without more information on what you want to paste it to.
[DllImport("ole32.dll")]
static extern int OleGetClipboard([MarshalAs(UnmanagedType.IUnknown)]out object ppDataObj);
string text = ppDataObj as string;
//paste it in your application somewhere
Get the text from right click paste
using System.Windows.Forms;
public static string GetControlV()
{
Textbox i = new Textbox();
i.Paste();
return i.Text;
}
Declare a new Textbox object
Call it's Paste() method - Paste() is built in to the Textbox component in windows form and places the text copied from right click copy into the textbox.
Return the Text property of the Textbox.
You'll need to add a reference to System.Windows.Forms.dll. Just in case...
Right click in the Solution Explore "References".
Click "Add Reference".
Under the ".NET" tab you should be able to find the dll.
Then the using statment should work.

Categories

Resources