Jump to content

tabControl with 6 tabs. Each tab has a listbox. Please help.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
Trevor

Trevor

    Newbie

  • Members
  • Pip
  • 4 posts
Hello,

I'm new to C# and could use some help! :) I have two questions that I'm sure are actually very simple to you all.

I have a windows form with a tabControl and a "Select" button on it. The tabControl consists of 6 tabs. Each tab has a listBox on it with different data for each tab.

What I'm wondering is how would I be able to find out (once the select button is clicked) if ALL of the listBoxes values are -1 (no selection)? I was successfully able to do this for one listBox, but I cannot seem to have it check them all. Here is my code for it to find out if the ONE listBox does not have a selection:
  if (listTopics.SelectedIndex < 0)

            {

            MessageBox.Show("Nothing is selected");

            }

            else

            {

            MessageBox.Show("Something is selected");

            }
I hope this makes sense... That's only for the first list box, but I want it to check them all to see if an item is selected.

And for my second question,
Once I get that frustration above out of the way, I have a label on a different form that I want to display the text from whatever item is selected out of the 6 listBoxes... Is this possible?

Thanks for your time and sorry for the long post.

-Trevor

#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
I am sure you want to +Rep me for giving this Undocumented Solution that even MSDN does not have .[I am sure you know how to +rep ]

So here is your code:
foreach (TabPage tbpage in tabControl.TabPages)
            {
                foreach (Control ctrl in tbpage.Controls)
                {
                    if (ctrl.GetType() == typeof(ListBox))
                    {
                        if (((ListBox)(ctrl)).SelectedIndex < 0)
                        {
                            MessageBox.Show("Nothing is Selected in "+ctrl.Name);
                        }
                    }
                }
            }
& here is the Explanation , pseudocode

1.TabControl is named as "tabControl"

                            [COLOR=Magenta]For each Tab in the TabControl[/COLOR]
& [COLOR=Orange]for each control in the current Tab[/COLOR]
   [COLOR=YellowGreen]Check if the control is a ListBox[/COLOR]
         [COLOR=Gray]If it is a ListBox then check if  selected Index is Null[/COLOR]
[COLOR=Teal]Show a Error message.[/COLOR]

Edited by gokuajmes, 13 May 2010 - 12:28 AM.
included control name in message box


#3
Trevor

Trevor

    Newbie

  • Members
  • Pip
  • 4 posts
Hello gokuajmes,

That works perfectly! +REP was given. Now all I need help with is how to transfer the selected item name to form2 (gamename.Text). In all of those listBoxes, they can only have ONE item selected out of the 6 tabs. Once the item is selected, all of the other tabs are disabled. I have that code already figured out. I just need to know how to transfer the name of the selected item over to form2.

Thanks a bunch!!

Trevor

#4
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
write this code in the Button that transfers the selected item from parent to Child form
button1_click(object sender, eventargs e) // write in form1
{
Form2 frm2Obj = new Form2();
frm2Obj.form1TextBoxValue = textbox1.Text;
frm2Obj.show();
}

write this in the form2 to which the variable is transferred

private string tempvalue;
public string form1TextBoxValue
{
get
{
return tempvalue;
}
set
{
tempvalue = value;
}
}
I hope this Helps !!

#5
Trevor

Trevor

    Newbie

  • Members
  • Pip
  • 4 posts
Hello,

Thanks for the quick reply again. However, it's not working for me =/... From form5 (where the tabControls are) I want the selected item (from the listbox in whatever tab) to be transfered to form3 (where there is a label called gameroom) I hope this makes sense. If it doesn't PM me and I will send you my source.

Thanks for all of your help gokuajmes.

Trevor

#6
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
Ok trevor listen , we don't like giving Tailor made solutions to any of our members . If you had thought a bit , the solution i had given early was more than enough .But anyway since you requested for it here is the solution,

Write below code in Form5 , place a button with ID = button1.OnClick event of the button write the below code.
button1_click(Object sender, Eventargs e) 
{
Form3 objForm3 = new Form3();
objForm3.gameroom.Text = GetSelectedItem():[COLOR=YellowGreen] [COLOR=Black]// [/COLOR]this method should be written by [B]you[/B] .It will return the value of the selected item from the list box[/COLOR]
objForm3.Show();
}

that is it man , i hope that helps .Always think like a programmer & not as a common man !!

Edited by gokuajmes, 14 May 2010 - 08:21 PM.
missed a / in code block


#7
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts

gokuajmes said:

I am sure you want to +Rep me for giving this Undocumented Solution that even MSDN does not have .[I am sure you know how to +rep ]

So here is your code:
foreach (TabPage tbpage in tabControl.TabPages)
            {
                foreach (Control ctrl in tbpage.Controls)
                {
                    if (ctrl.GetType() == typeof(ListBox))
                    {
                        if (((ListBox)(ctrl)).SelectedIndex < 0)
                        {
                            MessageBox.Show("Nothing is Selected in "+ctrl.Name);
                        }
                    }
                }
            }
& here is the Explanation , pseudocode

1.TabControl is named as "tabControl"

                            [COLOR=Magenta]For each Tab in the TabControl[/COLOR]
& [COLOR=Orange]for each control in the current Tab[/COLOR]
   [COLOR=YellowGreen]Check if the control is a ListBox[/COLOR]
         [COLOR=Gray]If it is a ListBox then check if  selected Index is Null[/COLOR]
[COLOR=Teal]Show a Error message.[/COLOR]

Tthere is an easier way to check if nothing is selected:
if (listBox.SeletedItem != null) {
//something is selected
}

Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#8
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
Ok, so you have a form with all the tabs on it.
Create a method to check what you want to check. It must be public so it can be called from wherever you want:
public bool Check()
//Example

On the form you want to get the info on (Form3):
Form5 myForm = new Form5();
gameroom.Text = myForm.Check();
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#9
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
declare a public string named MyString;
public string MyString;
Then, instead of wasting time and resources, use this:
if (listBox1.Enabled) if (listBox1.SelectedItems != null) myString = listBox1.SelectedItem;
 if (listBox2.Enabled) if (listBox2.SelectedItems != null) myString = listBox2.SelectedItem;
 if (listBox3.Enabled) if (listBox3.SelectedItems != null) myString = listBox3.SelectedItem;
//etc... for all 6 boxes
Then, in the parent form:
ChildFormName myForm = new ChildFormName();
myForm.Show();
label5.Text = myForm.myString();

Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#10
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
Davide why ? such a Big Quote :D looks like a Giant Big Foot :w00t: is on this Thread

#11
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts

gokuajmes said:

Davide why ? such a Big Quote :D looks like a Giant Big Foot :w00t: is on this Thread
Why what?
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#12
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
waah !! just LOL .
your solution Fits his needs ,let's see if trevor likes it :cool: