Visual C#.net help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kakashi
    New Member
    • Oct 2009
    • 9

    Visual C#.net help

    I want to only allow for one checkbox to be cheacked at one time so the user cannot delete multiple items at once. I think I need a event handeler or something I not relly sure also where I should put it too. I narrowed down where I should put it but Im not 100% on this. here is the code it that helps
    The main code;
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ToDoListWM5
    {
        public partial class Form1 : Form
        {
    
            private Model toDoListModel;
    
            
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                toDoListModel = new Model();
                this.deleteToDoItemsView1.ViewData = toDoListModel.ToDoListViewData;
                this.deleteToDoItemsView1.Visible = true;
                this.addToDoItemView1.Visible = false;
            }
    
            private void menuItem2_Click(object sender, EventArgs e)
            {
                switch (((MenuItem)sender).Text)
                {
                    case "Delete":
                        {
                            toDoListModel.DeleteToDoListItems(this.deleteToDoItemsView1.ViewItemsToDelete);
                            this.deleteToDoItemsView1.ViewData = toDoListModel.ToDoListViewData;
                            this.menuItem1.Text = "Add";
                            this.menuItem2.Text = "Delete";
                            this.deleteToDoItemsView1.Visible = true;
                            this.addToDoItemView1.Visible = false;
                            break;
                        }
                    case "OK":
                        {
                            toDoListModel.AddToDoListItem(this.addToDoItemView1.ViewItemToAdd);
                            this.deleteToDoItemsView1.ViewData = toDoListModel.ToDoListViewData;
                            this.menuItem1.Text = "Add";
                            this.menuItem2.Text = "Delete";
                            this.deleteToDoItemsView1.Visible = true;
                            this.addToDoItemView1.Visible = false;
                            break;
                        }
                }
            }
    
            private void menuItem1_Click(object sender, EventArgs e)
            {
                switch (((MenuItem)sender).Text)
                {
                    case "Add":
                        {
                            this.addToDoItemView1.ViewData = toDoListModel.ToDoListViewData;
                            this.menuItem1.Text = "BACK";
                            this.menuItem2.Text = "OK";
                            this.deleteToDoItemsView1.Visible = false;
                            this.addToDoItemView1.Visible = true;
                            break;
                        }
                    case "BACK":
                        {
                            this.deleteToDoItemsView1.ViewData = toDoListModel.ToDoListViewData;
                            this.menuItem1.Text = "Add";
                            this.menuItem2.Text = "Delete";
                            this.deleteToDoItemsView1.Visible = true;
                            this.addToDoItemView1.Visible = false;
                            break;
                        }
                }
    
            }
    
    
    
    
        }
    }
    And here is the delete code;
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ToDoListWM5
    {
        public partial class DeleteToDoItemsView : UserControl
        {
    
            private ToDoListDS.ToDoListDataTable viewData;
    
            private ToDoListDS.ToDoListDataTable viewItemsToDelete;
            
            public DeleteToDoItemsView()
            {
                InitializeComponent();
                viewItemsToDelete = new ToDoListDS.ToDoListDataTable();
            }
    
            public ToDoListDS.ToDoListDataTable ViewItemsToDelete
            {
                get
                {
                    viewItemsToDelete.Clear();
                    foreach (ListViewItem item in this.listView1.Items)
                    {
                        if (item.Checked)
                            viewItemsToDelete.AddToDoListRow(item.Text);
                    }
                    return viewItemsToDelete;
                }
            }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I want to only allow for one checkbox to be cheacked at one time so the user cannot delete multiple items at once
    Make life easy on yourself and use a radiobutton instead of a checkbox

    Comment

    • Kakashi
      New Member
      • Oct 2009
      • 9

      #3
      I look at radio button but for the program I dont think that would work because I have to add items to the list on the fly. Do radiobuttons do that?

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        If you are adding a checkbox on the fly you can add a radio button on the fly

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          Hmm, is this something that's built in? Like a checkstate on a listbox or something like that?

          If so, you'll just have to manually change the check state depending on your input. Something along the lines of...

          Code:
          foreach (CheckBoxObj checkBox in ListOfCheckBoxObjects)
          {
            if (checkBox != checkBoxWeAreChecking)
            {
              checkBox.Checked = true;
            }
            else
            {
              checkBox.Checked = false;
            }
          }
          Be careful using this though as you don't want to get yourself into infinitely recursive event calls. I don't know the exact setup of your code but maybe that will give you some ideas?

          Comment

          • Kakashi
            New Member
            • Oct 2009
            • 9

            #6
            Well this is built in visual C# using a form and i think it is in a listbox like you said. I kinda got this out of a book and am moding it to get some experance for moble dev. I remeber reading something about checkstate when I was looking this problme on the internet but I do not know where to put the code you have made into my code, also the checkboxes are not declared in the code I think they were made with visual studio.

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              If it's a checkstate on a listbox then yea, it's something built in. The code I posted isn't actual code... it's just an example of an idea, you'll need to adapt it to your own purposes :)

              Also, a good place to put it would be an event on your listbox... is there an event where the checkstate is changed? Look at the event list in Visual Studio or search around on MSDN.

              Comment

              • Kakashi
                New Member
                • Oct 2009
                • 9

                #8
                Ok, that makes sense. I sould put it in the listbox as an event. I have tried to look at MSDN and got lost for about 2 hours ;( That is turlly sad is it not. For the codeing would I have to have multiple if else for each possible checkbox or would one work for all to make sure that there is only one checkbox checked?

                Comment

                • GaryTexmo
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1501

                  #9
                  I'm not sure what to tell you other than dive in and start learning about events and the controls you're using. MSDN and Google are great references. If you get stuck once you've done a bit more coding, check back here and we can help you sort through it.

                  I feel like if I do anything more I'll just be giving you the solution and it would be better for you to learn to do it on your own. I'm happy to help, I just don't think it's appropriate for me to do it for you :)

                  Comment

                  Working...