Hi!!!
I am working on window application and in that I have made tool strip,menustrip and instead of using richtextbox I am using listbox.
In listbox I have added items statically using :
" this.listBox.Items.Add("..............."); "

Can anyone tell me how can I link
"save as" , "save" , "print", "print preview" buttons to the listbox...

Recommended Answers

All 17 Replies

Can anyone tell me how can I link
"save as" , "save" , "print", "print preview" buttons to the listbox...

could u explain more?
what do u want to do?
I think it's not clear.

I'll assume that you want to make buttons such as "Save As..." and etc. and bind them to do some stuff with your listbox.

Just make a button (or menustrip item) and dobleclick on them in the designer, the environment will create a method to process click on the control and hook this method to the control's click event.

In the created method you can do whatever you want with your listbox, for example this code will add new item to the list, and write the list to the file:

protected void SaveAsClick(object sender, EventArgs e)
{
     listbox.Items.Add("New item");
     
     StringBuilder builder = new StringBuilder();

     for(int i = 0; i < listbox.Items.Count; i++)
          builder.Append(listbox.Items[i]).Append("\r\n");

     File.WriteAllText("SomeFile.txt", builder.ToString());
}

The alternative is to hook on the button's Click event manually, from your code (from the constructor, for example), but judging by your problem's description, that's probably too high for you...

Can anyone tell me how can I link
"save as" , "save" , "print", "print preview" buttons to the listbox...

Here is what a menu item's click event wiring looks like. Usually, this is done by the designer, or you can put the code in your form's constructor:

this.mnuFileSave.Click += new System.EventHandler(this.mnuFileSave_Click);

and, you would create an event handler that looks like:

private void mnuFileSave_Click(object sender, EventArgs e)
        {
            // save my listbox items code here
        }

The code is the same for wiring a button's click event. Hope that helps.

I tried ur code but its giving error.........

I know that on click button event i have to add code..........
but what code to be used for saving listbox items.......

It is not ur it is your.:@
If you litterally typed in the code of DdoubleD it is likely that it will give errors. Different names I guess. If you say you have an error, please state what kind of error. You will get the answer you need more quickly. It is like your ur, not everybody understands that and will be not so keen to give you an answer.

DdoubleD has mentioned where to add the code , not the code..........
I know the code for richtextbox for save,print etc.,like in wordpad..........
But i could not apply the same functionality for listbox items........
tell me how to do the same for listbox

Any idea why a RichTextBox is called rich?
For a humble listbox it is all up to you to respond to a click of your save menuitem if you want to save your listbox.
In a response to a click of my save menuitem I would do the following:
open a file
read the listbox items
write each listbox item to the file
close the file

As I've posted above, you must manually implement your saving logic in click event handler.

There is NO way to, for example, write something like

protected void Click(object sender, EventArgs e)
{
     PleaseDoItLikeRichTextBoxDoes();
}

You must use richtextbox to have this functionality implemented out-of-the-box, or REIMPLEMENT it. No other way, sorry...

[Edited]
Well, there is another way - writing hidden richtextbox proxy object and handle save calls through it, but that's REALLY bad practice...

commented: I like this! +8

I tried the following code :

        System.IO.TextWriter wr = System.IO.File.CreateText("text.ini");

        for (int t = 0; t < listBox1.Items.Count; t++)
        {
            string aline = listBox1.Items[t]+ ","; // comma delimited 
            for (int s = 1; s < listBox1.Items[t]; s++)
            {
                aline += listBox1.Items[t] + ",";// comma deliminted
            }
            aline = aline.subString(0, aline.Length - 1); // remove trailing comma
            wr.WriteLine(aline);
        }

        wr.Flush();
        wr.Close();

But this code is giving error

You are doing it again!
>> But this code is giving error
What error?
Where (what line) did it occur?
And please post your code between CODE tags.

You are doing it again!
>> But this code is giving error
What error?
Where (what line) did it occur?
And please post your code between CODE tags.

LOL, you read my mind.

its giving following errors:
1)Operator '<' cannot be applied to operands of type 'int' and 'object'

2)'string' does not contain a definition for 'subString'

Also tell me whether I am using correct code or not

its giving following errors:
1)Operator '<' cannot be applied to operands of type 'int' and 'object'

2)'string' does not contain a definition for 'subString'

1) You are comparing your iterator "s" to the wrong object--you left out the "Length" property in your comparison.

2) spelling error? subString s/b Substring... so it can't find the definition

I could have provided you the code, but don't you think you need to learn why you are getting the errors and try to fix them yourself?

This is your code between CODE tags :

System.IO.TextWriter wr = System.IO.File.CreateText("text.ini");

for (int t = 0; t < listBox1.Items.Count; t++)
{
  string aline = listBox1.Items[t]+ ","; // comma delimited 
  for (int s = 1; s < listBox1.Items[t]; s++)
  {
  aline += listBox1.Items[t] + ",";// comma deliminted
  }
  aline = aline.subString(0, aline.Length - 1); // remove trailing comma
  wr.WriteLine(aline);
}

wr.Flush();
wr.Close();

I have not the faintest idea what lines 5 to 11 are doing in your for t loop. Just keep line 8 in it, then remove line 10 and 11 outside, after the flush.
You see, it pays of to formulate your questions so that we can understand them;)

Any idea why a RichTextBox is called rich?
For a humble listbox it is all up to you to respond to a click of your save menuitem if you want to save your listbox.
In a response to a click of my save menuitem I would do the following:
open a file
read the listbox items
write each listbox item to the file
close the file

Thanks a lot........
I have done it myself

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.