1

I am writing code to automate the code of PLSQL basic structure. So i have a list which holds the parameters, example as below,

 pv_student_name IN  VARCHAR2
 pn_number       OUT NUMBER

So the parameters must be properly aligned as above, So I have written a procedure (method) in c# which takes plain input and align the parameters and returns a list with properly aligned parameters.

But When I print in the Console using System.Console.WriteLine then the parameters are aligned properly But when I see the parameters in a List Box the parameters are not aligned properly , though the spaces are added.

Below is the alignment code.

//Alignment of parameters logic 
private List<string> alignParameters(List<string> nonAligned_parameters){
    List<string> alignedParamtersList = new List<string>();

    List<string> parameterList = new List<string>();
    List<string> inOutList = new List<string>();
    List<string> dataTypeList = new List<string>();

    foreach (string parameter in nonAligned_parameters)
    {
        string[] param = parameter.Split(' ');

        int count = 1;
        foreach (string word in param) 
        {
            if (count == 1)
            {
                parameterList.Add(word.Trim());
            }
            else if (count == 2)
            {
                inOutList.Add(word.Trim());
            }
            else if (count == 3) {
                dataTypeList.Add(word.Trim());
            }
            count++;
        }
    }
    // find the longest string in the list 
    int maxLength = parameterList.Max(x => x.Length);

    //aligning the parameters
    int numberOfElements= parameterList.Count();

    for (int i = 0; i < numberOfElements; i++) 
    {
        string param = parameterList[i].ToString().Trim();
        string inOut = inOutList[i].ToString().Trim();
        string dataType = dataTypeList[i].ToString().Trim();

        int requiredSpace = maxLength - param.Length + 1;

        string spaces = new string(' ', requiredSpace);
        string spc = "";
        if ("OUT".Equals(inOut)) {
            spc = " ";
        }
        else if ("IN".Equals(inOut)) {
            spc = "  ";
        }

        string alignedParameter = param +
                                  spaces +
                                  inOut +
                                  spc +
                                  dataType;

        // adding aligned parameters in aligned parameter list to be returned
        alignedParamtersList.Add(alignedParameter);
    }

    return alignedParamtersList;
}

Below is the procedure where it is called, when we add a new parameter then the parameter is added, aligned and then populated back in the ListBox

private void button_AddParameter_Click(object sender, EventArgs e)
{
    string error = "N";

    //validation code here

    if ("N".Equals(error)) {
        parameterName = label_paramPrefix.Text +
                        textBox_parameterName.Text +" "+
                        combo_InOut.SelectedItem.ToString()+" "+
                        combo_DataType.SelectedItem.ToString();
    }

    parameterList.Add(parameterName);

    // Align the parameters
    parameterListAligned = alignParameters(parameterList);

    // populating parameter list in GUI with fresh aligned parameters
    listBox_parameter.Items.Clear();
    foreach (string parameters in parameterListAligned)
    {
        listBox_parameter.Items.Add(parameters);
        System.Console.WriteLine("param" + parameters);
    }
}

So the problem is that even if the Console outputs the parameters aligned properly, the ListBox doesn't.

Can you guys please help me with this, I need those parameters to appear aligned in the ListBox as well.

Screen shot of the application where we can see the parameters not aligned in the ListBox is attached. and console output screenshot is also attached below that.

Screenshot parameters not appearing aligned in ListBox

Proper alignment in the console

prnjn
  • 386
  • 3
  • 5
  • 20
  • 2
    Your app uses a variable width font and thus the width of each letter is not the same. (as in W vs I ) You need to change your font to a Fixed one or use a ListView using columns to display each part of your output – Steve Jan 14 '17 at 14:34
  • @Steve Thanks , But i am new to .net (i work on java, my lead asked me to make the same application in .net which i made in java) , So can you please let me know how to change the font which is getting displayed on the ListBox (Also i need to change that in a RichTextBox) as i will be displaying it there as well. Also can you please let me know how to use a LisView using column (This i didn't understood at all what you meant) , Sorry for the stupid kind of questions i am asking. :-) – prnjn Jan 14 '17 at 14:39
  • I don't understand a thing here. You have showed an interface of a desktop application (WinForms or WPF?) but marked your question with the ASP.NET tag. Can you clarify which kind of application are we talking about here? – Steve Jan 14 '17 at 14:43
  • @Steve I am using visual basics to make this, and the language is C# . Its a desktop application. – prnjn Jan 14 '17 at 14:45
  • 1
    Well, then you just click on the ListBox in your designer then look at the Properties window, you will find a Property for the Font and you can change from there using a Fixed Width font http://stackoverflow.com/questions/4689/recommended-fonts-for-programming A ListView is just another control that you can use instead of a ListBox. An answer how to use a ListView is probably too big here. Just search on the net for ListView in Details mode. – Steve Jan 14 '17 at 14:48

1 Answers1

1

All you need is to use a monospace font, you can change the font of a listview from its properties windows (choose the listview in visual studio and click F4) and then change the font to something like 'Courier'.