Help Searching and replacing part of text file

motti100

Member
Joined
Mar 6, 2005
Messages
19
Programming Experience
1-3
In a file I have the following:

#levelTwoNav
{
height: 20px;
background: #177dae;
border-top: 3px solid #177dae;
/* border-bottom: 1px solid #024F74; */
padding-left: 7px;
vertical-align: middle;
}
#levelOneNav
{
height: 10px;
background: #177dae;
border-top: 2px solid #177dae;
/* border-bottom: 1px solid #024F74; */
padding-left: 6px;
vertical-align: left;
}

How can I search and replace "background" only in the first part "levelTwoNav" and avoid touching the second 'background'

Thanks
 
Hi Kulrom

Thanks for the reply.

What I am doing is the following.

I am clicking on an object (Button) and then selecting from ColorDialog a color.
The file is a stylesheet, so the button I clicked is a certain object in our software eg text or border, etc

So I have now changed the color of a certain object and want to write it to the Stylesheet file in the appropriate position.

Hope this makes sense.

Thanks
 
Ok, sorry for the delay.
Below you will find attached project that demostrates the idea how to change the color and replace the content in CSS file. Note that i made it transparent but you can hide the textbox control so, user wouldn't know what's going on there.

The main point is that you need to use Stream Writer and Stream Reader Classes to acomplish the task.

Happy coding ;)



for those who are lazy to open the project below;


PHP:
Dim path AsString = Application.StartupPath & "\myFile.css"
  
Dim myColor AsString
 
Dim i AsInteger
PHP:
PrivateSub btnReadFromCss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadFromCss.Click
  
txtCSS.Text = String.Empty
 
Dim objReader As StreamReader = New StreamReader(path)
 
Dim strContents AsString
 
strContents = objReader.ReadToEnd()
 
objReader.Close()
 
txtCSS.Text = strContents
 
EndSub





PHP:
PrivateSub btnPickUpTheColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPickUpTheColor.Click
  
If cd1.ShowDialog = DialogResult.OK Then
 
With cd1
 
myColor = System.Drawing.ColorTranslator.ToHtml(cd1.Color)
 
EndWith
 
EndIf
 
Dim q AsString = Me.txtCSS.Text
 
Dim w() AsString = q.Split(";")
 
Dim l() AsString = w(1).Split(" ")
 
w(1) = l(0).Trim & " " & l(1).Replace(l(1), myColor)
 
Dim newVal AsString = w(1) 
 
Dim finalVal AsString = w(0) & ";" & ControlChars.NewLine & newVal
 
For i = 2 To w.GetUpperBound(0)
finalVal &= ";" & w(i)

Me.txtCSS.Text = finalVal

Next

 
'using Stream Writer Class
 
Dim vbWriter As StreamWriter = New StreamWriter(path)
 
vbWriter.WriteLine(Me.txtCSS.Text)
 
'Put the data inside
 
vbWriter.Flush()
 
vbWriter.Close()
 
MessageBox.Show("Done")
 
EndSub



 

Attachments

  • WriteToCSS.zip
    26.5 KB · Views: 27
Hi Kulrom

Thanks for the code help. I really appreciate it.

Our software has a css stylesheet already created and used by our software.
I am trying to let our customers/service dept use my VB
program to change colors, fonts, etc

What I have done is the following.

One of the choices of my app is to change stylesheet.

I have used created a bit of our UI by using "buttons" with an image.

The user will clivk on the button and select text or background color.

Once he changes the color to what he wants, Click on save and it will write to the correct line in the CSS file based on different topics/items in the file.

SO my example

#levelTwoNav
{
height: 20px;
background: #177dae;
border-top: 3px solid #177dae;
/* border-bottom: 1px solid #024F74; */
padding-left: 7px;
vertical-align: middle;
}
#levelOneNav
{
height: 10px;
background: #177dae;
border-top: 2px solid #177dae;
/* border-bottom: 1px solid #024F74; */
padding-left: 6px;
vertical-align: left;
}


is actual code from the Stylesheet and these are 2 places the user can change.

If I click on the button which represents #levelOneNav, and I want to change the background, I will get the colordialog and choose say 'blue' and save.

background: #177dae; under #levelOneNav will be saved as background: blue;

I hope this is clearer.

Thanks
 
All that you claimed above my project performs as well ... Maybe, you can search for "background" instead semicolon like i've done that but that's easy to be adapted if you got the idea.
Also if you try to select black, blue, white and not the custome one ... you'll note that it will placed "white" or "black" word there instead #xxxxxx
However feel free to ask for additional assistence if you need one.

Cheers ;)
 
Writing to CSS...blues !

Hi KulromThanks for the reply. I am still having trouble with it.I have attached a jpg which has a pic of a website whic I will be able to change background and/or text,

What ever i change will be saved into an existing CSS file and when the user refreshes the page, the changes will take place.

Each of the items in the jpg are clickable and changeable and they each have different topic names in the CSS file (TopNav, etc)

What I am trying to do is when i click save, the correct sections in the CSS will be changed and nothing else affected.

How can I (when "save" is clicked), save the changes to the file in the correct places and the correct fields.?

Thank you very much.
 

Attachments

  • pic.jpg
    pic.jpg
    31.5 KB · Views: 220
It seems like you still don't get the concept of the project above. Well, here is the rough explanation of the idea: add an additional array that will hold only CSS class names. Then search for background keyword and tore in 2nd array and finally replace the value that will be stored in third array ... very simple, if i may. Anyway you can go with this but there is another fact that you should take under consideration.
Flexibility. That's why you don't need to use CSS for the purpose but, rather you need to use XML and XSLT/XSL as their main goal was to add more flexibility in handling documents.

Cheers ;)
 
Still ned help

Hi Kulrom

Last thing I promise. I am still learning so I need the last piece of code.
How can I build an array that will return only the names of the class

#levelTwoNav
#levelOneNav
#levelabc
#levela

If you can help me out on that one, I can figure the rest out.

Thanks
 
Well, after you read and pass the content to the string you can easy manipulate the string (you have a code to read from file).

VB.NET:
 Dim q AsString = Me.txtCSS.Text 
 
Dim w() AsString = q.Split("#")



#levelTwoNav
{
height: 20px;
background: #177dae;
border-top: 3px solid #177dae;
/* border-bottom: 1px solid #024F74; */
padding-left: 7px;
vertical-align: middle;
}
#levelOneNav
{
height: 10px;
background: #177dae;
border-top: 2px solid #177dae;
/* border-bottom: 1px solid #024F74; */
padding-left: 6px;
vertical-align: left;
}




now you can call levelTwoNav content like this:

Dim firstId as string = w(0)
Dim levelOneNav as string = w(1)


i hope this helps ... cheers ;)
 
Hi Kulrom

I am still stuck on the issue and I am a lot closer to my Goal, but I cannot get there..If you have time and patience, please help me.

I have attached my stle file.
In it , I have all my Websites stylesheet params/values.

The linesI need to change have */ Martin */; remarked.

In a previous mail, I attached a JPG of the VB.net form2 I load.

The user will change the colors of the screen by selecting (checkbox) and clicking on the color to change , a color dialog is loaded, the user chooses the color.

When the user is finished, he will click save and the changes will be written to the Stle sheet according to what he changed.

So my dilemma is writing to the file the correct values at the correct lines, etc.

Any more ideas>

Thanks
 

Attachments

  • style.zip
    5.8 KB · Views: 23
Back
Top