I am trying to get a clickable file path using asp.net c# visual studios web form, meaning to say that it is like windows file explorer, allowing the person to navigate through the different levels of folders etc, can anyone provide any links to help get me started? [1]: https://i.stack.imgur.com/WyyLq.png
You could try to get the path string and divide it in multiple pieces. Then store them in multiple textboxes, labels, buttons or whatever you want. My form looks like this: Picture Form
Secondly, you will have to update those (in my case) textboxes to save the path. See my code and decide what you want to use, and if you have to modify it.
private void changePath()
{
String path = webBrowser1.Url.AbsolutePath;
if (path.Contains(#"/")) { path = path.Replace(#"/", #"\"); }
string[] directories = path.Split(Path.DirectorySeparatorChar);
int count = directories.Count();
if (count <= 6)
{
textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = ""; textBox6.Text = "";
for (int i = 0; i < count; i++)
{
String txt = "textBox" + (i + 1);
TextBox tbx = this.Controls.Find(txt, true).FirstOrDefault() as TextBox;
tbx.Text = directories[i];
}
}
else
{
int p = count / 6;
int z = count - (p * 6);
for (int i = 0; i < count; i++)
{
int g = i - 1;
String txt = "textBox" + (i + 1);
TextBox tbx = this.Controls.Find(txt, true).FirstOrDefault() as TextBox;
tbx.Text = directories[z];
z++;
if (i == 5)
{
break;
}
}
}
}
Second step is to make click functions on the (in my case) textboxes. Here is one sample of how you can do this. See for yourself what you can do.
private void textBox5_Click(object sender, EventArgs e)
{
if(!textBox5.Text.Equals(String.Empty))
{
String p = webBrowser1.Url.AbsolutePath;
if(!textBox6.Text.Equals(String.Empty))
{
webBrowser1.Url = new Uri(p.Replace(#"/" + textBox6.Text, ""));
}
}
}
This code will remove the last piece, leaving you with a new path. Example:
Before: C:\Users\USERNAME\Desktop\C#
After:
After: C:\Users\USERNAME\Desktop
Again, you have to look what works for you. There are multiple ways to resolve your issue.
Good Luck!
Twan.
Related
I'm a beginner in c# and first time need your help, because I can't find any solution to this problem.
I make a typewriting tutorial, I have a richtextbox filled with randomly generated characters, spaces and 3 new line.
The user must type the characters in a textbox, and if he typed the adequate character the character turn to green in richtextbox and he can type the the next one. If he makes a mistake, he cant move to next character until the the right button was pressed.
My problem is everything works, except new line part.
When the program compare the 2 new line accept the new line but just when ctrl+enter was pressed, simple enter wont work, the program somehow thinks enter is a wrong character.
I need to make this user friendly and a simple enter needed for this.
I tried this so far:
Make textbox MultiLine makes no difference.
Changed Acceptsreturn to true makes no difference.
Changed in string that will be the text of richtextbox the Environment.Newline to \r and \n and \r\n and its makes no difference.
Changed the textbox to Richtextbox makes no difference.
I tried every possible combination above and I cant work out what is the problem.
Here it is the random character generating part:
public partial class Form1 : Form
{
char[] karakterek = { 'a','á','b','c','d','e','é','f','g','h','i','í','j','k','l','m','n','o','ó','ö','ő','p','q','r','s','t','u','ú','ü','ű','v','x','y','z' };
char[] nemkarakterek = {'0','1','2','3','4','5','6','7','8','9','"',',','+','%','/','=',':','-',};
char egykarakter;
string teljes = "";
int mutato = 0;
Random r = new Random();
public Form1()
{
InitializeComponent();
karakterfeltolt();
}
private void karakterfeltolt()
{
int hanyszor = 0;
do
{
string egesz = "";
if (hanyszor < 3)
{
do
{
if (egesz.Length < 150)
{
if (r.Next(1,15)<14)
{
egykarakter = karakterek[r.Next(0, karakterek.Length)];
egesz = egesz + egykarakter;
}
else
{
egykarakter = nemkarakterek[r.Next(0, nemkarakterek.Length)];
egesz = egesz + egykarakter;
}
}
} while (egesz.Length < 150);
if (egesz.Length > 150)
{
egesz = egesz.Substring(0, 150);
}
int vanespace = 0;
egesz = egesz.Insert(r.Next(2, 10), " ");
for (int i = 0; i < egesz.Length - 10; i++)
{
if (egesz[i] == ' ')
{
vanespace = i;
int eselynovelo = r.Next(0, 10);
if (eselynovelo > 6)
{
egesz = egesz.Insert(r.Next(vanespace + 3, vanespace + 10), " ");
}
else
{
egesz = egesz.Insert(r.Next(vanespace + 6, vanespace + 10), " ");
}
}
}
string nagybetus = egesz.Substring(0, 1).ToUpper() + egesz.Substring(1);
nagybetus = nagybetus.Insert(nagybetus.Length, ".\r\n");
teljes = teljes + nagybetus;
hanyszor++;
}
} while (hanyszor < 3);
richTextBox1.Text = teljes;
}
And here is the textbox compare part:
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == richTextBox1.Text[mutato])
{
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = mutato + 1;
richTextBox1.SelectionColor = Color.Green;
mutato++;
label2.Text = "OK";
}
else
{
label2.Text = "Wrong";
e.Handled = true;
}
}
What else can I do? Please help me.
I am looking a way to create a HyperLink in a RichTextBox pointing to a line of the text of the same RichTextBox.
I just found how to do that with Internet Links but I don't find a way to do it with the same text inside of the control (It's like Hyperlinks in MS Word pointing to a header or bookmark).
Thanks in Advance. - CCB
No, this will not work unless you code the necessary stuff yourself.
Two suggestions:
A simple workaround with links always starting with www.
The nicer solution with arbitrary link text
Let's have a look at both options..:
Using the built-in functionality of recognizing an URL seems the right way to start, but the link will always have to look like a URL, not like a hyperlink to an anchor.. If you can live with a solution that has, say, links like this: www.goto.234 and anchors like this: #234# this is really rather simple..
A working example can be as simple as this:
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
var s = e.LinkText.Split('.');
string anchor = s[2];
int a = richTextBox1.Text.IndexOf("#" + anchor + "#" );
if (a >= 0) richTextBox1.SelectionStart = a; else return; // do add more checks!
richTextBox1.SelectionLength = 0;
Text = anchor + " # " + a;
//richTextBox1.ScrollToCaret(); <<--- this crashes on my machine!
// so I take the jump out of the click event and it works:
Timer ttt = new Timer() { Interval = 100 };
ttt.Tick += (ss, ee) => { richTextBox1.ScrollToCaret(); ttt.Stop(); };
}
Option two: If you'd rather have more choice of how the links should read you can do this:
Start by formatting each to
Start with a special character, say a tilde '~'
format it to look blue and underlined if you want to
Either make it one word or replace space by underlines and format those to have the forecolor equal to the backcolor
Now this can do the job:
public string delimiters = " ()[]{}!&?=/\\,;.\r\n";
private void richTextBox2_Click(object sender, EventArgs e)
{
int sstart = -1;
string s = getWordAt(richTextBox2.Text,
richTextBox2.SelectionStart, delimiters, out sstart);
if (s.Length < 3) return;
string char1 = s.Substring(0, 1);
if (char1 == "~")
{
int p = richTextBox2.Text.IndexOf("#" + s.Substring(1));
if (p >= 0) { richTextBox2.SelectionStart = p; richTextBox2.ScrollToCaret(); }
}
}
public static string getWordAt(string text, int cursorPos,
string delimiters, out int selStart)
{
int startPos = 0;
selStart = startPos;
if ((cursorPos < 0) | (cursorPos > text.Length) | (text.Length == 0)) return "";
if ((text.Length > cursorPos) & (delimiters.Contains(text[cursorPos]))) return "";
int endPos = text.Length - 1;
if (cursorPos == text.Length) endPos = text.Length - 1;
else { for (int i = cursorPos; i < text.Length; i++)
{ if (delimiters.Contains(text[i])) { endPos = i - 1; break; } } }
if (cursorPos == 0) startPos = 0;
else { for (int i = cursorPos; i > 0; i--)
{ if (delimiters.Contains(text[i])) { startPos = i + 1; break; } } }
selStart = startPos;
return text.Substring(startPos, endPos - startPos + 1);
}
Here are the two versions side by side, once at the top then after clicking on a link:
Both versions work fine, although both could do with some more checks.
Note that I was too lazy to format the pseudo-links in the second example, so they show their tildes and hashes..
Not hard to write a helper function that can insert the formatting; the search will still work as it searches in the Text, not the Rtf property..
I'm in a bit of a pickle at the moment, I've created a bit of code that creates 4 textboxes and adds them to a table layout at run time (code below) but I'm struggling with getting text from it, I tried getting the value from it as you would string s = TxtBox1.Text.ToString(); but it just gets a null reference, then I tried txt.Text.ToString();and this just gets the text from the last text box that was created.
private void button2_Click(object sender, EventArgs e)
{
int counter;
for (counter = 1; counter <= 4; counter++)
{
// Output counter every fifth iteration
if (counter % 1 == 0)
{
AddNewTextBox();
}
}
}
public void AddNewTextBox()
{
txt = new TextBox();
tableLayoutPanel1.Controls.Add(txt);
txt.Name = "TxtBox" + this.cLeft.ToString();
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
}
I've looked all over for the answers to this and as of yet found nothing if anybody has any ideas I would be grateful.
Thanks
this code picks textbox1 from tableLayoutPanel1, cast it from Control to TextBox and takes Text property:
string s = ((TextBox)tableLayoutPanel1.Controls["TxtBox1"]).Text;
if you need them all, then iterate over textboxes:
string[] t = new string[4];
for(int i=0; i<4; i++)
t[i] = ((TextBox)tableLayoutPanel1.Controls["TxtBox"+(i+1).ToString()]).Text;
You can try
var asTexts = tableLayoutPanel1.Controls
.OfType<TextBox>()
.Where(control => control.Name.StartsWith("TxtBox"))
.Select(control => control.Text);
That will enumerate the Text value for all child controls of tableLayoutPanel1 where their type is TextBox and their name starts with "TxtBox".
You can optionally relax the filters removing the OfType line (that excludes any non TextBox control) or the Where line (that allow only the control which name matches your example).
Ensure to have
Using System.Linq;
at the beginning of the file.
Regards,
Daniele.
public void AddNewTextBox()
{
txt = new TextBox();
tableLayoutPanel1.Controls.Add(txt);
txt.Name = "TxtBox" + this.cLeft.ToString();
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
txt.KeyPress += txt_KeyPress;
}
private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//the sender is now the textbox, so that you can access it
System.Windows.Forms.TextBox textbox = sender as System.Windows.Forms.TextBox;
var textOfTextBox = textbox.Text;
doSomethingWithTextFromTextBox(textOfTextBox);
}
I will do what I can to get a snipit of the txt files on here for you to look at. most of them are class and it will take a bit of work for me to do that. The idea is to take a txt file with say a list of over 500 places run them against a list of 50 places and pull out the ones that match. As I said I have tried many diffrent ways, well all the ones I know, and I can not seem to get it to work right. I am trying to take the following code and have it do the above action. Does that help.
InPutBox = Input.Text;
int x = 1;
var lines = Input.Text.Split(new string[] { Environment.NewLine },StringSplitOptions.None);
for (var index = 0; index < lines.Length; index++)
{
var line = lines[index];
do
{
x++;
System.Console.WriteLine("'{0}'", InPutBox);
bool test1 = InPutBox.StartsWith("TAG");
bool test2 = InPutBox.EndsWith("TAG");
if (test1 && test2)
{
int first = InPutBox.IndexOf("TAG");
int last = InPutBox.LastIndexOf("TAG");
InPutBox = InPutBox.Substring(first, last - first);
}
}
while (x < 50);
System.Console.WriteLine("'{0}'", line);
if ((line.StartsWith("TAG") && line.EndsWith("TAG")))
{
MessageBox.Show("These are errors in line" + index + ": " + line);
break;
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Find a control in C# winforms by name
I will try to explain it.. So I have a for loop, and each time I want to load the textbox number equal with the cycle integer.
Here is my code:
for(int i=1 ; i<=indulok+1 ; i++)
{
StreamWriter sw = new StreamWriter("futamok/"+comboBox1.Text + "/" + indulok + ".txt");
sw.WriteLine(textBox1.Text);
sw.Dispose();
}
Now I want to replace textBox1.Text to something like this:
sw.WriteLine(textBox + i + .Text);
But this is not working of course.
Here is an alternative that would find the control by name.
for (int i = 1; i <= indulok + 1; i++)
{
if (this.Controls.ContainsKey("textBox" + i.ToString()))
{
TextBox txtBox = this.Controls["textBox" + i.ToString()] as TextBox;
if (txtBox != null)
{
string FileName = "C:\\" + i.ToString() + ".txt";
System.IO.File.WriteAllText(FileName, txtBox.Text);
}
}
}
If your control is nested inside other controls (ie. a GroupBox), you may need to use the Find function
for (int i = 1; i <= indulok + 1; i++)
{
Control[] controls = this.Controls.Find("textBox" + i.ToString(), true);
if (controls.Length > 0)
{
TextBox txtBox = controls[0] as TextBox;
if (txtBox != null)
{
string FileName = "C:\\" + i.ToString() + ".txt";
System.IO.File.WriteAllText(FileName, txtBox.Text);
}
}
}
You can put your textboxes into some sort of indexed collection. the simpelest of which is an array
so, assume you had some array like
textBox[] textBoxes
then you can call
sw.WriteLine(textBoxes[i].text);
I guess you are running some sort of Windows Forms-project? If you are, you can loop the Controls-array to find all textboxes:
foreach (Control c in this.Controls)
{
if (c.GetType() == textBox1.GetType())
sw.WriteLine(c.Text);
}