Solved

Drag and Drop Formatting

Posted on 2008-06-20
8
285 Views
Last Modified: 2013-12-16
I have a richtextbox that I just recently added Drag-and-drop capabilities to, but I am having a problem with formatting.  If the rtb contains all regular text, it works fine, but if it contains different fonts, all the text in the richtextbox changes to one font.  How can I code my drag and drop abilities and retain my text formatting????  My code is below.
private void richTextBox1_DragDrop(object sender, DragEventArgs e)
{
	listBox1.Items.Add("rtb_dragenterdrop");
 
	int start = richTextBox1.SelectionStart;
 
	if(e.Data.GetDataPresent(DataFormats.Text))
	{
		if(ddelocal == DragDropEffects.Move)
		{
			if(localindex >= 0)
			{
				string tmp = e.Data.GetData(DataFormats.Text).ToString();
				richTextBox1.Text = richTextBox1.Text.Remove(localindex,tmp.Length);
				if(start > localindex)
					richTextBox1.Text = richTextBox1.Text.Insert(start-tmp.Length,e.Data.GetData(DataFormats.Text).ToString());
						else
					richTextBox1.Text = richTextBox1.Text.Insert(start,e.Data.GetData(DataFormats.Text).ToString());
			}
			else
				richTextBox1.Text = richTextBox1.Text.Insert(start,e.Data.GetData(DataFormats.Text).ToString());
		}
		else if(ddelocal == DragDropEffects.Copy)
		{
			richTextBox1.Text = richTextBox1.Text.Insert(start,e.Data.GetData(DataFormats.Text).ToString());
		}
		else if(ddelocal == DragDropEffects.All)
		{
			richTextBox1.Text = richTextBox1.Text.Insert(start,e.Data.GetData(DataFormats.Text).ToString());
		}				
	}
	else if(e.Data.GetDataPresent(DataFormats.FileDrop))
	{
		string[] s  = (string[])e.Data.GetData(DataFormats.FileDrop);
		foreach(string str in s)
		{
			MoveFile(str, start);
		}
	}
	listBox1.Items.Add("rtb_dragexitdrop");
			
}

Open in new window

0
Comment
Question by:gvector1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 7

Expert Comment

by:AUmidh
ID: 21836609
I don't have enough idea but on searching of DataFormts Member i found there is a member "rtf" use that instead of "text"
change all your this lines e.Data.GetData(DataFormats.Text to e.Data.GetData(DataFormats.Rtf

May be this solve your problems.
0
 
LVL 7

Expert Comment

by:AUmidh
ID: 21836616
and one other thing don't assign it to the text property of your rich text box. like you did
>> richTextBox1.Text = ...............

assign it as

richTextBox1.Rtf=..............

Regards
0
 

Author Comment

by:gvector1
ID: 21846606
How can I coexist rich text and insertions points.  The code above shows how I am using this stuff.  Based on text location, I am removing and inserting text in the richtextbox.  If I am using richtext, my insertion points and selection start points will not be correct will they?????
0
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 

Author Comment

by:gvector1
ID: 21869518
Any ideas???????
0
 
LVL 7

Expert Comment

by:AUmidh
ID: 21872762
Sorry I was Away from work so that's why i did not answer you.


as i understand your problem! You want to insert formated text(Text from drag operation) before or after your selected text in your rich text box.

if yes then follow the following steps.



break your rich text box into two portions like one selected text and second un selected text.

save your mid point which break this two portion like your start variable.

and apply the following algo.



richtextbox.select(0,start);

string rtf1=richtextbox.selectedRtf;

richtextbox.select(start,endIndex);

string rtf2=richtextbox.selectedRtf;

string tRtf=rtf1+DragDropRtf+rtf2;

richtextbox.Rtf=tRtf;



Where endIndex is the index of end character in your rich text box.

and dragDropRtf is the rtf of that data which you are droping on text box.

hope this solve your problems.
0
 

Author Comment

by:gvector1
ID: 21944830
I will give that a try this afternoon and get back.

Thanks
0
 

Author Comment

by:gvector1
ID: 21946476
I tried that and I am having some issues.

1st:  When I say "string rtf1 = richtextbox.selectedRtf;", I get the selectedrtf but it also has the richtext formatting code that is found at the beginning of a rich text document and the '\0' at the end. So when I append the sections of selected text and load it in a richtextbox, only the first selection is visible.

2nd: I am having issues on removing richtext from a richtextbox and maintaining the formatting.

Any recommendations on this issue????
0
 

Accepted Solution

by:
gvector1 earned 0 total points
ID: 21983035
I found out how to accomplish this.

I first select the text I am moving, Store the .selectedtext and .selectedrtf. Then I set the .selectedtext = "".  I then insert  into the richtextbox  by setting .selectedrtf to the stored rtf value.  

Thanks for the attempts.
Kendal
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question