Windows Forms Fatch Combobox's value Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KalariaNitya
    New Member
    • Apr 2008
    • 34

    Windows Forms Fatch Combobox's value Problem

    hello all,

    i need ur help..

    i have 10 combobox & 10 textbox on windows forms

    it's names like :

    cmbsign1 txtdiscount1
    cmbsign2 txtdiscount2
    cmbsign3 txtdiscount3
    cmbsign4 txtdiscount4
    cmbsign5 txtdiscount5


    i just make a for loop to fatch combobox and textbox's value
    [code=vbnet]
    dim sign as new combobox
    dim discount as new textbox

    j=1
    For i = 0 To 10
    sign = CType("cmbSign" & j, ComboBox)

    txt.Name = "discount" & j
    discount.Text = txt.Text

    MsgBox(discount .Text)

    discount.Text = ("txtdiscoun t" & j)
    commission.Text = ("txtcommission " & j)

    j = j + 1
    Next
    [/code]
    i.e.
    =============== =
    sign = CType(cmbSign1, ComboBox) if we do like this then work but i want

    sign like
    sign = CType("cmbSign" & j, ComboBox)

    it gives error that can not convert from string to combobox..

    can anybody help me?

    any help will be appriciated..

    thanking u in advanced..
  • shweta123
    Recognized Expert Contributor
    • Nov 2006
    • 692

    #2
    Hi ,

    You can try this code for your requirement :

    Code:
    //variables for program
    string txtName="";
    string cmbName="";
    string txtValue;
    string cmbValue;
    TextBox t1 = new TextBox();
    ComboBox c1 = new ComboBox();
     
    //For loop to get Textbox and Combo box values on the Form
    for (int i = 1; i <= 10; i++)
    {
     
    //define textbox name 
    txtName = "txtdiscount" + i;
     
    //define combobox name
    cmbName = "cmbSign" + i;
     
    //Get textbox control
    t1 = (TextBox)this.Controls[txtName];
     
    //Get combobox control
    c1 = (ComboBox)this.Controls[cmbName];
     
    //Get combo box value
    cmbValue = c1.Text;
     
    //Get textbox value
    txtValue = t1.Text;
     
    }

    Comment

    • KalariaNitya
      New Member
      • Apr 2008
      • 34

      #3
      hi, shweta

      first of all thanks..

      u send me code in c# right?

      i have to convert it into vb.net
      Code:
       Dim discounttxt As New TextBox 
      For i = 0 To 2
       
      Dim b As String
      Dim c As String
      b = "txtdiscount" & j
      i got the value of b
      Code:
       discounttxt = CType(Me.Controls(b), TextBox) 
      c = discounttxt.Text
       
      Next
      but can't get discounttxt.
      i got value Nothing
      i didn't get textbox control

      do u have any idea?

      thanking u in advanced....... .......
      Last edited by DrBunchman; Jun 4 '08, 12:03 PM. Reason: Added code tags - Please use the # button

      Comment

      • shweta123
        Recognized Expert Contributor
        • Nov 2006
        • 692

        #4
        Hi,

        For getting the value of discounttxt you have to add following code in your existing vb .net code :

        Code:
         Dim discounttxt as new TextBox 
         
        For i = 0 To 2
        Dim b As String
        Dim c As String
        b = "txtdiscount" & j
         
        '''''''Get textbox control here 
        discounttxt = CType(me.Controls(b),"TextBox")
         
        '''''''''''Get the value of discount textbox
        c = discounttxt.Text
        Next

        Comment

        • KalariaNitya
          New Member
          • Apr 2008
          • 34

          #5
          hi shweta
          first of thanks, for replying me such fast...

          thanks i had done according to u. but still i didn't get textbox as NOTHING

          i just try following code..
          i know it bit longer then u suggested..but will work now..code as follows:
          Code:
             
          For ksign = 1 To 10
          strsign = "cmbsign" & ksign
          sign = Me.Controls("pnlCategoryInfo").Controls(strsign)
          signvalue = sign.Text
          Next
           
          'for discount 
          For kdiscount = 1 To 10
          strdisount = "txtdiscount" & kdiscount
          discount = Me.Controls("pnlCategoryInfo").Controls(strdisount)
          Next
           
          'for commission 
          For kcommission = 1 To 10
          strcommission = "txtcommission" & kcommission
          commission = Me.Controls("pnlCategoryInfo").Controls(strcommission)
          Next
          once again thanks,
          Last edited by DrBunchman; Jun 4 '08, 12:04 PM. Reason: Added code tags - Please use the # button

          Comment

          • shweta123
            Recognized Expert Contributor
            • Nov 2006
            • 692

            #6
            Hi,

            Where are you getting error in this code?

            Comment

            • KalariaNitya
              New Member
              • Apr 2008
              • 34

              #7
              Hello,
              above pasted code is perfactly working..

              i had done it..

              thanks 4 ur help..

              i told u that b4 one was not working..

              any ways, have a great day ahead..
              thanks again



              Originally posted by shweta123
              Hi,

              Where are you getting error in this code?

              Comment

              Working...