|
How to i get the cursor position. I mean the x and y coordinates. I am desparate. Some one help. to be precise in a richtextbox control.
Nana
|
|
|
|
|
richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart)
|
|
|
|
|
I think he is wanting to get the position that the mouse is currently in. One way I can think to do it is to keep the current mouse position in memory using the MouseMove event. Set up a MouseMove event on the control and then use the event args that are passed to the delegate to store the mouse position in memory. Then, when you are wanting to retrieve the mouse position in another event (such as onClick) simply retrieve the positions you had already stored.
|
|
|
|
|
Am so sorry to border you but its not working. am so stressed right now. I even applied the PointToClient. Am just the getting the right points. Like where the current cursor is pointing. thx
Nana
|
|
|
|
|
the code below should work for you:
<br />
Point p = this.textBox1.PointToClient(Cursor.Position);<br />
int x = p.X;<br />
int y = p.Y;<br />
Except of course use your control instead of textBox1.
|
|
|
|
|
ooppss. Sorry i think I was asking the wrong question.
The Point am looking for is the selected point at which to insert text. The x and y cordinates of the current position selected in the textbox.
thx
Nana
|
|
|
|
|
So you're not wanting X-Y coords. You're wanting to insert text at the current cursor position. In this case you would simply use the property:
<br />
this.textBox1.SelectionStart<br />
|
|
|
|
|
To be even more specific you should use this to insert text where the cursor exists:
<br />
this.textBox1.Text = this.textBox1.Text.Insert(this.textBox1.SelectionStart,"test");<br />
|
|
|
|
|
thanks so much for ur continous help. Yes the above code worked fine for the x coordinate. I need the y too, because, i will be using these points as the location for a Listview control i will be creating and adding to the control at run time.
Nana
|
|
|
|
|
You've managed to totally confuse me Can you explain from start to finish what exactly you are trying to do? X and Y coords do not come along with a "selection".
|
|
|
|
|
I have a rich textbox with all the basic funtionalities implemented. BOLD, ITALIC, UNDERLINE, FONT, ect. However, I want the users to be able to insert a table into the textbox if they do select table from the context menu. Now, I couldn't find any means to do that. Infact, what I tried doing was copying an HTML table code onto the clipboard and then pasting it(that didn't work for me). Now am trying to use a glacial list control instead of a table. And my plan is to create the glacial contol when the user selects table on the context menu. This would appear as if it is part of the document and that can serve as the table. Now I need to be able to know the y and x coordinates of the point selected so i can draw the glacial list control with those points as the starting point. and that is why i need the x and y and not just the x. however, if you do have any idea as it how i can insert the table instead of going through this problem. That will be be great. thanks a lot.
Nana
|
|
|
|
|
I think perhaps I can help you if I can see the code. Can you IM me on MSN = xraheemx@hotmail.com or AIM = XxRaheemxX?
|
|
|
|
|
Just so everybody else sees the resolution in case they are trying the same. You were trying to get the mouse position of a click point within a control, and then create a new control over the top of that control on the form. To do this I recommended the following that seemed to work:
global declarations of MouseCoords:
private int Control1MouseX;
private int Control1MouseY;
Form is called Form1
Main control is called Control1
New Control is call NewControl1
in the MouseDown event of Control1 do the following:
<code>
Point p = this.Form1.PointToClient(this.Control1.PointToScreen(new Point(e.X,e.Y)));
this.Control1MouseX = p.X;
this.Control1MouseY = p.Y;
</code>
then when creating and placing the new Control use this:
<code>
Control NewControl1 = new Control();
NewControl1.Location = new Location(this.Control1MouseX,this.Control1MouseY);
Form.Controls.Add(NewControl1);
</code>
|
|
|
|
|
Cursor.Position
You might also want to use Control.PointToClient to translate the screen coordinates to control coordinates.
|
|
|
|
|
Ahh yes, that makes way more sense and also doesn't use quite the processing and memory that storing the coords would. I must have had a brain fart when I came up with that
|
|
|
|
|
Im sorry for asking this as it is probably already been covered, but i cant seem to put my queary into a search phrase that returns any thing useful to me.
Here is my custom control related problem.
Just to get my head round making custom controls im mocking up a simple windows like menu bar and start menu within my program.
myMenu1: This is a custom control made up of 2 image boxes. 1 is the "start" button the other is the actual bar. When the user places the mouse in the transparent hot region (basicly the blank background of the menu item while the actual images reside offscreen) the menu bar images slide in from right to left, while the mouse hovers over the start button it changes color.
myMenu2 This is a the actual menu that will pop up when the start button is pressed. it is made up of MenuItem custom control items....
MenuItem
Just a label and image box that expose thier image, text and font properties.
What i want to do is have an event in a menu item be handled by the main form directly rather than the custom control.
At the moment the main form can only tell if im clicking on myMenu1 not wether im clicking on the start button or bar.
It can also only tell if im clicking on myMenu2 not which MenuItem.
What im angling for is to be able to click on myMenu1 start button and be able to use that event to set the visable and locaiton property of myMenu2 and be able to click on menu items in myMenu2 and have them function like indervidual buttons on my main form so they can fire of methods, etc.
Again sorry if this has been covered, I am rusty on Events and Delegates and it just seems like a matter of makingthe Click events public... but im struggling getting my head round it
Cheers.
|
|
|
|
|
Create a custom event delegate and event inside the control that your main form can use. When the in-control button is clicked have it check the custom event for registrars and fire it off it they exits. Examples: (I wrote this in the web browser, not a compiler so there may be a few compiler mistakes)
public delegate void OnClickStart(object,SystemEventArgs);
public event OnClickStart ClickStart;
private void button1_Clicked(object sender, SystemEventArgs e)
{
if(this.ClickStart != null)
{
this.ClickStart(sender,e);
}
}
Now in your main form use the code like such:
CustomControl cc = new CustomControl;
cc.ClickStart += new cc.OnClickStart(Mainform_ClickStart);
public void Mainform_ClickStart(object sender, SystemEventArgs e)
{
}
Try this out and let me know if you have any other questions.
-- modified at 11:41 Friday 7th October, 2005
|
|
|
|
|
That is awesome! thank you SO much!
Why cant my books explain that as simply as you have.
Thanks again for opening my eyes.
|
|
|
|
|
No problem, feel free to email me as well if you have any other c# questions
|
|
|
|
|
I'm not a dot net guru, but I had a similar issue and will share.
I created a control that creates a SQL statement based upon user selections. I needed an event that would fire when the SQL was generated. I first declared the following:
public delegate void SQLCreatedHandler();
[Category("Action")]
[Description("Fires when the SQL is created")]
public event SQLCreatedHandler SQLCreated;
When I wanted to fire the event I called this code:
//Raise event notifying subscribers that the SQL has been created
OnSQLCreated();
And the OnSQLCreated() method looked like this:
// If an event has no subscribers registerd, it will
// evaluate to null. The test checks that the value is not
// null, ensuring that there are subsribers before
// calling the event itself.
if (SQLCreated != null)
{
SQLCreated(); // Notify Subscribers
}
From the hosting application I created a handler for the event:
this.queryControl1.SQLCreated += new QueryControl.QueryControl.SQLCreatedHandler(this.queryControl1_SQLCreated);
And the method for the handler:
private void queryControl1_SQLCreated()
{
MessageBox.Show(this.queryControl1.SQL);
}
Hope this helps, don't mind the codeing. I found the articles here on CP that helped me create the above code. Search the articles, not messageboards
www.lovethosetrains.com
|
|
|
|
|
I'm having a bit of a problem getting this to work. I am writing an application that is using a StreamReader to read the output of a shell process. Here is the code to launch the process:
Process p = new Process();<br />
p.StartInfo.Arguments = "-n 1 -w 200 " + ip;<br />
p.StartInfo.FileName = "ping.exe";<br />
p.StartInfo.RedirectStandardOutput = true;<br />
p.StartInfo.UseShellExecute = false;<br />
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;<br />
p.Start();
The problem I'm having is that ProcessWindowStyle.Hidden seems to do nothing when UseShellExecute is = false. A (blank) console window still comes up for every IP address I'm trying to ping. Everything else works fine except the hiding of the window. I haven't been able to find anybody else that seems to be having this same issue out there. Any clues?
|
|
|
|
|
I used:
p.CreateNoWindow = true;
www.lovethosetrains.com
|
|
|
|
|
Excellent.. I feel so stupid now. Thanks for your response, worked like a charm.
|
|
|
|
|
Just glad that I can actually be of help.
www.lovethosetrains.com
|
|
|
|
|
I have a DLL written in C and am trying to access it with C#. I had to use DLLImport commands to talk with it and am having a problem. I get "Attemped to read or write protected memory." Now longs in C are 32 bits and longs in C# are 64. Could this be the problem and would just using a Convert.ToInt32(variable) fix it. I tried this when I made the call to the DLL and its still giving me the same error. Even when I declare my variable that I am sending to the DLL function as an int it doesnt work.
-Cal
-- modified at 9:30 Friday 7th October, 2005
|
|
|
|